110 likes | 123 Views
Understand concurrent processes, creating new processes, inherited data, file descriptors, zombies, premature exits, process and group IDs, UNIX signals, and more. Learn how to effectively manage processes in the UNIX environment.
E N D
Concurrent Processes • Processes can concurrently run same program. • Processes can start other processes. • UNIX provides concurrency functions
Creating New Processes • system -- makes a shell • Exec -- replace this process with a new one • Fork -- start a concurrent process
Example “System” int main() { char buffer[80]; strcpy(buffer, “wc *.*”); system(buffer); }
The exec( ) Family char *path, *file; char *arg0, *arg1, ..., *argn; char *argv[ ]; int ret; ret = execl (path, arg0, arg1, ... , argn, (char *)0); ret = execv (path, argv); ret = execlp (file, arg0, arg1, ... , argn, (char *) 0); ret = execvp (file, argv);
The fork( ) Facility • Basic process creation primitive. • Duplicates the calling process. • New process child process • Initiating process is parent. • Returns child’s process-id
Inherited Data and File Descriptors • Forked child has instances of current values of the variables and open file descriptors. • Variables; “pass” by copy • Read/write pointers for a file; reference
wait( ) • int wait (int * status) • Suspends execution of process while child runs • Parent resumes upon child termination • When the first child process terminates • Normally returns process-id of exiting child. • On error, wait( ) returns a -1, usually meaning no child exists on which to wait.
Zombies and Premature Exits • Zombie processes are ones that somehow lose connection with the parent. How? • Zombie occupies a slot in process table, but uses no system resources. • Zombie cleared when parent “claims” child • Wait removes chance of zombie??? • Parent exits B4 children premature exit
Process and Group Id’s • pid = getpid( ); its own process id • pid = getppid( ); parent process id • newpg = setpgrp( ); change group • pgid = getpgrp( ); access group
UNIX Signals SIGHUP - The hangup signal, sent upon user logout SIGINT - Interrupt signal, sent on user break signal SIGQUIT - Quit signal, sent when user hits quit key SIGILL - Illegal Instruction signal SIGTRAP - Trace trap signal used by debuggers SIGFPE - Floating-Point exception signal SIGKILL - Sent process n to terminate process m SIGSYS - Bad argument to a system call signal SIGPIPE - Write on a pipe with no one to read it SIGALRM - Alarm clock signal used for software timers SIGTERM - Software signal to terminate process SIGUSR1 - user signal SIGUSR2 - user signal
pause( ) int pause( ) pause( ) suspends the calling process, without wasting resources, until some kind of signal is received.