ch03: processes in unix
DESCRIPTION
ch03: Processes in UNIX. Ju, Hong Taek Computer Network Lab. Keimyung University [email protected] 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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/1.jpg)
2005 System Programming
ch03: Processes in UNIX
Ju, Hong TaekComputer Network Lab.
Keimyung [email protected]
Rm: 1228, Tel: 580-5234
![Page 2: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/2.jpg)
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
![Page 3: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/3.jpg)
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 th
at convey privileges to the process real user and group ID: process ownership effective user and group ID: determining access permissi
on for resources The getegid, geteuid, getgid, getuid function
s are used for retrieve relative value
![Page 4: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/4.jpg)
3.2 Process State
The state of a process indicates its status at a particular time
State Meaning
new Being created
running Instructions are being executed
blocked Waiting for an event such as I/O
ready Waiting to be assigned to a processor
done Finished
![Page 5: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/5.jpg)
new ready blocked
running done
process created
selected to run
quantumexpired
I/O completed
I/O requested
normal or abnormaltermination
![Page 6: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/6.jpg)
The ps utility display information about processes
![Page 7: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/7.jpg)
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
![Page 8: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/8.jpg)
![Page 9: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/9.jpg)
![Page 10: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/10.jpg)
![Page 11: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/11.jpg)
![Page 12: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/12.jpg)
1
2
3
4
5
![Page 13: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/13.jpg)
![Page 14: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/14.jpg)
1 2 3
4
![Page 15: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/15.jpg)
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);
![Page 16: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/16.jpg)
![Page 17: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/17.jpg)
![Page 18: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/18.jpg)
Describe the possible forms of the output?
![Page 19: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/19.jpg)
![Page 20: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/20.jpg)
![Page 21: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/21.jpg)
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[]);
![Page 22: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/22.jpg)
![Page 23: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/23.jpg)
![Page 24: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/24.jpg)
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
![Page 25: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/25.jpg)
![Page 26: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/26.jpg)
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
![Page 27: ch03: Processes in UNIX](https://reader034.vdocument.in/reader034/viewer/2022051401/56814e38550346895dbb9eeb/html5/thumbnails/27.jpg)
Question and Answer