Date post: | 13-Dec-2015 |
Category: |
Documents |
Upload: | gerard-matthews |
View: | 227 times |
Download: | 2 times |
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?
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
Copyright ©: Nahrstedt, Angrave, Abdelzaher
4
Waiting for Children
#include <errno.h>#include <sys/wait.h>
pid_t childpid;
…
childpid = wait(NULL);if (childpid != -1) printf(“waited for child with pid %ld\n”, childpid);
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
6
Zombie Removal
Professional code installs signal handler for signal SIGCHLD … which issues a wait() call
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
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 <sys/wait.h>
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.
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”);
Copyright ©: Nahrstedt, Angrave, Abdelzaher
10
Typical Child Creation Code#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>
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;}
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
Copyright ©: Nahrstedt, Angrave, Abdelzaher
12
Exec Example
#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>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;}
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
Copyright ©: Nahrstedt, Angrave, Abdelzaher
15
PropertyProcesses created with fork
Threads of a process Ordinary function calls
variablesget copies of all variables
share global variables share global variables
IDs get new process IDsshare the same process ID but have unique thread ID
share the same process ID (and thread ID)
Communication
Must explicitly communicate, e.g.pipesor use small integer return value
May communicate with return valueor shared variablesif done carefully
May communicate with return valueor shared variables(don't have to be careful)
Parallelism (one CPU)
Concurrent Concurrent Sequential
Parallelism (multiple CPUs)
May be executed simultaneously
Kernel threads may be executed simultaneously
Sequential
Threads versus Processes
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:
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
21
Pthread OperationsPOSIX function description
pthread_create
create a thread
pthread_detach
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