process management i
play

Process Management I CS 351: Systems Programming Michael Saelee - PowerPoint PPT Presentation

Process Management I CS 351: Systems Programming Michael Saelee <lee@iit.edu> 1 Computer Science Science Creating Processes 2 Computer Science Science #include <unistd.h> pid_t fork (); 3 Computer Science Science fork


  1. Process Management I CS 351: Systems Programming Michael Saelee <lee@iit.edu> 1

  2. Computer Science Science §Creating Processes 2

  3. Computer Science Science #include <unistd.h> pid_t fork (); 3

  4. Computer Science Science fork traps to OS to create a new process … which is (mostly) a duplicate of the calling process! 4

  5. Computer Science Science e.g., the new (child) process runs the same program as the creating (parent) process - and starts with the same PC, - the same SP , FP , regs, - the same open files, etc., etc. 5

  6. Computer Science Science P parent int main () { fork(); foo(); } OS 6

  7. Computer Science Science P parent int main () { fork(); foo(); } OS 7

  8. Computer Science Science P parent P child int main () { int main () { fork(); fork(); foo(); foo(); } } creates OS 8

  9. Computer Science Science P parent P child int main () { int main () { fork(); fork(); foo(); foo(); } } OS 9

  10. Computer Science Science fork , when called, returns twice (to each process @ the next instruction) 10

  11. Computer Science Science int main () { fork(); printf("Hello world!\n"); } Hello world! Hello world! 11

  12. Computer Science Science int main () { fork(); fork(); printf("Hello world!\n"); } Hello world! Hello world! Hello world! Hello world! 12

  13. Computer Science Science int main () { fork(); fork(); fork(); printf("Hello world!\n"); } Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! 13

  14. Computer Science Science int main() { while(1) fork(); } the “fork bomb” (I didn’t show you this) 14

  15. Computer Science Science # processes waiting to be scheduled 15

  16. Computer Science Science typedef int pid_t; pid_t fork(); - system-wide unique process identifier - child’s pid (> 0 ) is returned in the parent - sentinel value 0 is returned in the child 16

  17. Computer Science Science void fork0() { int pid = fork(); if (pid == 0) printf("Hello from Child!\n"); else printf("Hello from Parent!\n"); } main() { fork0(); } Hello from Child! Hello from Parent! (or) Hello from Parent! Hello from Child! 17

  18. Computer Science Science i.e., order of execution is nondeterministic - parent & child run concurrently ! 18

  19. Computer Science Science void fork1 () { int x = 1; if (fork() == 0) { printf("Child has x = %d\n", ++x); } else { printf("Parent has x = %d\n", --x); } } Parent has x = 0 Child has x = 2 19

  20. Computer Science Science important: post-fork, parent & child are identical, but separate ! - OS allocates and maintains separate data/state - control flow can diverge 20

  21. Computer Science Science void fork2() { printf("L0\n"); fork(); printf("L1\n"); fork(); printf("Bye\n"); } L0 L1 L1 Bye Bye Bye Bye 21

  22. Computer Science Science void fork2() { printf("L0\n"); fork(); printf("L1\n"); fork(); printf("Bye\n"); } process tree: 22

  23. Computer Science Science void fork2() { printf("L0\n"); fork(); printf("L1\n"); fork(); printf("Bye\n"); } Which are possible? A. B. C. D. E. L1 L0 L0 L1 L0 L0 L1 L1 Bye Bye L1 Bye Bye Bye Bye Bye Bye Bye L0 L1 Bye L1 Bye L1 L1 Bye Bye L1 Bye Bye Bye Bye Bye Bye Bye 23

  24. Computer Science Science main() { fork(); fork(); while(1) ; } $ ./a.out & [1] 8198 $ pstree -p 8198 a.out(8198) ��� a.out(8199) ��� a.out(8201) �� a.out(8200) 24

  25. Computer Science Science void fork3() { printf("L0\n"); fork(); printf("L1\n"); fork(); printf("L2\n"); fork(); printf("Bye\n"); } 25

  26. Computer void fork4() { Science Science printf("L0\n"); if (fork() != 0) { printf("L1\n"); if (fork() != 0) { printf("L2\n"); fork(); } } printf("Bye\n"); } A. B. C. D. E. L0 L0 Bye L0 L0 L1 L1 L0 Bye L1 L2 Bye Bye L1 Bye Bye Bye L1 Bye Bye Bye L2 Bye L2 Bye Bye Bye L2 Bye L2 Bye Bye Bye Bye Bye 26

  27. Computer void fork4() { Science Science printf("L0\n"); if (fork() != 0) { printf("L1\n"); if (fork() != 0) { printf("L2\n"); fork(); } } printf("Bye\n"); } 27

  28. Computer void fork5() { Science Science printf("L0\n"); if (fork() == 0) { printf("L1\n"); if (fork() == 0) { printf("L2\n"); fork(); } } printf("Bye\n"); } 28

  29. Computer Science Science a good question: what if fork fails? 29

  30. Computer Science Science most syscalls return -1 on failure global var errno populated with “cause” 30

  31. Computer Science Science #include <errno.h> extern int errno; /* get error string */ char *strerror(int errnum); /* print error string w/ message */ void perror(const char *s); 31

  32. Computer Science Science int fd = open("/etc/shadow", O_RDONLY); if (fd == -1) { perror("Uh-oh"); exit(1); } $ ./errtest Uh-oh: Permission denied 32

  33. Computer Science Science $ man errno NAME errno - number of last error SYNOPSIS #include <errno.h> DESCRIPTION ... E2BIG Argument list too long (POSIX.1) EACCES Permission denied (POSIX.1) EADDRINUSE Address already in use (POSIX.1) EADDRNOTAVAIL Address not available (POSIX.1) EAFNOSUPPORT Address family not supported (POSIX.1) EAGAIN Resource temporarily unavailable (may be the same value as EWOULDBLOCK) (POSIX.1) EALREADY Connection already in progress (POSIX.1) EBADE Invalid exchange ... 33

  34. Computer Science Science §Terminating Processes 34

  35. Computer Science Science int main () { return 0 ; } 35

  36. Computer Science Science void exit (int status); 36

  37. Computer Science Science void foo() { exit(1); /* no return */ } int main () { foo(); /* no return */ return 0; } 37

  38. Computer Science Science Unix convention: - normal termination → exit status 0 - other exit status values = error 38

  39. Computer Science Science void foo() { exit(1); } int main () { ... foo(); /* $^@#%!! */ release(resource); /* cleanup */ return 0; } 39

  40. Computer Science Science int atexit (void (*fn)()); 40

  41. Computer Science Science int atexit (void (*fn)()); - registers function to call before exiting - can call multiple times; functions are invoked in reverse order 41

  42. Computer Science Science void cleanup() { printf("Cleaning up\n"); } void foo() { printf("Self-destructing\n"); exit(1); } int main() { atexit(cleanup); foo(); /* no return */ return 0; } Self-destructing Cleaning up 42

  43. Computer Science Science void cleanup() { printf("Cleaning up\n"); } void foo() { fork(); exit(1); } int main() { atexit(cleanup); foo(); /* no return */ return 0; } Cleaning up Cleaning up 43

  44. Computer Science Science i.e., atexit handlers are “inherited” by child processes on fork 44

  45. Computer Science Science void fork7() { if (fork() == 0) { printf("Terminating Child, PID = %d\n", getpid()); exit(0); } else { printf("Running Parent, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ } } (demo) 45

  46. Computer Science Science All terminating processes turn into zombies 46

  47. Computer Science Science “dead” but still tracked by OS - pid remains in use - exit status can be queried 47

  48. Computer Science Science §Reaping Processes (& Synchronization) 48

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend