Unix API 2 — shells / fjle descriptors
1
Unix API 2 shells / fjle descriptors 1 last time context switch - - PowerPoint PPT Presentation
Unix API 2 shells / fjle descriptors 1 last time context switch in xv6 (fjnish) POSIX standard source compatibility fork copy current process return value in copy (child) is 0 return value in original (parent) is
1
2
also posix_spawn (not widely supported), …
3
4
5
6
7
8
9
10
11
12
13
13
14
14
14
14
14
14
15
15
15
15
15
16
17
18
19
20
21
22
23
24
24
24
24
24
25
25
read/write, read-only, write-only
append to end of fjle
truncate (set length to 0) fjle if it already exists
create a new fjle if one doesn’t exist (default: fjle must already exist)
26
27
28
29
eax=42init. val., ecx=133init. val., …
fd 0: (terminal …) fd 1: …
…
30
eax=42init. val., ecx=133init. val., …
fd 0: (terminal …) fd 1: …
…
30
eax=42init. val., ecx=133init. val., …
fd 0: (terminal …) fd 1: …
…
30
eax=42init. val., ecx=133init. val., …
fd 0: (terminal …) fd 1: …
…
30
eax=42child (new) pid, ecx=133, …
fd 0: … fd 1: …
…
eax=420, ecx=133, …
fd 0: … fd 1: …
…
31
pid = fork(); if (pid == 0) {
exec…(…); … } else if (pid > 0) { waitpid(pid,…); … } … pid = fork(); if (pid == 0) {
exec…(…); … } else if (pid > 0) { waitpid(pid,…); … } …
pid = fork(); if (pid == 0) {
exec…(…); … } else if (pid > 0) { waitpid(pid,…); … } …
main() { … }
32
33
34
35
36
37
38
39
40
41
42
43
44
44
45
46
47
48
49
50
51
52
53
53
53
53
54
55
pid_t p = fork(); int pipe_fds[2]; pipe(pipe_fds); if (p == 0) { /* child */ close(pipe_fds[0]); char c = 'A'; write(pipe_fds[1], &c, 1); exit(); } else { /* parent */ close(pipe_fds[1]); char c; int count = read(pipe_fds[0], &c, 1); printf("read %d bytes\n", count); }
56
57
int pipe_fds[2]; pipe(pipe_fds); pid_t p = fork(); if (p == 0) { close(pipe_fds[0]); for (int i = 0; i < 10; ++i) { char c = '0' + i; write(pipe_fds[1], &c, 1); } exit(); } close(pipe_fds[1]); char buffer[10]; ssize_t count = read(pipe_fds[0], buffer, 10); for (int i = 0; i < count; ++i) { printf("%c", buffer[i]); }
58
59
60
61
also posix_spawn (not widely supported), …
62
63
63
64
65
66
66
67
68
69
70
71
72
pid = fork(); if (pid == 0) { exec…(…); … } else if (pid > 0) { waitpid(pid,…); … } … pid = fork(); if (pid == 0) { exec…(…); … } else if (pid > 0) { waitpid(pid,…); … } … pid = fork(); if (pid == 0) { exec…(…); … } else if (pid > 0) { waitpid(pid,…); … } … main() { … }
73
74
75
also posix_spawn (not widely supported), …
76