Лав. Разработка Ядра linux 2006
Post on 02-Mar-2018
226 Views
Preview:
TRANSCRIPT
-
7/26/2019 . Linux 2006
1/451
-
7/26/2019 . Linux 2006
2/451Novell
Ximian Desktop, Novell
Linux
www.williamspublishing.com
NOVELL PRESS
;)
-
7/26/2019 . Linux 2006
3/451
Linux
-
7/26/2019 . Linux 2006
4/451
Linux Kernel
DevelopmentSecond Edition
Robert Love
Novell Press, 800 East 96th Street, Indianapolis,
Novell
-
7/26/2019 . Linux 2006
5/451
Linux
-
7/26/2019 . Linux 2006
6/451
32.973.26018.2.75
13
681.3.07
"c"
. ..
A.
"" :
info@williamspubiishing.com, http://www.williamspublishing.com
115419, , / 783; 03150, , / 152
, .
13 Linux, 2 . : . . . : "..
2006. 448 . : . . . .
ISBN 5845910854 (.)
Linux 2.6, , . : , , , , , VFS, , .
Linux. , , .
, , .
32.973.26018.2
. , , Novell Press.
Aufhorued translation from the English language edition published by Novell Press, Copyright 20by Pearson Education, Inc.
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permsion from the publisher.
All terms men tione d in this book that are known to be trademarks or service marks have been appr
rialely capitalized. Novell Press cannot attest to the accuracy of this information. Use of a term in this boshould not be regarded as affecting the validity of any trademark or service mark.Russian language edition is published by Williams Publishing House according to the Agreement w
R&I Enterprises International, Copyright 2006
-
7/26/2019 . Linux 2006
7/451
15 17
21
22
1. Linux 23
2. Linux 33
3. 45
4. 65
5. 95
6. 109
7. 131
8. 163
9. 177
10. 207 1 1 . 233
12. 265
13. 293
14. 311
15. 331
16. 343
17. kobject sysfs 355
18. 373
19. 389
20. , 405
. 415
. 423
. 429
. 433
437
-
7/26/2019 . Linux 2006
8/451
15 17... 18 18 18 19 20
21
22 22
1. Linux 23
: Linux 25 26
Linux U nix 29 Linux 31 Linux 32 32
2. Linux 33
33 33 34
34 34
37 37 38
" " 38 l i b c 39 GNU 39 41 42 42 42 43
43
3. 45
t k t t 46
-
7/26/2019 . Linux 2006
9/451
47 48 50 51 51 52
53 54
Linux 57 59
. 59 61 " " 61
63
4. 65 67, 67 68 69 . 70 70
71 72
74 75 78 81 83 87 88 88
89 91
, 91 92 92 93
5. 95
I, POSIX 96 syscall 97
. 98
99 99 99 100
-
7/26/2019 . Linux 2006
10/451
10
10
104
104
106
107
10
6. 109
109
11
11
112
114
115
116 117
119
121
124
125
126
127
, ! 128
7. 131
132
133
133
(softirq) 136
136
139
141
141 144
ksoftirqd 146
148
149
150
154
157
157
159
160 162
-
7/26/2019 . Linux 2006
11/451
8. 163
164
164 167
169
170 172
174
176
9. 177
1 7 7 17 8 1 81
1 83 186 1 87 1 88 1 9 0 1 93 1 9 3
. 1 95 1 9 6 1 96
K: 1 97 1 99 2 0 0 2 0 1 2 0 5
10. 207
208
: HZ 209
HZ 210 jiff ies 213
j iffies 214
jiffies 215
HZ 217
218
. 218
218
219
221 223
224
, 226
-
7/26/2019 . Linux 2006
12/451
226
227
227
229
schedule_timeout () 230
232
1 1 . 233
233
235
238
238
239
kmalloc () 240
gfp_mask 241
kf () 245
vmalloc () 246
248
249
252
254
256
257
257
257 258
, 259
percpu 260
, , 260
, , 261
, 263
264
12. 265
266
266
Unix 267
VFS 269
VFS 270
superblock 270
272
inode 274
276
dentry 278 280
dentry 280
281
-
7/26/2019 . Linux 2006
13/451
file 283
284
, 288
, 289
Linux 291
13. 293
294
295
bio 298
300
. 301
301
302
302
503 304
307
308
noop 309
309
310
1 4 . 3 1 1
313 315
315
mm_struct 316
316
VMA 317
VMA 319
320
321
322 find_vma () 323
find_vma_prev () 324
find_VMA_intersection () 324
mmap () do_mmap (): 325
mmap () 326
munmap () do_munmap (): 327
munmap () 327
327
329
15. 331
332 a dd res s_sp ac e 332
-
7/26/2019 . Linux 2006
14/451
335
336
336
pdflush 337
bdflush kupdated 339
: 339
341
16. 343
"Hello,Worldl" 343
345
345
347
347
347
348
349
351
353
354
17. kobject sysfs 355
kobject 356
ktype 357
kset 358
358
359
kobject 360
361
kref 362
sysfs 363
sysfs 365
sysfs 366
369 kobj ect sysfs 371
18. 373
373
374
pr intk () 375
pr intk () 375
376
377 syslogd klogd 377
pr intk () 378
Oops 378
ksymoops 380
-
7/26/2019 . Linux 2006
15/451
kallsyms 380
381
38]
382
SysRq 382
384
gdb 384
kgdb 385
kdb 385
385
UID 385
386
386
387
388
388
19. 389
Linux 390
391
' 394
394
395
char 396
396 397
397
398
399
bigendian littleendian 401
401
401
402
403
, 403 404
20. , 405
405
406
406
406
407
408 408
408
typedef 410
-
7/26/2019 . Linux 2006
16/451
, 41
ifdef 41
41
41
41
' 41
41
41
41
. 41
41
41
Linux 41
41
41
42
. 42
42
42
42
42
. 42
42
43
43
43
, 43
. 43
43 Unix 43
Linux 43
43
API Unix 43
43
Web 43
43
-
7/26/2019 . Linux 2006
17/451
(Doris) (Helen)
, Linux , ,
Linux.
, ,
Linux,
,
.
:
. , , . ,
,
.
, Linux
, ,
. , Linux, ,
,
.
: , , " ,
" .. (Linus
Torvalds).
, ,
, ,
. (
. , ,
.)
,
.
.
-
7/26/2019 . Linux 2006
18/451
(Robert Love) ,
,
.
: ,
, , , ..
, ,
, ,
.
, :
.
,
.
. ,
. .
! ,
,
. , ,
Linux, , ,
.
(Andrew Morton)
Open Source Development Labs
-
7/26/2019 . Linux 2006
19/451
Linux
, , , .
. ,
, ,
. ? ,
, .
,
. .
. . ,
. ,
.
,
Linux. ,
. , .
, .
, , ,
( ),
. ,
, . ,
.
, .
.
:
, .
. , ,
Linux
1
2.7. 2.6.
, ,
, , 2.6,
. ,
, " " .
, .
, .
, , , .
1 Linux (Linux Kernel Development
Summit), 2004 . , .
-
7/26/2019 . Linux 2006
20/451
. . .
, Unix.
,
. , , ,
, .
, .
Linux ,
. .
.
! !
! ,
.
Linux 2.6 2.6.10.
" ",
.
,
, .
,
Linux.
.
(API)
(, API Linux ).
,
. , .
, ,
, , . ,
,
, . , ,
Linux,
.
, , . ,
. 7, "
", (bottom half).
-
7/26/2019 . Linux 2006
21/451
( ), , bottom half ( ).
,
. , , , , , . , .
, . , (API). , .
, , . , . , , . , , , , , . , , , ,
.,
Linux. . , , .
, , . , , . ; .
h t t p : //tech9.net/rml/kernel_book/, , , , . .
-
7/26/2019 . Linux 2006
22/451
, , (
, ), , , .
, , .
, (Scott Meyers) , . (Georg Nedeff), , . (Margo Catts). , , .
(Adam Belay), (Martin Pool) (Chris Rivera). . , , , . (Zak Brown), .
, , , . (Andrea Arcangely),
(Alan ), (Greg KroahHartman),
(Daniel Phillips), (David Miller), (Patrick Mochel), (Andrew Morton), (Zwane Mwaikambo), (Nick Piggin) (Linus Torvalds). ( ).
. (Paul Amichi), (Keith Barbag), (Dave Eggers), (Richard Erickson), {Nat Friedman), (Dostin Hall), (Joyce Hawkins), (Miguel de Icaza),
(Jimmy Krehl), (Doris Love), (Jonathan Love), (Patrick LeClair), (Linda Love), '(Randy O'Dowd), (Salvatore Ribaudo) , (Chris Rivera), (Joey Shaw), (Jeremy VanDoren) , (Steve Weisberg) (Helen Whinsnant).
, . !
,
. , , .
-
7/26/2019 . Linux 2006
23/451
(Robert Love) Linux . GNOME. Ximian Desktop Novell. Vista Software.
, , , () (preemptive kernel), , (VM),
. schedutils GNOME. Linux Journal.
. , , . , .
-
7/26/2019 . Linux 2006
24/451
, , . , , . , .
. Web . , , , ,
. ,
, . . :
E m a i l : i n f o @ w i l l i a m s p u b l i s h i n g . c o mWWW: http://www.williamspublishing.com
:
: 115419, , / 783: 03150, , / 152
Sams
Publishing www. nowellpress.com.
ISBN (
) .
-
7/26/2019 . Linux 2006
25/451
L i n u x
()Unix
. Unix 1969 , (Dennis Ritchie) (Ken Thompson) , , .
Unix Multics , BellLaboratories. Multics, Bell Laboratories
Computer Sciences Research Center . 1969 Bell Labs , Unix. PDP7. 1971 Unix PDP11, 1973 , , . Unix, Bell Labs, UnixSystem 6, V6.
Unix ., , , , 1977 Bell Labs Unix System III, 1982 AT&T System V
1.
Unix, , , , .
(University of California at Berkeley).
1 System IV? , o .
1
-
7/26/2019 . Linux 2006
26/451
Unix Berkeley SoftwareDist r ibut ions (BSD). Unix, 1981 , 3BSD. 4BSD:4.0BSD, 4.1BSD, 4.2BSD 4.3BSD. Unix
, (dem and pagin g) TCP/IP. Unix
4.4BSD, 1993 , . BSD Darwin, Dragonfly BSD, FreeBSD, NetBSD OpenBSD.
19801990 , , Unix. AT&T
, . Tru64 Digital, HPUX Hewlett Packard, AIX IBM, DYNIX/ptx
Sequent, IRIX SGI, Solaris Sun. Unix , , Unix , . Unix . , Unix :
, U nix . , Unix 2.
, : o pen ( ) , re a d ( ) , w r i t e ( ) , i o c t l ( ) c l o s e ( ) . , Unix Unix .
U nix fork() . , Unix ,, ,
, , , .
Unix , , , , , , , TCP/IP. Unix , Unix . Unix , ( )
2, , , . , Plan9 ( Unix), .
-
7/26/2019 . Linux 2006
27/451
Unix,
.
Unix
. ,
,
Unix .
: L i n u x Linux (Linus
Torvalds) 1991 ,
Intel 80386.
Unix .
DOS, Microsoft,
, " ", . Minix, Unix
, .
( Minix),
, Minix.
,
. ,
Unix .
, . , Unix. 1991
.
, Linux
.
Linux , ,
, .
, Linux ,
.
Linux ,
AMD 8664, ARM, Compaq Alpha, CRIS, DEC VAX, H8/300, Hitachi
SuperH, HP PARISC, IBM S/390, Incel IA64, MIPS, Motorola 68000, PowerPC, SPARC,
UltraSPARC v850. , ,
.
Linux . ,
Linux {Monta Vista Red Hal), (IBM, Novell)
, .
Linux Unix, Linux
Unix. Linux Unix, Linux
API Unix ( POSIX Single UnixSpecification), Linux
Unix, Unix, , ,
, ,
-
7/26/2019 . Linux 2006
28/451
Unix
.
Linux ,
; , ,
. ,
Linux ,
. Linux. Linux,
,
3.
, Linux GNU General Public License
(GPL) 2.0.
.
,
,
4
. Linux
. ,
, , , ,
(login) (shell).
Linux
X Windows, (desktop
environment), , , GNOME. Linux
. Linux, ,
Linux., ,
, Linux . , Linux .
,
. , ,
, . , ,
, .
, (boot loader),
, .
, .
, .
, .
,
3 , , http://www.fsf.org http://www.opensource.org.
4, GNU GPL, . COPYING,
, .
http://www.fsf.org.
-
7/26/2019 . Linux 2006
29/451
. ,
,
.
(core) .
,
, ,
, , , ,
.
.
.
, , , ,
( , kernelspace). ,
(
, , userspace).
, ,
.
() ,
, .
, ,
(system call) (. 1.1).
,
, , ,
, . ,
,
. pr int f () .
write () .
. ,
open () ,
pen () . , , , strcpy (),
, .
, ,
. , ,
, .
,
, .
.
, ,
Linux, (interrupt).
, ,
5.
5 , ,
. *. .
-
7/26/2019 . Linux 2006
30/451
.1.1. , . .
. (interrupt handler), . , , ,
, . ,
. , , , . : , . . (interrup context), . , .
. , , Linux .
.
1
2
3
-
7/26/2019 . Linux 2006
31/451
, .
.
L i n u x U n i x
API, Unix . Unix . , , . Unix (memory management unit);
. Unix.
, ,
: . ( ,
, , ,
.)
, 1980 . ,
,
.
. ,
, , .
, .
.
.
, . , , . . , , . , , , . . (Inter Process Comrrmnication, IPC) , "" IPC. .
, . IPC , ,
,
, .
-
7/26/2019 . Linux 2006
32/451
, , , , , . Windows NT, Mach ( Mac OS X) . Windows NT, Mac OS X ,
. Linux , .. , . Linux : , ( ). Linux , : , . , Linux , , .
.
Linux, , Linux , Unix (, , API Unix). Linux Unix,
! Linux, Unix.
Linux .
Linux , .
Linux (SMP). Unix SMP, Unix .
Linux . Unix, Linux , .
Unix Solaris IRIX. Linux
(threads): . , .
Linux Unix, , , , STREAMS, "" .
Linux . , Linux, Linux. ,
-
7/26/2019 . Linux 2006
33/451
. ,
Linux "" :
,
. ,
Unix, ,
. , Linux
Unix.
L i n u x Linux : (stable)
(development).
, .
. , ,
. , .
Linux
(. 1.2.). ,
, . (ma
or) , (minor),
, revision). ,
; , ,
, . , , 2.6.0
. 2, 6 0.
" ", 2.6.
6
( )
2
2.6.0
. 1.2.
.
, .
,
.
. .
, .
. () , , . ,
2.5 2.6.
-
7/26/2019 . Linux 2006
34/451
. . , . 2004 Linux 2.6 Linux 2.7. , 2.6 ; , , , .
, , , , , . , , 2.6 . , .
2.6.
L i n u x Linux,
Linux.
Linux(linuxkernel mailing list).
ht tp://vger .kernel .org.
, (
300 ) (
, ) .
;
, .
,
.
Linux: ,
.
, . ,
.
, .
, Linux
. ,
Linux, "" ,
, . ,
Linux,
. ,
.
!
!
-
7/26/2019 . Linux 2006
35/451
2
L i n u x
, Linux: ,
. , Linux, , , .
, , , . .
.
tar (tarball), ttp://www.kernel.org.
, . ernel.org , , .
tar
NU zip (gzip) bzip2. bzip2 , gzip. bzip2 l i n u x x . . z . t a r . b z 2 , , , z
. . tar GNU zip, .
$ tar xvzf linuxx..z.tar.gz
bzip2, .
$ tar xvjf linuxx..z.tar.bz2
-
7/26/2019 . Linux 2006
36/451
linuxx.y.z.
/ u s r / s r c / l i n u x . , . ,
, . , , root,
root . / u s r / s r c / l i n u x .
Linux (patch)
.
.
(incremental
patch), .
,
.
.
, ,
.
$ patch p1 < ../patch..z
.
.
,
. ,
, . 2.1. , ,
. COPYING (GNU GPL v2).
CREDITS ,
. MAINTAINERS ,
. , Makefile .
. , , ,
, glibc. 2.6
,
2.4.
-
7/26/2019 . Linux 2006
37/451
2.1.
Linux, , , .
. , . , , ,. CONFIG_FEATURE. , (Symmetric mult iprocessing, SMP) CONFIG SMP. , MP . , SMP . .conf ig , , make xc o n f i g . , , .
: (boolean) (instate).
yes . , CONFIG_PREEMPT, .
yes, no m o d u l e . module
, , (.. ,
) . .
arch
crypto
Documentation
drivers
fs
include
init
ipc
kernel
lib
mm
net
scripts
security
sound
usr
API
VFS
,
Linux
(initramfs)
-
7/26/2019 . Linux 2006
38/451
, , . , , . , .
, Linux , Novell Redhat, . . . , , , , .
, .
:
make config
, yes, no module { ). , , ncurses:
make menuconfig
X11:
make xconfig
, gtk+
make gconfig
, Processor Features ( ) Network Devices ( ). , , .
$ make defconfig
, , . ( , i386 ), , . , , , .
.config. , , . .
-
7/26/2019 . Linux 2006
39/451
,
:
make oldconfig
, .
, :
make
, 2.6
make dep ,
. ,
bzlmage, . ,
Makefile, ,
!
, ,
, ,
,
make (1):
make > "__"
,
. ,
, .
make > /dev/null,
.
make (1)
.
, . ,
(,
).
make (1) ,
.
"
", . ,
. .
$ make jn
n , .
-
7/26/2019 . Linux 2006
40/451
. ,
.
$ make j4
, d i s tcc (1) ccache( l) ,
.
, .
.
,
,
.
, ,
!, x86, grub
arch/i386/boot/bzlmage
/boot /etc/grub/grub.conf ,
. ,
LILO, /etc/ l i lo.conf
l i l o (8) .
. root.
$ make modules_install
System.map.
.
.
" "
,
,
.
.
, .
( , , ),
. .
.
GNU . , .
.
.
-
7/26/2019 . Linux 2006
41/451
,
SMP,
.
.
,
.
l i b c
,
(
). ,
,
. ,
, .
,
. ,
l i b / s t r i n g . .
.
, , , . ,
,
p r i n t f ( ) .
pr i n t f ( ) , pr i nt k () . pr intk( )
(kernel log buf
er), syslog.
p r i n t f ( ) :
printk("Hello world! : %s : %d\n", a_string, an_integer);
pr i nt f () pr i ntk () , p r i n t k () .
syslog , , .
:
printk(KERN_ERR " !\n");
p r i n t k () .
pr i nt k ().
G NU " " Unix, Linux . , , Linux
ANSI . , ,
-
7/26/2019 . Linux 2006
42/451
, gcc(GNU Com piler Collection GNU, , ).
ISO C991 GNU . Linux gcc,
, Imel , gcc , Linux. 99, , 99 , . , , ANSI GNU . , .
GNU (inline functions). , , , .
( ) , . ( ) , .
, . , , .
s t a t i c i n l i n e . ,
static inline void dog(unsigned long tail_size);
,
. . (static), . , .
.
1 ISO C99 ISO . 99 . ISO C99 complex.
-
7/26/2019 . Linux 2006
43/451
gcc
. , ,
, .
asm(). Limix .
,
. .
gnu ,
, .
.
l i k e l y ( ) un l i k e l y ( ) , . , if :
if (foo) {
/*..*/
}
, ,
:
/* , foo ..*/
if (unllkely(ffoo)) {
/*..*/
}
,
/* , foo ..*/
if (likely(foo)) {
/*..*/
}
,
. ,
,
,
. unl i k e l y () l i k e l y ()
.
,
. ,
.
-
7/26/2019 . Linux 2006
44/451
. , , NULL , !
, . , , . , .
, . , , .
, ,
.
, . , : ; .
""
, . , (,
, , , DOS, , ).
, , , , . . 86 4 8 .
, , 8 32 16 64 . . .
.
(race c o n d it io n ) .
, , . , .
-
7/26/2019 . Linux 2006
45/451
Linux . ,
, ,
.
.
, , ,
.
Linux . , , , .
) .
.
, Linux . , , , .
, , 64 , . .
, : , , ,
. Linux . , ; , , . .
, , , , .
, , , .
, , , , , , .
.
-
7/26/2019 . Linux 2006
46/451
-
7/26/2019 . Linux 2006
47/451
3
Unix
1. , , .. ,
. , Unix text section ( ). (data section), ; , ; . .
, (thread), , . (programcounter), . , . Unix . . , Linux . Linux .
: . , , , , , . 4," ", . , . 11, " ". , .
.
-
7/26/2019 . Linux 2006
48/451
, ; ., , . , , , .
, . Linux fork() (, ), . , fork (), (, pannt), (, child).
, . . exec*() . Linux fork() clone(), .
exit() . . wait4()
2,
. , (zombie), , wait()
waitpid().
(task). Linux . , , .
task structure ,
task list3( ).
struct task_struct, include/linux/sched.h. .
2 wa i t 4 ( ) . Linux
wa i t ( ) , wa i t p i d ( ) , wai t3( ) wai t4 ( ) .
,
.
3 task a r r ay
( ). Linux , ,
task l i s t .i
-
7/26/2019 . Linux 2006
49/451
task_struct 1,7 32 . ,, , . , , , , , (. 3.1).
task_struct , (slab allocator),
(cache coloring)(. 11, " "). 2.6 task_struct . , (, ,
86), , (stack pointer), . , thread_info, ( , ) ( , )
4(. 3.2.).
struct task_struct
struct task_struct
struct task struct
unsigned long state;int prio;unsigned long policy;
struct task_struct *parent;
struct list_head tasks;pid_t pid;
(task list)
. .1.
4 thread_info , , , .
stru ct task struct
-
7/26/2019 . Linux 2006
50/451
current_thread_inf()
struct thread_inf
thread_inf
struct task_struct
3.2.
struct thread_info 86 .
struct thread_info {
struct task_struct *task;struct exec_domain *exec_domain;
unsigned long flags;
unsigned long status;
u32 cpu;
__s32 preempt_count;
mm_segment_t addr_limit;
struct restart_block restart_block;
unsigned long previous_esp;
__u8 supervisorytack[0];
};
thread_info . thread_info task task_struct .
,
(process identification, PID). PID
, pid_t
5
, int.
5 ( o p a q u e t yp e ) ,
.
-
7/26/2019 . Linux 2006
51/451
, Unix Linux 32768( short int). pid .
, , . 32768 , . , , : , . , //sys/kernel/pid_max.
task_struct. , , , task_struct. , , , current. . task _s t ru ct , , , . , , , , thread_inf .
thread_info, task_st ruct.
86 current 13 thread_inf. current_thread_info (). .
movl $8192, %eax
andl %esp, %eax
c u r r e n t task thread_info:
current_thread_info()>task;
PowerPC ( RISC IBM), current r2. , , 8, . ,
.
-
7/26/2019 . Linux 2006
52/451
s t a t e
(. 33). .
fork()
TASK_ZOMBIE( )
: schedule () concext_switch ()
do exit()
TASK_RUNNING(,
)
TASK_RUNNING(]
TASK_INTRRUPTIBLE
TASK_UNINTERRUPTTLE
( )
,
. 3.3.
, .
TASK_RUNNING (runnable). , , , ( , runqueue,
4. " "). TASK_INTERRUPTIBLE (
, sleeping),..
-
7/26/2019 . Linux 2006
53/451
. ,
TASK__RUNNING. (wake up)
.
TASK_UNNTERRUPTIBLE TASK_INTERRUPTIBLE,
, .
, , .
, TASK_UNINTERRUPTIBLE
, TASK_INTERRUPTIBLE6.
TASK_ZOMBIE ,
wait4 ().
,
. wait4 (),
.
TASK_STOPPED . . ,
SIGSTOP, SIGTSTP, SIGTTIN SIGTTOU,
, .
.
set_task state(task, state);
/* 'task' 'state' */
. ,
(memory barrier),
(
SMP). :
task>state = state;
set current s t a te (state) set_task_
tate(current, state).
. (executable)
.
. (. 5,
") ,
.
6 "" ,
ps(1) , D, ,
SIGKILL. , ,
, , .
-
7/26/2019 . Linux 2006
54/451
, " " . current
1.
, . ,
. .
.
Linux .
init, PID
1. i ni t . ini t , , (initscripts) , .
. , ., , (siblings). . task_struct task_struct , parent,
, children. , (current), :
struct task_struct *task = current>parent;
, , :
struct task_struct *task;
struct list_head *list;
list_for_each (list, scurrent>children) {
task = list_entry(list, struct task_struct, sibling);
/* task ,
*/
}
ini t i ni t t ask . , .
1 , 6, "
". ,
. ,
.
-
7/26/2019 . Linux 2006
55/451
struct task_struct *task
for (task = current; task ! = $init_task; task = task>parent)
/* task init */
, ,
. , , . ,
.
, ,
:
list_entry(task>tasks.next, struct task_struct, tasks)
.
list_entry (task>tasks.prev, struct task_struct, tasks)
next_task (task)
( ), prev_task (task) ( ).
, for_each_process (task)
. task
:
struct task_struct *task;
for_each_process(task) {
/* PID
*/
printk("%s[%d]\n", task>comm, task>pid);
}
, ,
, .
(
).
Unix
.
(spawn).
, ,
. Unix ,
: fork () exec ()8.
8 exec() exec* () .
execve(), execlp(), execle(),
execv() execvp().
-
7/26/2019 . Linux 2006
56/451
fork() ,
.
PID (
), PPID ( PID
PID ),
, ( ),
exec () .
fork () exec ()
, .
fork()
.
. Linux fork ()
(copyonwrite)
(copyonwrite, COW)
.
.
,
, ,
. , ,
,
(readonly). ,
. ,
, , , exec () fork (),
. ,
fork (),
.
(
10 ),
, Unix .
f o r k ()
Linux fork ()
clone () .
, , (
) . "
Linux" .
f ork( ) ,v fork( ) c l oned clone ()
. clone ()
do_fork ().
-
7/26/2019 . Linux 2006
57/451
do_f ork (), kernel/fork.. , , copy_pracess () . , copy_process ().
dup_task_struct (), ,
thread_info task_struct , . .
, .
. .
. TASK_
UNINTERRUPTIBLE, , .
copy_process () copy_f lags (), flags task s truct . PF_SUPERPRIV, , . PF_FORKNOEXEC, , exec (), .
get_pid () , PID .
, clone (), , , , (namespace). . .
( 4," ").
, .
do_fork () . opy_process () , .
9.
9 , ,
, .
-
7/26/2019 . Linux 2006
58/451
, exec () , , , , .
vfork ()
vfork () , fork (), , . , exec () . . 3BSD, fork ()
. , vfork () . Linu
10,
. vfork () (, , , exec () ?), , vfork () . vfork () fork (), Linux 2.2.
vfork () clone (), .
copy_process () vfork_done task_struct NULL.
do_fvork (), , vfork_done ( ).
, , copy_process () , , vfork_done.
mm_release () (, ), vfork_done NULL, .
do_fork()
.
10 Linux. ,
, 2.6
, .
-
7/26/2019 . Linux 2006
59/451
, ,
,
. ,
.
L i n u x .
.
.
oncurrent programming),
.
Linux . Linux
. Linux
, . Linux
. ,
.
ask_s t r uct (
, , ).
Linux ,
icrosoft Windows Sun Solaris,
(
, lightweight process). " " Linux
. ,
, ,
. Linux
(
)11
.
, , .
,
.
,
. , . Linux,
, , ,
task_struct . ,
.
, , ,
clone () ,
:
Clone (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
11 ( )
Linux .
.
-
7/26/2019 . Linux 2006
60/451
,
fork (), , ,
,
. , , ,
. , fork ()
:
clone (SIGCHLD, 0);
vfork () :
clone (CLONE_VFORK | CLONE_VM | SIGCHLD, 0);
, clone () ,
,
. . 3.1
clone () .
3 . 1 . c l o n e ()
CLONE_FILES
CLONE_FS
CLONE_IDLETASK
CLONE_NEWNS
CLONE_PARENT
CLONE_PTRACE
CLONE_SETTID
CLONE_SETTLS
CLONE_SIGHAND
CLONE_SYSVSEM
CLONE_THREAD
CLONE_VFOK
CLONE_ONTRACED
CLONE_3T0P
CLONE_CHILD_CLEARTID
CLONE_CHILD_SETTID
CLONE_PARENT_SETTID
CLONE_VM
PID ( (idle) )
TID ?
(thread local storage, TLS)
SEM_UNDO System V
v f ork (): ,
CLONE_PTRACE
TA SK _S TO PP E D
TID
TID
TID
-
7/26/2019 . Linux 2006
61/451
.
(kernel thread)
, .
, ( mm NULL).
, .
,
.
Linux ,
, pdflush ksoftirq.
.
,
. :
int kernel_thread(int (*fn) (void * ) , void * arg, unsigned long flags)
clone ()
, flags.
task _ s t r uc t .
, fn,
arg.
CLONE_KERNEL, CLONE_FS, CLONE_FILES CLONE_SIGHAND,
flags.
, , ,
Linux ). ,
,
.
.
, .
, , ,
, , ,
, , "".
,
ex i t () ex i t ()
main ()). . ,
,
-
7/26/2019 . Linux 2006
62/451
. , , doexec (), .
PF_EXITING flags task struct.
del_timer_sync (),
, .
, (BSD process accounting), acct_process () , .
__exit_mm() mm_struc . ( , ), .
exit_sem (). IPC,
.
__exit_files (), __exit_fs () , exit_namespace () exit_signals () , , , .
, ,
. , exitcod
task struct. exit () , .
ex i t not i f (), (reparent)
, i nit. TASK_ZOMBIE.
schedule () (. 4, " "). TASK_ZOMBIE , , .
do_exit () kernel/exit.. , (
). (, , ), , TASK_ZOMBIE. , , , thread inf task struct.
-
7/26/2019 . Linux 2006
63/451
, .
do_exit ()
, TASK_ZOMBIE . , . , . , task_struct . wait () (
) wait4 (). , . PID . , , .
, release_task (), .
free_uid () . Linux , . , , .
unhash_process () pidhash .
(ptrace), , (pirate) .
put_task_struct () , thread_inf, a , task_struct.
, , , .
""
,
, , , , , . :
-
7/26/2019 . Linux 2006
64/451
, i n i t . do_exit ()
notif y_ parent (), forge t_or igi na l pa rent (
(reparent),
.
struct task_struct *, *reaper = father;struct list_head *list;
if (father>exit_signal != 1)
reaper = prev_thread(reaper);
else
reaper = child_reaper;
if (reaper == father)
reaper = child_reaper;
reaper
.
, reaper child_reaper
i ni t . ,
,
, .
list_for_each(list, &father>children) {
= list_entry(list, struct task_struct, sibling);
reparent_thread(p, reaper, child_reaper);
}
list_for_each (list, sfather>ptrace__children) {
p = list_entry(list, struct task:_struct, ptrace_list);
reparent_thread(p, reaper, child_reaper);
}
:
child list ,
ptraced child list. ,
, ( 2.6). ptrace,
,
(debugging).
,
.
, ,
, ,
:
.
,
-
7/26/2019 . Linux 2006
65/451
I n i t w a it () , , , .
. , , . , , Linux , ( task_struct thread_inf), (o n e () f o rk () ),
( exec ()), , ( ait ()) (
exit ()).
, , , , ( ).
, , , .
-
7/26/2019 . Linux 2006
66/451
-
7/26/2019 . Linux 2006
67/451
, .
, .
(scheduler) , , . ,
(, , )
, ,
. (multitasking) , Linux. ,
, ,
.
, , . , , ,
. , , .
(runnable). ,
, , , .
, . , .
. , . , , .
, , (, , ..). , Linux 100 ,
.
-
7/26/2019 . Linux 2006
68/451
(multitasking) : (cooperative) (preemptive,
) . Linux, Unix , . , ,
, . , , (preemption) . , , , . (timesiice). , . . , , . , Linux
, .
, ,
. , ,
(yielding). : , ; ,
; "" , , . , , , . Mac OS 9 . , Unix .
Linux 2.5, . 0(1)
(0(1) scheduler) 1.
Linux , . , (1), 0(1), , , .
1 (1) " ". , ,
,
. , " ",
, " ".
-
7/26/2019 . Linux 2006
69/451
(policy)
, , .
. , .
,
,
(I/0bound), , (processorbound).
,
. , ,
, (
, ,
, , ).
, , ,
.
, ,
.
,
, . , , ,
, . ,
. ,
, .
.
: X Windows ,
.
,
. ,
, ,
.
:
( , low latency)
hroughput).
, , ,
, . Unix , ,
, .
, ,
-
7/26/2019 . Linux 2006
70/451
, . Linux ( ), .. , , . , , ,
.
(prioritybased). ,
. , ,
( , roundrobin), .. . , Linux, . , , , . , .
Linux (dynamic prioritybased), .
, , . , , , , . Linux . ,, , , . .
Linux .
nice, 20 19, 0. nice
(ic . , ). nice ( ) niie ( ). nice , . nice 20 , nice 19 . nice Unix .
-
7/26/2019 . Linux 2006
71/451
(realtime priority), . 0 99. . Linux POSIX. Unix .
(timeslice
2) , ,
, . , , . , . , . , , . , , , , , , , .
, . , , , 20 . Linux , . Linux , . Linux ( 4.1). , Linux
. , , . .
, . , , 100 , 100 , .
20 .
2 timeslice ( ) quantum () proces
sor slice. Linux timeslice.
-
7/26/2019 . Linux 2006
72/451
5 100 800
. 4.1.
,
,
, ,
.
, ,
. , ,
, .
, ,
.
. Linux
, .
.
, Linux
. TASK_RUNNING,
. ,
, , ,
( ,
). ,
,
.
:
.
,
( ,
, ). ,
,
. .
,
,
,
100%.
-
7/26/2019 . Linux 2006
73/451
: ,
. , , .
, ,
.
. ,
,
. ,
.
, ,
.
.
Linux. ,
, ,
Linux.
Linux
kernel/sched.c.
,
2.5. ,
.
, .
(1) . ,
,
,
.
SMP.
.
SMP (SMP affinity). ,
,
, , ,
.
.
.
.
(fairness).
.
,
.
-
7/26/2019 . Linux 2006
74/451
, 12,
, .
.
(runqueue).
kernel/sched.c 3 s t r u c t ru nqu e u e . .
. . , ,
. ,
. , . .
struct runqueue {
spinlock_t lock; /* */
unsigned long nr_rinning; /* , */
unsigned long nr_switches; /* */
unsigned long expired timestamp; /* */
unsigned long nr_uninterruptible; /* */
unsigned long long timestamp last tick; /*
*/
struct task_struct *curr; /* ,
*/
struct task_struct *idle; /* */
struct mm_struct *prev_mm; /* mm_struct
*/
struct prio_array "active; /* */
struct prio_array 'expired; /* */struct prio_array arrays[2]; /* */
struct task_3truct *migration_thread; /*
*/
struct list_head migration_queue; /*
*/
atomic_t nr_iowait; /* , */
};
3 : kernel/sched., i n c l u d e / l i n u x / s c h e d . h ? , .
-
7/26/2019 . Linux 2006
75/451
, , . cpu_rq (processor) , , . t his_rq () , . , task_rq(task) ,
. ,
( 8, " "). , (, , ). , , , . , ,
. tapk_rq_lock () ask_rq_unlock(), .
struct runqueue *rq;
unsigned long flags;
rq = task_rq_lock(task, &flags);
/* */
task_rq_unlock (rq,&flags);
this_rq_lock (), , rq__unlock (structrunqueue *rq), .
, , , ( 8, ", ).
, , .
/* , ... */
if(rqllock] ;
spin_lock(Srq2>lock) ;
} else (
spin_lock(Srq2>lock) ;
spin_lock(&rql>lock)
/* ... */
/ , ... */
spin_unlock(brql>lock) ;spin_unlock(&rq2>lock);
}
-
7/26/2019 . Linux 2006
76/451
double_rq_lock ()double_rq_unlock ()
. .
double_rq_lock(rql, rq2);
/* ...*/
double_rq_unlock(rql, rq2) ;
, , . 8, "
" 9, " ".
:
.
. ,
. , , ,
.
, (
), ,
. (, spinning),
, ,
. , ,
, ,
, . ,
,
.
, .
,
. .
,
, ,
, .
,
. 8 9 .
(priority arrays):
. kernel/sched.c
s t r uc t pr io_array.
, 0(1).
,
. (priority bitmap),
,
.
struct prio_array (
int nr_active; /* */
unsigned long bitmap[BITMAP_SIZE]; /* */
struct list head queue[MAX_PRIO];/* */
};
-
7/26/2019 . Linux 2006
77/451
MAX_PRIO .
140. ,
s t r uct list_head. BITMAP_SIZE
, unsigned long.
. 140 32
, BITMAP_SIZE 5. , bitmap , 160 .
bitmap,
.
0.
( TASK_RUNNING),
bitmap 1.
, , 7, ,
7.
. , , ,
, . ,
Linux
(find first set) .
sched_find_first_bit ().
4.
.
,
st r uct l ist_head. queue.
.
,
, ,
. ,
,
.
.
nr_active,
.
( Linux)
, .
4 86 bsfl,
cntlzw.
-
7/26/2019 . Linux 2006
78/451
, , .
for ( ) (
}
. .
. , (n), n .
. .
.
, ( Linux).
Linux . : (active) (expired). , , .
, . , , , . .
, , . schedule ().
struct prio_array array = rq>active;
if (!array>nr_active) {
rq>active = rq>expired;
rq>expired = array;
}
, O(1). , (1) . .
-
7/26/2019 . Linux 2006
79/451
schedule ()
schedule (). (sleep), a . schedule ()
. , , .
schedule () , , . .
struct task_struct *prev, *next;
struct list_head *queue;
struct prio_array *array;
int idx;
prev = current;
array = rq>active;
idx = sched_find_first_bit(array>bitmap);
queue = array>queue + idx;
next = list__entry(queue>next, struct task struct, run_ist);
. . ,
. , . . 4.2.
schedule()
0 0
sch e d_f i nd_f i r s t_se t ( )
7 7
140 139 139
. 4.2. (1) Linux
, 7
-
7/26/2019 . Linux 2006
80/451
prev next , (next).
, p re v , , next, context_switch (), .
. . , , , . , . . , schedule () . .
, , , . , , , , . , .
, nice. 20 19, 0. 19 , 20 . nice s t a t i c _ p r i o task_struct . , , . , prio. .
e f f e c t i v e _ p r i o () . ic 5 5,
. , , nice, 10, , 5. , nice, 10,
, , 12. , , , , nice.
, , . , ,
. (sleep). , . ,
-
7/26/2019 . Linux 2006
81/451
, . , , ; , .
Linux , ,
, , . sleep_avg t a s k _ s t r u c t . MAX_SLEEP_AVG, 10 . , sleep_avg , , sleep_avg MAX_SLEEP_AVG. , (timer tick) , 0.
, , . , , , . , , , : , , . . , , , . , . sleep_avg. , nice, nice.
, , , nice ( ). . ,
. . , , . task_timeslice () . . , . MAX_TIMESLICE,
200 . MIN_TIMESLICE, 10 .
-
7/26/2019 . Linux 2006
82/451
, ( nice,
),
100 , . 4.1.
4.1.
nice
,
+19 5 (MIN_TIMESUCE)
0 100 (DEF_TIMESLICE)
20 800 (MAX_TIMESLICE)
:
, ,
. ,
:
, .
,
O(1). , ,
, ,
"" .
. ,
,
, .
scheduler_tick (),
( 10, " "),
.
struct task_struct *task = current;
struct runqueue *rq = this_rq();
if (!task>time_slice) {
if (!TASK_INTERACTIVE(task) || EXPIRED_STARVING(rq))enqueue_task(task, rq>expired);
else
enqueue_task(task, rq>active);
}
,
. ,
.
SK_INTERACTIVE ().
nice , "". nice( ),
. nice, 19,
-
7/26/2019 . Linux 2006
83/451
. , nice, 20,
, .
nice, , .. ,
, ,
. , EXPIRED_STARVING (),
, ,
(startving), .
,
, ,
. ,
.
, .
( , , sleeping, blocked) ,
. ,
, ""
, , ,
, .
,
.
,
. ,
( 9,
" ").
,
read () , .
.
:
, (wail queue),
sc heduled
. (wake up) : ,
.
,
: TASK_INTERRUPTIBLE TASK_UNINTERRUPTIBLE.
, TASK_UNINTERRUPTIBLE
, TASK_INTERRUPTIBLE
.
, ,
, .
(wait queue). ,
-
7/26/2019 . Linux 2006
84/451
.
wait_queue_head_t.
DECLARE_WAIT_QUEUE_HEAD ()
ini t_waitqueue_head ().
. , , ,
, . ,
(race).
,
.
:
, .
.
.
/* q ( ),
*/
DECLARE_WAITQUEUE(wait, current) ;
add_wait_queue(q, &wait);
set_current_State(TASK_INTERRUPTIBLE); /* TASK_UNINTERRUPTIBLE */
/* condition ,
*/
while (!condition)
schedule() ;set_current_state(TASK_RUNNING);
remove_wait queue(q, &wait);
, ,
.
DECLARE_WAITQUEUE ().
add wait_queue () .
, , , . ,
, wake_up () ,
.
TASK_INTERRUPTIBLE TASK_
UNINTERRUPTIBLE.
, . ,
.
, schedule ().
,
. ,
-
7/26/2019 . Linux 2006
85/451
. , schedule () .
, TASK_RUNNING remove_wait_queue().
, , . , . , schedule () ; , ERESTARTSYS; .
(wake up) wake_up (), , , . try_to_wake_up () , TASK_RUNNING, activate_task () need_resched , , , . , , wakeup () , . , , VFS wake_up () , , .
, . , , , , : , , , , (. 4.3).
, Linux
. , . , , . ? , , , , ? , ,
. .
-
7/26/2019 . Linux 2006
86/451
add_wait_q ueje() , TASK_INTER RUPTIB LE s chedu le (). sc he du le d deact ivate_task(), .
,
TASK_R UNNING
, , , try_to_wake_up() TASK_RUNNING, activate_task() schedule() . remove_wait_quaue () .
. 4,3. (sleeping) (wake up)
, , . , ,
,
. k e r n e l / s c h e d .
load_balance (). . schedule (), . 1 , , 200 . lo a d _b a la n c e () ,
, .
, , . , l o a d b a l a n c e ( ) s c h e d u l e ( ) ,
, . , . , , . 4.4.
-
7/26/2019 . Linux 2006
87/451
load_balancer()
1
2
3
4
5
6
15
1,
20
2,
15
. 4.4.
load_balance () , , , .
load _ba la nc e () find_bu si est _queu e () . . , 25% , , f ind_busiest_queue () NULL
load_balance (). .
load_balance () , . , , , (.. , not "cache hot"). , , .
load_balance () ,
( ), , .
, , . , , pu l l _ t a s k () .
, . , , load_balance ().
4
5
6
20
1
2
-
7/26/2019 . Linux 2006
88/451
load_balance (), ,
.
static int load_balance(int this_cpu, runqueue_t *this_rq,
struct sched_doraain *sd, enum idle_type idle)
{
struct sched_group *group;
runqueue_t *busiest;
unsigned long imbalance;
int nr_moved;
spin_lock(&this_rq>lock);
group = find_busiest_group(sd, this_cpu, &imbalance, idle);
if (!group)
goto out_balanced;
busiest = find_busiest_queue(group) ;
if (!busiest)
goto out_balanced;
nr_moved = 0;
if (busiest>nr_running > 1) {
double_lock_balance(this_rq, busiest);
nr_moved = move_tasks(this_rq, this_cpu, busiest,
imbalance, sd, idle);
spin_unlock(&busiest>lock);
}
spin_unlock(&this rq>lock);
if (!nr_moved) {
sd>nr_balance_failed++;
if (unlikely(sd>nr_balance_failed > sd>cache_nice_tries+2)) {
int wake = 0;
spin_lock(abusiest>lock);
if (!busiest>active_balance) {busiest>active_balance = 1;
busiest>push_cpu = this_cpu;
wake = 1;
}
spin_unlock(&busiest>lock);
if (wake)
wake_up_process(busiest>migration_thread);
sd>nr_balance_failed = sd>cache_nice_tries;
)
} elsesd>nr_balance_failed = 0;
sd>balance_interval = sd>min_interval;
return nr_moved;
-
7/26/2019 . Linux 2006
89/451
out_balanced:
spin_unlock (&this_rq>lock) ;
if (sd>balance_interval < sd>max_interval)
sd>balance_interval *=2;
return 0;}
,
. context_switch(), kernel/sched.. schedule (), .
. switch_mm (), include/asm/
mmu_context.h .
s wi tch_to () , i n c l ud e /asm/system.h, .
.
, schedule (). , , . need_resched , , schedule () (. 4.2). schediiler_tick (), , try_to_wake_up (), , , . , , schedule () . , , .
4.2. n e e d _ r e s c h e d
set_tsk_need_resched (task) need_resched
clear_tsk_need_resched (task) need_resched
need_resched() need_resched . t rue,
, fa lse,
-
7/26/2019 . Linux 2006
90/451
, need_resched . , , .
, , ( current , ). , 2.2. 2.2 2.4 task_struct int. 2.6 thread info. ,
.
(user preemption)
, , need_resched , , . ,
"" . , , .
, need_resched. , , . ,
entry.S ( , , ). , .
.
.
Linux, Unix, (, preemptible). . , , , . , ( ) . 2.6, Linux : , , ,
. ?
, ,
-
7/26/2019 . Linux 2006
91/451
. ,
, .
(SMPsafe), ,
.
, ,
preempt_count thread_inf
.
,
.
. ,
, need_resched
preempt_count. need_resched
preempt__count , ,
. .
preempt_count , ,
.
.
, ,
preempt_count . ,
, , need_r esched.
, .
,
9.
,
schedule () .
,
, ,
. ,
schedule (), , .
.
.
.
, , schedule ().
, ,
, .. ( schedule ()).
Linux
(reallime): SCHED_FIFO SCHED_RR.
SCHED_OTHER , ..
. SCHED_FIFO
" " (firstinfirstout, FIFO) .
SCHED_FIFO
SCHED_OTHER.
-
7/26/2019 . Linux 2006
92/451
SCHED_FIFO ,
, .
,
SCHED_FIFO, (round
robin). , SCHED_FIFO,
,
, .
SCHED_RR SCHED_FIFO, ,
,
. , SCHED_RR
SCHED_FIFO , .. (roundrobin)
.
SCHED_RR,
. ,
.
SCHED_FIFO, ,
SCHED_RR,
.
.
. , ,
, .
Linux
(soft realtime).
, ,
.
(hard realtime)
. Linux
. Linux
, ,
. Linux ,
,
Linux .
2.6 .
1 MAX_RT_PRIO 1,
MAX_RT_PRIO 100,
1 99.
nice
SCHED_OTHER,
MAX_RT_PRIO (MAX_RT_PRIO+40).
, nice 20 +19
100 139.
-
7/26/2019 . Linux 2006
93/451
Linux
.
,
, ,
(yield) .
,
(man pages),
( ,
). . 4.3
. , ,
5, " ".
4.3.
nice () nice
schedsetscheduler ()
sche d_getsc he dule r ()
sched_setparam ()
sched_getparam ()
sched_get_priority_max () Eched_get_priority_min ()
sched_rr_g e t_ int erva l ()
s c h e d _ s e t a f f i n i t y ( )
sc he d_ g e t a f f in i t y ()
sched_yield ()
,
sched_setscheduler () sched_getcheduler () . , , , . policy r t _ p r i o r i t y task_struct .
sched_setparam () sched_getparam () . r t_pr ior i ty, sched_param. sched_get_priority_max ()
-
7/26/2019 . Linux 2006
94/451
sched_get_priority__min () . (MAX_USER_RT_PRIO1), 1.
nice ()
. root , .. nice . nice ()
set_user_nice (), s tat ic_pr ia prio task_struct.
Linux
(processor affinity).
, : " ". cpus_allowed task_struct. . 1, . sched_setaffinity ()
. sched_getaffinity () cpus_allowed.
. , . , . , , (migration threads) . , , cpus_allowed .
Linux sched_yield ()
, . ( , ) . , , , ,
. , . ( ).
-
7/26/2019 . Linux 2006
95/451
. Linux sched_yield () .
.
,
, sched_yield ().
, , yi eld () ,
, TASK_RUNNING, sched_yield ().
sched_yield ().
,
( , )
. , ,
. , ,
,
, ,
. , Linux
,
,
.
, , (
) ,
, , , .
. .
NUMA (
) ,
NUMA . (schedu
ler domain) , ;
2.6 .
,
Linux. ,
, .
-
7/26/2019 . Linux 2006
96/451
-
7/26/2019 . Linux 2006
97/451
5
, , ,
. . , , ( ). , , , , , , .
, . . , . , , , , , . , . , . ,
, , . , , 3, " ".
, . Linux ,
; . , /, .
-
7/26/2019 . Linux 2006
98/451
, Linux , 1.
Linux.
A P I, P O S IX
, (Application Programing Interface, API). , , , , .
API , . , , . , , API .
Unix POSIX. POSIX I 2, , Unix. Linux POSIX.
POSIX API . U nix
API, POSIX, . , POSIX , , Unix, . , , OSUnix, Windows NT, , POSIX.
Linux, U nix , .
Unix, . , , , , , .
1 x8 6 25 0 ( ) . , .
2I E EE , e y e tr p le E ( , I n s t i t u te o f E l e ct r i ca l and E l e ct r o ni cs E ng i ne e r s ) , , P OS IX. : h t t p : / / w w n . i e e e . o r g .
-
7/26/2019 . Linux 2006
99/451
pr in tf ()>
pr i r t f ( ) > wr ite() wr ite()
> >
. 5.1. ,
p r i n t f ( )
API
POSIX.
, :
, , API. ,
: ,
,
.
,
.
Unix " ,
". , ,
. ,
, .
s y s c a l l ( syscall Linux)
.
inputs), 3,
,
.
long4,
. , ,
, .
( ) .
Unix
errno.
y perror ().
, , . ,
getpid () ,
, PID .
.
3 "".
(.. ), , , , getpid (),
.
4 long 64 .
-
7/26/2019 . Linux 2006
100/451
asmlinkage long sys_getpid(void)
{
return current>tgid;
)
, .
,
, ,
. ,
, ,
( )5.
,
. ,
asmlinkage .
,
.
. , , getpid () , sys_getpid ().
Linux:
bar () sys_bar ().
Linux
(syscall number).
.
, . .
,
. ,
. Linux
" " ("not implemented")
sys_ni _syscall (), , ,
, ENOSYS, ,
. " "
, .
. ,
sys_call_table.
entry.S.
syscal l .
5 , , ge t pi d () tgid,
(thread group ID)? ,
TGID PID.
TGID .
getpid ()
PID.
-
7/26/2019 . Linux 2006
101/451
Linux ,
.
.
.
, .
.
, ,
.
,
".
,
,
, .
, ,
: (exception)
.
(system call handler). 8
i nt $0x80.
128,
.
ystem_call ().
entry.S6. , sysent
r.
,
i nt . .
, ,
, ,
, .
,
,
.
.
6
x86. ,
.
-
7/26/2019 . Linux 2006
102/451
86 , . . .
system_call() NR_syscalls.
NR_syscalls, ENOSYS. :
call *sys_call_table(,%eax,4)
32 (4 ), 4 (. 5.2).
read()
read()
read ()
system_call()
sys_read()
sys_read()
. 5.2.
, .
. : . 86 ebx, ecx, edx, esi, edi
. , , , .
. 86 .
-
7/26/2019 . Linux 2006
103/451
Linux
.
inux .
. .
, ,
Linux.
,
. .
. ( ,
, ,
) Linux
. , ,
ioctl().
, ?
, .
, ,
.
.
?
. ,
, .
, .
?
. 19, "",
. ,
. Unix:
, ".
,
, .
Unix .
, !
,
, .
,
, .
,
, . ,
, ,
PID . ,
.
-
7/26/2019 . Linux 2006
104/451
,
. , , ,
, !
, ,
, . ,
, .
. ,
.
. ,
.
.
. ,
.
. ,
!
.
copy_to_user ().
:
; ; ,
, .
copy_from_user (),
copy_to_user (). ,
, ,
, .
,
. .
EFAULT.
,
copy_from_user () copy_to_user () . s i l ly_copy ()
.
. , .
.
/*
* silly copy ,
* len ,
* src, ,
* dst,
* . !
*/
asmlinkage long sys_silly_copy(unsigned long *src,
unsigned long *dst, unsigned long len)
}
-
7/26/2019 . Linux 2006
105/451
unsigned long buf;
/* ,
, */
if (len != sizeof(buf))
return EINVAL;
/* src,
, buf */
if (copy_from_user (&buf, src, len))
return EFAULT;
/* buf dst,
*/
if (copy_to_user (dst, &buf, len) )
return EFAULT;
/* */
return len;
}
, , copy_from_user () copy_to_user (), . , , ,
, ,
.
, (page fault handler)
.
.
Linux suser ()
, root.
, root.
" " (capabilities).
. capable ()
, ,
, , .
, capable (CAP_SYS_NICE) ,
nice .
, ,
root, .
,
, .
asmlinkage long sys_am_i_popular (void)
{
/* ,
CAP_SYS_NICE */
if (!capable(CAP_SYS_NICE))
return EPERM;
/* , */
return 0;
}
" " , , .
-
7/26/2019 . Linux 2006
106/451
3, " ",
. current , , .
(, schedule ()), . . ,
. 6, " ", 7. , , , , , .
, , . , . , , 8, " ", 9," ".
system_call (), , .
,
.
. , ( ). , . , .
i n c l u d e / l i n u x / u n i s t d . h .
( 8). kernel/.
7 , , , .
8 . , , . . .
-
7/26/2019 . Linux 2006
107/451
, f (). sys_f ()
.
entry.S .
ENTRY(sys_call_table)
.long sys_restart_syscall /* 0 */
.long sys_exit
.long sys_fork
.long sys_read
.long sys_write
.long sys_open /* 5 */
...
.long sys_timer_delete
.long sys_clock_settime
.long sys_clock_gettime /* 280 */
.long sys_clock_getres
.long sys_clock_nanosleep
:
.long sys_foo
, 283,
. ,
,
(
).
.
, ,
.
nclude/asm/unistd.h, .
/*
* This file contains the system call numbers.
*/
#define __NR_restart_syscall 0
#define __NR_exit 1
#define __NR_fork 2
#define __NR_read 3
#define __HR_write 4
#define __NR_open 5
...
#define __NR_mq_unlink 278
#define __NR_mq_timedsend 279
#define __NR_mq_timedreceive 280
#define __NR_mq_notify 281#define __NR_mq_getsetattr 282
-
7/26/2019 . Linux 2006
108/451
.
#define __NR_foo 283
f ().
,
k ernel/sys.. . ,
, sched..
/*
* sys_foo .*
*
*/
asmlinkage long sys_foo(void)
{
return THREAD_SIZE;
}
! .
foo ().
.
( ,
).
, , gl ibc !
, Linux
.
i n t 50x80. sysca l l n (),
. ,
, ,
, ,
. , open (),
.
long open(const char "filename, int flags, int model
.
#define NR_open 5
_syscall3(long, NR_open, const char *, filename, int, flags, int, mode)
open (). 2 + 2*n .
.
.
-
7/26/2019 . Linux 2006
109/451
, . NR_open, , .
, .
, , . , open ().
, , , .
#define NR_foo 283
__syscallO(long, foo)
int main ()
{
long stack_size;
stack_size = foo () ;
printf (" %ld\n", stack_size);
return 0;
}
,
, . ,
, .
"" "" .
"".
.
Linux
.
"".
,
.
,
" ". ,
.
.
" ".
-
7/26/2019 . Linux 2006
110/451
.
r e a d () w r i t e () , i o c t l ( ) .
, , . .
sysfs.
, . Linux , . , (deprecated) (.. , ).
, Linux . 2.3 2.5. .
,
(API). , Linux, : , , .
, , . ! , . , , , .
"" "" .
-
7/26/2019 . Linux 2006
111/451
6
, , .
. , , , , . , , .
polling). . , , , , . , . (interrupt).
. , ( , ) , , . , . , .
, . , , .
-
7/26/2019 . Linux 2006
112/451
, . .
, . ,
.
, . , , , , . , . .
, ,
(interrupt request lines, IRQ lines). . , PC IRQ, 0, , a IRQ, 1, .
. , PC I, , . , PCI, . , , . , , "! ! !.
(exceptions) . , . , . ( , ) , (, , page fault). , , , .
, (, ), (, ).
. , 86 . ,
. , , , .
-
7/26/2019 . Linux 2006
113/451
, ,
(interrupt handler)
(interrupt service routine). , ,
. , , , . , .
Linux , . , , . , , , ,
( interrupt context), .
, , , . , . , , , , . , ,
, . . , . , , , . , .
, , , , , . , , . (top half) , , . , , ( ) (bottom half). , , .
.
-
7/26/2019 . Linux 2006
114/451
, 7, "
".
. , ,
. , . :", ! !.
.
, , , . , .
. , .
,
. , ( ), .
.
/* request_irq: */
int request_irq(unsigned int irq,
irqreturn_t (*handler)(int, void *, struct pt_regs *),
unsigned long irqflags,
const char * devname,
void *dev_id);
, irq, .
, , , , , , , . (probing)
. , h a n d l e r ,
, . , . . irqreturn_t. .
, irqflags, .
-
7/26/2019 . Linux 2006
115/451
SA_INTERRUPT. , . , Linux ., , , , .
: . , . ( ) , , . , , .
SA_SAMPLE_RANDOM. , , , .
. , , , . , (, , ) (, , ). , . .. , " ".
SA_SHIRQ. , (shared). , ,
. . .
, devname, ASCII, , . , "keyboard". /proc/irq /proc/interrupts, .
, d e v i d , , . ( ), dev_id (cookie), . , . , NULL,
, (cookie) ( ISA,, , ).
-
7/26/2019 . Linux 2006
116/451
. (
), , , , .
r eq uest_ i rq () . ,
. EBUSY, ,
( , SA_SHIRQ).
, r e q u e s t _ i r q () (sleep) , , , , . , request_irq() , .
, , request_i rq() . . /proc/irq. proc_mkdir () procfs. proc_create () pro c f s , kmalloc () . 11, " ", kmalloc () . !
.
if (request_irq(irqn, my_interrupt, SA_SHIRQ, "my_device", dev)){
printk(KERN_ERR "my_device: cannot register IRQ %d\n", irqn);
return EIO;
}
irqn , my_interrupt , , "my_device", dev dev_id. , , . , . . , , .
void free_irq(unsigned int irq, void *dev_id)
-
7/26/2019 . Linux 2006
117/451
, .
, , dev_id. , . , dev_id.
, , , f ree_irq() . , devoid NULL, , .
free_irq() .
6. 1 .
r e q u e s t _ i r q ()
f r e e _ i r q () . ,
.static irqreturn_t intr_handler (int irq, void *dev_id, struct pt_regs *regs)
, , request_irq (). , irq, , . , . , 2.0, dev_id, i rq , , , (
). , dev_id, , ,
request_irq () . , , , , . , () (device structure) , , , , dev_id .
, regs, , , . ,
-
7/26/2019 . Linux 2006
118/451
. , . , , .
irqreturn_t. : IRQ_NONE IRQ_HANDLED. , , , , . ,
, , . , IRQ_RETVAL (x). , IRQ_HANDLED, , IRQ_NONE. , () . , , IRQ_NONE, . , , i r q r e t u r n _ t , int. ,
, . 2.6 void.
typedef i r q r e t u
top related