scis.regis.edu ● [email protected] CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1
Dec 14, 2015
Process Management
•First process is init•Process are created by processes
▫Calling fork()▫One of few calls that return twice
•Child process inherits parent’s process table entries, except for PID, PPID, and running times
•Call to fork returns the PID of the child process to the parent and 0 to the child
•Best practice is to test the return of fork()3
First Processes
•PID 0 (sched) – created at boot•PID 0 executes fork and exec twice to
create processes 1 (init) and 2 (pageout)
4
Process 1
fork/exec fork/exec
process 48 process 12
process 34
fork/exec
Process States
5
RunningZombie
dRunnabl
eIdle
Sleeping
Suspended
Initialize
Signal Signal
Exit
Wait on Event
EventOccurs
AllocatedCPU
Parent process
•A process creates a child process•Parent waits until children processes
terminate•He return code from the child is passed to
the parent to take appropriate action• If the parent process terminates
unexpectedly, all its children processes are inherited by init
•When a child terminates, it waits to die until parent acknowledges its termination
6
Unix process-oriented system calls•fork – duplicates a process•getpid – obtains process ID number•getppid – obtains parent process ID
number•exit – terminates a process•wait – waits for child process•exec – replaces the code, data, and stack
of a process
7
New process with fork
8
•The parent calls fork and two return from it▫The parent and child continue to run the
same code concurrently▫Each has a different stack and data spaces
•If fork succeeds, it returns the PID of the child to the parent and returns 0 to the child
•If fork fails, it returns -1 and there is no child process created
Process IDs
•A process can get its process ID and parent ID▫getpid = returns process ID▫getppid = returns parent process ID
•See example program on page 475 in UPU•You can try the program if you have a C/C+
+ compiler•For cygwin, you can use g++•Some examples follow
9
Orphan processes
•If a parent dies before its children processes, init adopts them
•The original PPID becomes 1 (init)•Note how the child inherits PPID after
parent terminates
12
Executing other programs
•Can call other programs with exec•When running exec, your process current
image is replaced with the program’s image
•When calling a program, your current process never terminates▫It is replaced by the called program
16
Explanation•Displays message on the screen•Calls one of exec function
▫execl means to pass an argument▫execlp means to search the path
•First argument is the program to run•The rest of the parameters are passed to
the programs as arguments (argv)▫argv[0] is the name of the program
(duplicated)▫argv[1] is the directory list▫argv[2] null pointer – end of arguments
19
Exec family of programs
•Exec followed by one or more characters▫e = array of pointes to variables▫l = command line▫p = use PATH variable▫v = command line arguments passes as
array of pointers•Arguments (arv[x]):
▫0 – name of program▫Rest depend on the program being
executed
20
Notes on executed program
•New program inherits no code or data from current process
•Signals and signal handlers are cleared•Security information and PID of process
are retained▫Including UID of owner ▫File descriptors remain open and available
to new program
21
Child process clean up
•If parent terminates right away, no problem
•If parent continues to execute and the child exits, parent must clean up▫Or zombie processes hang around
22
Waiting for a process to continue•When we exec a program, the original
process is no longer active•How does the shell remain active when
we execute commands?•We want the shell to remain dormant
while the command executes and then we want to return to the shell
27
What happened?
•The code invokes the shell in the child▫PS1 sets the prompt for the shell
•The child invokes the shell•We get interactive access to the shell while
the child waits•When we exit, we return to the child, and
the child exits•Parent notices child exited and then parent
exits•Parent is blocked until child is done
32
Other programs
•myexec.c – pp. 480-481•chdir.c - pp 481-482•nice.c pp482-483•background.c – pp.484
33
Signals
•Sometimes called interrupts•Called when unexpected things happen:
▫Floating point errors▫Power failures▫Alarm clock ‘ring’▫Death of child process▫User termination▫User suspend request
•Signal handler is called to act on it
34
Signals
1 – SIGHUP: hangup2 – SIGINT: interrupt3 – SIGQUIT: quit4 – SIGILL: invalid instruction5 – SIGTRAP: trace trap6 – SIGABRT: abort7 – SIGEMT: emulator trap instruction8 – SIGFPE: arithmetic exception 9 – SIGKILL: kill
35
Signals (Cont’d)
10 – SIGBUS: bus error11 – SIGSEGV: segmentation violation12 – SIGSYS: bad argument to system call13 – SIGPIPE: write on pipe and no reader14 – SIGALRM: alarm clock15 – SIGTERM: software termination16/17 – SIGUSR1/1: user signal 1/2 18 – SIGCHLD: child status changed19 – SIGPWR: power fail or restart
36
Signals (Cont’d)20 – SIGWINCH: window size changed21 – SIGURG: urgent socket condition22 – SIGPOLL: pollable event23 – SIGSTOP: stopped (signal)24 – SIGSTP: stopped (user)25 – SIGCONT: continued26/27 – SIGTTIN/SIGTTOU: stopped (in/out) 28 – SIGVTALRM: virtual timer expired29 – SIGPROF: profiler timer expired30/31 – SIGXCPU/SIGFSZ: CPU/File limit exceeded
37
Control Terminals
•<Ctrl>-<C> terminates a process and its children
•Every process is a member of a process group•When a process forks, children inherit its
process group from its parent•Every process has a control terminal – the
process where the process was started•When <Ctrl>-<C> is detected, the terminal
sends the signal to all processes in the process group
41