View
223
Download
1
Category
Preview:
Citation preview
Concurrent ProcessesConcurrent Processes
Processes can concurrently run same Processes can concurrently run same program.program.
Processes can start other processes.Processes can start other processes. UNIX provides concurrency functions UNIX provides concurrency functions
Creating New ProcessesCreating New Processes
system -- makes a shellsystem -- makes a shell
Exec -- replace this process with a new Exec -- replace this process with a new oneone
Fork -- start a concurrent process Fork -- start a concurrent process
Example Example ““SystemSystem””
int main(){ char buffer[80]; strcpy(buffer, “wc *.*”); system(buffer);}
The exec( ) FamilyThe exec( ) Family
char *path, *file;char *path, *file;
char *arg0, *arg1, ..., *argn;char *arg0, *arg1, ..., *argn;
char *argv[ ];char *argv[ ];
int ret;int ret;
ret = execl (path, arg0, arg1, ... , argn, (char *)0);ret = execl (path, arg0, arg1, ... , argn, (char *)0);
ret = execv (path, argv);ret = execv (path, argv);
ret = execlp (file, arg0, arg1, ... , argn, (char *) 0);ret = execlp (file, arg0, arg1, ... , argn, (char *) 0);
ret = execvp (file, argv);ret = execvp (file, argv);
The fork( ) FacilityThe fork( ) Facility
Basic process creation primitive. Basic process creation primitive. Duplicates the calling process. Duplicates the calling process. New process New process child processchild process Initiating process is Initiating process is parentparent.. Returns childReturns child’’s s process-idprocess-id
Inherited Data and File Inherited Data and File DescriptorsDescriptors
Forked child has instances of current Forked child has instances of current values of the variables and open file values of the variables and open file descriptors.descriptors.
Variables; Variables; ““passpass”” by copy by copy Read/write pointers for a file; Read/write pointers for a file; referencereference
wait( )wait( )
int wait (int * status)int wait (int * status) Suspends execution of process while child Suspends execution of process while child
runsruns Parent resumes upon child terminationParent resumes upon child termination When the When the firstfirst child process terminates child process terminates Normally returns process-id of exiting child. Normally returns process-id of exiting child. On error, wait( ) returns a -1, usually meaning On error, wait( ) returns a -1, usually meaning
no child exists on which to wait.no child exists on which to wait.
Zombies and Premature ExitsZombies and Premature Exits
Zombie processes are ones that somehow Zombie processes are ones that somehow lose connection with the parent. How?lose connection with the parent. How?
Zombie occupies a slot in process table, Zombie occupies a slot in process table, but uses no system resources. but uses no system resources.
Zombie cleared when parent Zombie cleared when parent ““claimsclaims”” child child Wait removes chance of zombie???Wait removes chance of zombie???
Parent exits B4 children Parent exits B4 children premature exit premature exit
Process and Group IdProcess and Group Id’’ss
pid = getpid( ); its own process idpid = getpid( ); its own process id pid = getppid( ); parent process idpid = getppid( ); parent process id newpg = setpgrp( ); change groupnewpg = setpgrp( ); change group pgid = getpgrp( ); access group pgid = getpgrp( ); access group
UNIX SignalsUNIX SignalsSIGHUPSIGHUP - The hangup signal, sent upon user logout- The hangup signal, sent upon user logout
SIGINTSIGINT - Interrupt signal, sent on user break signal - Interrupt signal, sent on user break signal
SIGQUITSIGQUIT - Quit signal, sent when user hits quit key- Quit signal, sent when user hits quit key
SIGILL SIGILL - Illegal Instruction signal- Illegal Instruction signal
SIGTRAPSIGTRAP - Trace trap signal used by debuggers- Trace trap signal used by debuggers
SIGFPESIGFPE - Floating-Point exception signal- Floating-Point exception signal
SIGKILLSIGKILL - Sent process n to terminate process m- Sent process n to terminate process m
SIGSYSSIGSYS - Bad argument to a system call signal- Bad argument to a system call signal
SIGPIPESIGPIPE - Write on a pipe with no one to read it- Write on a pipe with no one to read it
SIGALRMSIGALRM - Alarm clock signal used for software timers- Alarm clock signal used for software timers
SIGTERMSIGTERM - Software signal to terminate process- Software signal to terminate process
SIGUSR1SIGUSR1 - user signal- user signal
SIGUSR2SIGUSR2 - user signal- user signal
pause( )pause( )
int pause( )int pause( )
pause( ) suspends the calling process, pause( ) suspends the calling process, without wasting resources, until some without wasting resources, until some kind of signal is received.kind of signal is received.
Recommended