Download presentation
Presentation is loading. Please wait.
Published byGerard Matthews Modified over 9 years ago
1
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads
2
Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 The Multi-Process Astronomy Server Consider the following problem: A server is made up of one “input process” and multiple “worker processes”. The input process performs a loop that reads multiple file names from disk. Each named file contains a very large scientific data set (with observation data on all stars in some galaxy). The process passes each file to a different newly forked child that opens the named file and performs some math on the data (to compute the trajectories of the observed stars). The results are written by the child to a results file for that galaxy. The child then exists with a code that indicates success or failure. The parent (input process) waits for the children. Each time a child exists, the parent checks the exit code and restarts the child if the code indicates that an error has occurred. What do you need to know to write this up?
3
Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 Waiting for Children to Return The wait() Function wait function allows parent process to wait (block) until child finishes then returns the id and status of that child wait function causes the caller to suspend execution until child’s status is available waitpid function allows a parent to wait for a particular child
4
Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Waiting for Children #include pid_t childpid; … childpid = wait(NULL); if (childpid != -1) printf(“waited for child with pid %ld\n”, childpid);
5
Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 Dead Children may become Zombies When a process terminates but its parent does not wait for it? Zombie (pid entry remains in system process table) Zombies unlike orphans do not consume many resources.
6
Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Zombie Removal Professional code installs signal handler for signal SIGCHLD … which issues a wait() call
7
Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 Orphans When the parent process terminates first? “Child is orphaned” Child is “re-parented” to init process Child continues to consume resources init process (id=1) waits for children
8
Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 Status Values for wait(int *stat_loc) The following macros are available for checking the return status of a child: #include WIFEXITED(int stat_val) WEXITSTATUS(int stat_val) WIFSIGNALED(int stat_val) WTERMSIG(int stat_val) WIFSTOPPED(int stat_val) WSTOPSIG(int stat_val) They are used in pairs. If WIFEXITED returns true, the child executed normally and the return status (at most 8 bits) can be gotten with WEXITSTATUS.
9
Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 Simple Example int status; childpid = wait(&status); if (childpid == -1) perror("Failed to wait for child"); else if (WIFEXITED(status) && 0==WEXITSTATUS(status)) printf(“Child terminated normally”);
10
Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Typical Child Creation Code #include int main (void) { pid_t childpid; /* set up signal handlers here... */ childpid = fork(); if (childpid == -1) { perror("Failed to fork");return 1;} if (childpid == 0) fprintf(stderr, "I am child %ld\n", (long)getpid()); else if (wait(NULL) != childpid) fprintf(stderr, "A signal must have interrupted the wait!\n"); else fprintf(stderr, "I am parent %ld with child %ld\n", (long)getpid(), (long)childpid); return 0; }
11
Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 exec() Function Exec function allows child process to execute code that is different from that of parent Exec family of functions provides a facility for overlaying the process image of the calling process with a new image. Exec functions return -1 and sets errno if unsuccessful
12
Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 Exec Example #include int main (void) { pid_t childpid; childpid = fork(); if (childpid == -1) { perror("Failed to fork"); return 1; } if (childpid == 0) { /*child code*/ execl(“/bin/ls”, “ls”, “-l”, NULL); perror(“Child failed to exec ls”); return 1; } if (wait(NULL) != childpid) { /* parent code */ perror(“Parent failed to wait due to signal or error”); return 1; } return 0; }
13
Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 Background Processes and Daemons Shell – command interpreter creates background processes if line ends with & When shell creates a background process, it does not wait for the process to complete before issuing a prompt and accepting another command Ctrl-C does not terminate background process Daemon is a background process that normally runs indefinitely
14
Copyright ©: Nahrstedt, Angrave, Abdelzaher 14 Processes and Threads
15
Copyright ©: Nahrstedt, Angrave, Abdelzaher 15 Property Processes created with fork Threads of a processOrdinary function calls variables get copies of all variables share global variables IDsget new process IDs share the same process ID but have unique thread ID share the same process ID (and thread ID) Communicatio n Must explicitly communicate, e.g.pipes or use small integer return value May communicate with return value or shared variables if done carefully May communicate with return value or shared variables (don't have to be careful) Parallelism (one CPU) Concurrent Sequential Parallelism (multiple CPUs) May be executed simultaneously Kernel threads may be executed simultaneously Sequential Threads versus Processes
16
Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 Thread Components address space: code and global variables open files semaphores signals timers process ID A thread has its own program counter and stack, but shares a number of resources with its process and other threads of the process:
17
Copyright ©: Nahrstedt, Angrave, Abdelzaher 17 Thread Components
18
Copyright ©: Nahrstedt, Angrave, Abdelzaher 18 Creating a Thread When a new thread is created it runs concurrently with the creating process. When creating a thread you indicate which function the thread should execute.
19
Copyright ©: Nahrstedt, Angrave, Abdelzaher 19 Normal function call
20
Copyright ©: Nahrstedt, Angrave, Abdelzaher 20 Threaded function call
21
Copyright ©: Nahrstedt, Angrave, Abdelzaher 21 Pthread Operations POSIX functiondescription pthread_creat e create a thread pthread_detac h set thread to release resources pthread_equal test two thread IDs for equality pthread_exit exit a thread without exiting process pthread_kill send a signal to a thread pthread_join wait for a thread pthread_self find out own thread ID
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.