1 / 10

Chapter 5 Interlude: Process API

Chapter 5 Interlude: Process API. Yeong Jae Woo (yjwoo@archi.snu.ac.kr) School of Computer Science and Engineering Seoul National University. Outline. The fork() system call The wait() system call The exec() system call Why? Motivating the API Other parts of the API Summary.

rling
Download Presentation

Chapter 5 Interlude: Process API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5Interlude: Process API Yeong Jae Woo (yjwoo@archi.snu.ac.kr) School of Computer Science and Engineering Seoul National University

  2. Outline The fork() system call The wait() system call The exec() system call Why? Motivating the API Other parts of the API Summary

  3. The fork()system call 1 main() 2 { 3 int child_pid = fork(); 4 if (0 == child_pid) { 5 printf(“[pid: %d] child\n”, getpid()); 6 } else { 7 printf(“[pid: %d] parent of %d\n”,8 getpid(), child_pid);9 } 10 } New process Child Parent Process 1 main() 2 { 3 int child_pid = fork(); 4 if (0 == child_pid) { 5 printf(“[pid: %d] child\n”, getpid()); 6 } else { 7 printf(“[pid: %d] parent of %d\n”,8 getpid(), child_pid);9 } 10 } 1 main() 2 { 3 int child_pid = fork(); 4 if (0 == child_pid) { 5 printf(“[pid: %d] child\n”, getpid()); 6 } else { 7 printf(“[pid: %d] parent of %d\n”,8 getpid(), child_pid);9 } 10 } [pid: 29147] child [pid: 29146] parent of 29147 • Creates a new process • By duplicating the calling process • Returns the PID of the child process • PID is a unique number that identifies each of the running processes

  4. The fork()system call 1 main() 2 { 3 int child_pid = fork(); 4 if (0 == child_pid) { 5 printf(“[pid: %d] child\n”, getpid()); 6 } else { 7 printf(“[pid: %d] parent of %d\n”,8 getpid(), child_pid);9 } 10 } Parent Child 1 main() 2 { 3 int child_pid = fork(); 4 if (0 == child_pid) { 5 printf(“[pid: %d] child\n”, getpid()); 6 } else { 7 printf(“[pid: %d] parent of %d\n”,8 getpid(), child_pid);9 } 10 } 1 main() 2 { 3 int child_pid = fork(); 4 if (0 == child_pid) { 5 printf(“[pid: %d] child\n”, getpid()); 6 } else { 7 printf(“[pid: %d] parent of %d\n”,8 getpid(), child_pid);9 } 10 } [pid: 29146] parent of 29147 [pid: 29147] child • Now, there are two processes: the parent and the child • CPU scheduler determines which process runs at a given moment in the time • We don’t know which process will run first • What if a parent wants to wait for a child to terminate?

  5. The wait() system call 1 main() 2 { 3intchild_pid = fork(); 4 if (0 == child_pid) { 5printf(“[pid: %d] child\n”, getpid()); 6 } else { 7intwc = wait(NULL); 8printf(“[pid: %d] parent of %d\n”,9getpid(), child_pid);10 } 11 } yawn Child Parent 1 main() 2 { 3 int child_pid = fork(); 4 if (0 == child_pid) { 5 printf(“[pid: %d] child\n”, getpid()); 6 } else { 7int wc = wait(NULL); 8 printf(“[pid: %d] parent of %d\n”,9 getpid(), child_pid);10 } 11 } 1 main() 2 { 3 int child_pid = fork(); 4 if (0 == child_pid) { 5 printf(“[pid: %d] child\n”, getpid()); 6 } else { 7 int wc = wait(NULL); 8 printf(“[pid: %d] parent of %d\n”,9 getpid(), child_pid);10 } 11 } [pid: 29147] child [pid: 29146] parent of 29147 • Waits for a child to finish what it has been doing • When the child is done, wait() returns to the parent • What if we want to run a different program?

  6. The exec() system call High address 1 main() 2 { 3 int child_pid = fork(); 4 if (0 == child_pid) { 5 char *args[3]; 6 args[0] = strdup(“B”); 7 args[1] = strdup(“in”); 8 args[2] = NULL; 9 execvp(args[0], args); 10 } else { 11 int wc = wait(NULL);12 } 13 } Low address Storage A A B B • Replaces the current running program with a new running program • Loads code and static data from the executable • Overwrites to its current code segment and current static data • Re-initializes the heap and stack

  7. Why? Motivating the API Sources: http://direct.sanwa.co.jp/ItemPage/KRS-102K http://79.160.12.86/599N105E/hwdocs/serial/serial01.html http://cosam.org/computers/dec/pdp11-23/index.htmlhttp://www.cosam.org/images/pdp11-23/ http://vt100.net/vt_history (Retrieved on 2015/04/22)

  8. Why? Motivating the API fork() PID 1 exec() init exit() PID 2 PID 4 PID 3 init init init PID 2 PID 3 PID 4 getty getty getty PID 3 login PID 3 /bin/sh PID 20 PID 21 PID 22 PID 23 /bin/sh /bin/sh /bin/sh /bin/sh PID 23 PID 21 PID 20 PID 22 ls cat a.c gcca.c ./a.out Sources: http://www.cosam.org/images/pdp11-23/ http://vt100.net/vt_history (Retrieved on 2015/04/22)

  9. Other parts of the API • The kill() system call • Sends signals to a process • Command-line tools • ps command shows which processes are running • top command displays how much CPU and other resources running processes are eating up

  10. Summary • We have introduced some APIs dealing with process creation • The fork() system call • The exec() system call • The wait() system call • For more information… • Read the man pages

More Related