Вы находитесь на странице: 1из 41

, , 16 2011

#yakit



UNIX *nix

strace ltrace
-

UNIX
1969 (AT&T/Bell Labs: , , )

- - (POSIX)
AIX, Solaris, Mac OS X/FreeBSD/NetBSD/OpenBSD
UNIX
Linux, Minix
POSIX Windows
*nix
6

*nix =

Portable Operating System Interface


[for Unix]
ISO C/C++ (STL)

Runtime
Shell
Java
Perl/Python/PHP/...

10

(, IPC, , )

11


?
ABI - syscalls

mov edx,len
mov
ecx,msg
mov
ebx,1
mov
eax,4
(sys_write)
int
0x80
mov eax,1
int

;file descriptor (stdout)


;system call number
;call kernel

;system call number (sys_exit)


0x80
;call kernel

section .data
msg db
len

12

'Hello, world!',0xa
equ $ - msg

13


?
ABI - syscalls
int main() {
__asm__(
"movl $20, %eax \n"
"call *%gs:0x10 \n"
"movl %eax, pid \n"
);
printf("pid is %d\n", pid);
return 0;
}
14

C?
ABI - syscalls
#include <unistd.h>
/* ... */
const char msg[] = "Hello world";
write( STDOUT_FILENO, msg, sizeof(
msg ) - 1 );
/* write
syscall? */
15


ltrace
strace

16

Shell: /bin/*sh
*nix
While (1) {
write (1, "$ ", 2);
readcmd (cmd, args);
// parse user input
if ((pid = fork ()) == 0) { // child?
exec (cmd, args, 0);
} else if (pid > 0) {
// parent?
wait (0);
// wait for child to terminate
} else {
perror ("fork");
}
}

17

:
: read, write, fork,
exec, wait
:
-1

errno
perror
errno

18

: fork
,

:
(, , )
: , ,
:
PID
fork
0
pid
19

: exec




(
pid, uid, )

20

: wait

?

wait?

21

fork/exec
?
ls ,
stdin, stdout?
cwd

Copy-on-write fork

22

: read/write
:


23

?
*nix, Windows NT

24

:
:

25

:
?
()

?
, ,



26


vmstat
cat /proc/1/status
ps

27


?
man ps
/STATE

28

:
: :
2^32

? .

29




90-
x86
CS, DS


4



copy-on-write fork
30


vmstat
cat /proc/swaps
swapon -s

31



( )

32

:
(Windows 3.1, Mac OS 9, WOW16)
(preemptive)

33

batch

BSD:

Android:

34


FIFO/FCFS
SJF (Shortest Job First)
()
Round-Robin


CFQ

35

(sharing)
()

()

36

-
:

-

37

-
:
(RSS)
FIFO/FCFS
LIFO
CFQ
SCAN ()
Noop (FIFO )

Anticipatory ()
Deadline (
38

...


LRU

...
39


Operation

Time (nsec)

L1 cache reference 0.5


Branch mispredict

L2 cache reference 7
Mutex lock/unlock 25
Main memory reference

100

Compress 1KB bytes with Zippy

3,000

Send 2K bytes over 1 Gbps network

20,000

Read 1MB sequentially from memory

250,000

Roundtrip within same datacenter 500,000

Disk seek

10,000,000

Read 1MB sequentially from disk 20,000,000


40

Send packet CA -> Netherlands -> CA

150,000,000


Operation

Time (nsec)

System call overhead 400


Context switch between processes

3000

fork() (statically-linked binary) 70,000

fork() (dynamically-linked binary) 160,000

41



119021, , ,
. , . 16.
+7 (495) 739-70-00
+7 (495) 739-70-70
vashik@yandex-team.ru

Оценить