ch03: processes in unix

27
2005 System Programming ch03: Processes in UNIX Ju, Hong Taek Computer Network Lab. Keimyung University [email protected] Rm: 1228, Tel: 580-5234

Upload: ranit

Post on 19-Jan-2016

54 views

Category:

Documents


6 download

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 Presentation

TRANSCRIPT

Page 1: ch03: Processes in UNIX

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

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

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

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

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

The ps utility display information about processes

Page 7: ch03: Processes in UNIX

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
Page 9: ch03: Processes in UNIX
Page 10: ch03: Processes in UNIX
Page 11: ch03: Processes in UNIX
Page 12: ch03: Processes in UNIX

1

2

3

4

5

Page 13: ch03: Processes in UNIX
Page 14: ch03: Processes in UNIX

1 2 3

4

Page 15: ch03: Processes in UNIX

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
Page 17: ch03: Processes in UNIX
Page 18: ch03: Processes in UNIX

Describe the possible forms of the output?

Page 19: ch03: Processes in UNIX
Page 20: ch03: Processes in UNIX
Page 21: ch03: Processes in UNIX

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
Page 23: ch03: Processes in UNIX
Page 24: ch03: Processes in UNIX

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
Page 26: ch03: Processes in UNIX

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

Question and Answer