Download presentation
Presentation is loading. Please wait.
Published byEustacia Thornton Modified over 8 years ago
2
שיתוף PDT בין חוטים PDT Thread A Process Descriptor File Object 1 File Object 2 File 1 File 2 pthread_create Thread B Process Descriptor ה PDT משותף לכל החוטים באותו התהליך לפי סטנדרט pthreads.
3
שיתוף PDT בין תהליך אבא לתהליך בן 46 PDT Parent Process Descriptor File Object 1 File Object 2 File 1 File 2 fork() 45 Child Process Descriptor 46 PDT 45 ה PDT מועתק מהאב לבן, אבל ה file objects לא מועתקים – הם משותפים. כמובן שאם Child פותח קובץ חדש – זה לא משפיע על Parent.
4
pipe- תוכנית דוגמא int main() { int my_pipe[2]; int status; char father_buff[6]; pipe(my_pipe); status = fork(); 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1
5
pipe- תוכנית דוגמא int main() { int my_pipe[2]; int status; char father_buff[6]; pipe(my_pipe); status = fork(); 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 File Object pipe in File Object pipe out
6
pipe- תוכנית דוגמא int main() { int my_pipe[2]; int status; char father_buff[6]; pipe(my_pipe); status = fork(); 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 0 1 45 46 PDT Child Process Descriptor … File Object pipe in File Object pipe out
7
pipe- תוכנית דוגמא 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 0 1 45 46 PDT Child Process Descriptor … /* son process */ if (status == 0) { close(my_pipe[0]); write(my_pipe[1],“BAD",4*sizeof(char)); exit(0); } File Object pipe in File Object pipe out
8
pipe- תוכנית דוגמא 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 0 1 45 46 PDT Child Process Descriptor … File Object pipe in File Object pipe out /* son process */ if (status == 0) { close(my_pipe[0]); write(my_pipe[1],“BAD",4*sizeof(char)); exit(0); }
9
pipe- תוכנית דוגמא 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 0 1 45 46 PDT Child Process Descriptor … File Object pipe in File Object pipe out /* son process */ if (status == 0) { close(my_pipe[0]); write(my_pipe[1],“BAD",4*sizeof(char)); exit(0); }
10
pipe- תוכנית דוגמא 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 0 1 45 46 PDT Child Process Descriptor … File Object pipe in File Object pipe out /* son process */ if (status == 0) { close(my_pipe[0]); write(my_pipe[1],“BAD",4*sizeof(char)); exit(0); }
11
pipe- תוכנית דוגמא 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 File Object pipe in File Object pipe out /* son process */ if (status == 0) { close(my_pipe[0]); write(my_pipe[1],“BAD",4*sizeof(char)); exit(0); }
12
pipe- תוכנית דוגמא 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 File Object pipe in File Object pipe out else /* father process */ { close(my_pipe[1]); wait(&status);/* wait until son process finishes */ read(my_pipe[0], father_buff,4*sizeof(char)); printf(“Who’s %s\n", father_buff); exit(0); }
13
pipe- תוכנית דוגמא 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 File Object pipe in מוחקים את ה file object כי אין יותר מצביעים אליו else /* father process */ { close(my_pipe[1]); wait(&status);/* wait until son process finishes */ read(my_pipe[0], father_buff,4*sizeof(char)); printf(“Who’s %s\n", father_buff); exit(0); }
14
pipe- תוכנית דוגמא 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 File Object pipe in לא מובטח שהבן רץ קודם ולכן מחכים שהבן יסיים לכתוב ל pipe. else /* father process */ { close(my_pipe[1]); wait(&status);/* wait until son process finishes */ read(my_pipe[0], father_buff,4*sizeof(char)); printf(“Who’s %s\n", father_buff); exit(0); }
15
pipe- תוכנית דוגמא 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 File Object pipe in else /* father process */ { close(my_pipe[1]); wait(&status);/* wait until son process finishes */ read(my_pipe[0], father_buff,4*sizeof(char)); printf(“Who’s %s\n", father_buff); exit(0); }
16
pipe- תוכנית דוגמא 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 File Object pipe in else /* father process */ { close(my_pipe[1]); wait(&status);/* wait until son process finishes */ read(my_pipe[0], father_buff,4*sizeof(char)); printf(“Who’s %s\n", father_buff); exit(0); }
17
pipe- תוכנית דוגמא 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 File Object pipe in else /* father process */ { close(my_pipe[1]); wait(&status);/* wait until son process finishes */ read(my_pipe[0], father_buff,4*sizeof(char)); printf(“Who’s %s\n", father_buff); exit(0); } מה מודפס על המסך ?
18
pipe- תוכנית דוגמא 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 File Object pipe in else /* father process */ { close(my_pipe[1]); wait(&status);/* wait until son process finishes */ read(my_pipe[0], father_buff,4*sizeof(char)); printf(“Who’s %s\n", father_buff); exit(0); } לאו דווקא Who’s BAD. כי לא מובטח ש write כתבה את כל התווים או ש read קראה את כולם...
19
הכוונת קלט פלט 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 status = fork(); if (status == 0) { close(1); fd = open(“myfile”, O_WRONLY…); execv(“/bin/ls”, …); {
20
הכוונת קלט פלט 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 status = fork(); if (status == 0) { close(1); fd = open(“myfile”, O_WRONLY…); execv(“/bin/ls”, …); { 0 1 45 46 PDT Child Process Descriptor …
21
הכוונת קלט פלט 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 status = fork(); if (status == 0) { close(1); fd = open(“myfile”, O_WRONLY…); execv(“/bin/ls”, …); { 0 1 45 46 PDT Child Process Descriptor …
22
הכוונת קלט פלט 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 status = fork(); if (status == 0) { close(1); fd = open(“myfile”, O_WRONLY…); execv(“/bin/ls”, …); { 0 1 45 46 PDT Child Process Descriptor …
23
הכוונת קלט פלט 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 status = fork(); if (status == 0) { close(1); fd = open(“myfile”, O_WRONLY…); execv(“/bin/ls”, …); { 0 1 45 46 PDT Child Process Descriptor …
24
הכוונת קלט פלט 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 status = fork(); if (status == 0) { close(1); fd = open(“myfile”, O_WRONLY…); execv(“/bin/ls”, …); { 0 1 45 46 PDT Child Process Descriptor … Myfile File Object
25
הכוונת קלט פלט 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 status = fork(); if (status == 0) { close(1); fd = open(“myfile”, O_WRONLY…); execv(“/bin/ls”, …); { 0 1 45 46 PDT Child Process Descriptor … Myfile File Object
26
הכוונת קלט פלט 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 status = fork(); if (status == 0) { close(1); fd = open(“myfile”, O_WRONLY…); execv(“/bin/ls”, …); { 0 1 45 46 PDT Child Process Descriptor … Myfile File Object
27
הכוונת קלט פלט 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 status = fork(); if (status == 0) { close(1); fd = open(“myfile”, O_WRONLY…); execv(“/bin/ls”, …); { 0 1 45 46 PDT … Myfile File Object execv אינה מחליפה את ה PDT של התהליך הקורא !
28
הכוונת קלט פלט 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 status = fork(); if (status == 0) { close(1); fd = open(“myfile”, O_WRONLY…); execv(“/bin/ls”, …); { 0 1 45 46 PDT … Myfile File Object /bin/ls Process Descriptor
29
הכוונת קלט פלט 0 1 45 46 PDT Parent Process Descriptor … File Object 0 File Object 1 status = fork(); if (status == 0) { close(1); fd = open(“myfile”, O_WRONLY…); execv(“/bin/ls”, …); { 0 1 45 46 PDT … Myfile File Object /bin/ls Process Descriptor ועכשיו כל הפלט של ls ילך לקובץ
30
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); }
31
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT
32
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT Shell SON Process Descriptor …
33
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT Shell SON Process Descriptor …
34
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT Shell SON Process Descriptor …
35
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT Shell SON Process Descriptor …
36
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT Shell SON Process Descriptor …
37
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT Shell SON Process Descriptor …
38
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT Shell SON Process Descriptor …
39
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT Shell SON Process Descriptor …
40
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT Shell SON Process Descriptor 0 1 45 46 PDT …
41
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT …
42
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor
43
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 int fd[2]; pipe(fd); status = fork(); if (status == 0) { /* first child */ close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execv(“/bin/ls”,…); } Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor /bin/ls OUTPUT
44
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT
45
הכוונת קלט פלט 2 0 1 45 46 PDT Shell Process Descriptor … File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT Shell SON 2 Process Descriptor …
46
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT Shell SON 2 Process Descriptor … נוציא את ה shell מהתמונה כי קשה לראות ככה משהו
47
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT Shell SON 2 Process Descriptor …
48
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT Shell SON 2 Process Descriptor …
49
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT Shell SON 2 Process Descriptor …
50
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT Shell SON 2 Process Descriptor …
51
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT Shell SON 2 Process Descriptor …
52
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT Shell SON 2 Process Descriptor …
53
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT Shell SON 2 Process Descriptor …
54
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT Shell SON 2 Process Descriptor …
55
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT Shell SON 2 Process Descriptor …
56
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT Shell SON 2 Process Descriptor …
57
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT …
58
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT … /bin/more Process Descriptor
59
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT … /bin/more Process Descriptor כעת more יכול לקרוא את מה ש less כתב לו ב pipe
60
הכוונת קלט פלט 2 File Object 0 File Object 1 Pipe File Object IN Pipe File Object OUT 0 1 45 46 PDT … /bin/ls Process Descriptor status = fork(); if (status == 0) { /* second child */ close(0); dup(fd[0]); close(fd[0]); close(fd[1]); execv(“/bin/more”,..); } close(fd[0]); close(fd[1]); /bin/ls OUTPUT 0 1 45 46 PDT … /bin/more Process Descriptor שתי הפקודות האחרונות המסומנות – הן של ה shell שסוגר את ה pipe כי הוא לא ישתמש בו
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.