280 likes | 415 Views
ch03: Processes in UNIX. Ju, Hong Taek Computer Network Lab. Keimyung University juht@kmu.ac.kr Rm: 1228, Tel: 580-5234. Objectives. Learn how to create processes Experiment with fork and exec Explore the implications of process inheritance Use wait for process cleanup
E N D
ch03: Processes in UNIX Ju, Hong Taek Computer Network Lab. Keimyung University juht@kmu.ac.kr Rm: 1228, Tel: 580-5234
Objectives • Learn how to create processes • Experiment with fork and exec • Explore the implications of process inheritance • Use wait for process cleanup • Understand the UNIX process model
3.1 Process Identification • Unix identifies process by a unique value called the process ID • Each process also has a parent process ID • The getpid and getppid funtions return the process ID and the parent process ID, respectively • A UNIX process has several user and group IDs that convey privileges to the process • real user and group ID: process ownership • effective user and group ID: determining access permission for resources • The getegid, geteuid, getgid, getuid functions are used for retrieve relative value
3.2 Process State • The state of a process indicates its status at a particular time
normal or abnormal termination running done selected to run I/O requested process created quantum expired new ready blocked I/O completed
3.3 UNIX Process Creation and fork • A process can create a new process by calling fork • The calling process becomes the parent, and the created process is called the child #include <sys/types.h> #include <unistd.h> pid_t fork(void); return -1 if errorreturn 0 to childreturn child's pid to parent
1 2 3 4 5
4 1 2 3
3.4 The wait Function • The parent can execute wait or waitpid to block until the child finishes • waitIf a child terminated, return its pidOtherwise return -1 and set errno • waitpidAllows you to wait for a particular process, or all process if pid is -1. Important option is NOHANG which will return 0 if there is a specified child to wait for but it has not yet terminated. #include <sys/wait.h> pid_t wait(int *stat_loc); pid_t waitpid(pid_t pid, int *stat_loc, int options);
3.5 The exec Function • The exec family of functions provides a facility for overlaying the process image of the calling process with a new image #include <unistd.h> extern char **environ; int execl(const char *path, const char *arg0, ... /*, char *(0) */); int execle (const char *path, const char *arg0, ... /*, char *(0),*/ char *const envp[]); int execlp (const char *file, const char *arg0, ... /*, char *(0) */); int execv(const char *path, char *const argv[]); int execve (const char *path, char *const argv[], char *const envp[]); int execvp (const char *file, char *const argv[]);
3.6 Background Processes and Daemons • A daemon is a background process that normally runs indefinitely • When a shell create a background process, it does not wait for the process to complete before issuing a prompt and accepting additional commands
3.7 Critical Section • The portion of code in which each process access, but should be used by only one process at a time, is called a critical section • Programs with critical section must be sure not to violate the mutual exclusion • One method of providing mutual exclusion uses a locking mechanism • Operating systems manages many shared devices that requires exclusive access by the processes in the system • A common approach is to have only one daemon handle the device with message queue