














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















Application program
Processor
Main memory
I/O devices
Operating System
File system operations
Duplicate, differentiate, suspend, andterminate processes z Inteprocess communication (IPC):pipes and sockets z Signals
z^
The code z^
Data z^
A stack z^
A unique process id (
pid
)
z^
Etc. {^
{^
init
is the ancestor of any UNIX process
: the process is currently using CPU
{^
Runnable
: the process can make use of the CPU
{^
Sleeping
: the process is waiting for an event, e.g.
I/O, to occur {^
Suspended
: the process is frozen by a signal;
another signal is needed to wake up {^
Idle
: the process is being created by a
fork()
and not
yet runnable {^
Zombified
: the process ended but not returned its
exit code to its parent; a process remain zombie untilits parent accepts its return code via the wait()system call
{^
Parent and child both run same code^ {
Distinguish parent from child by return value from
fork
z^
Start with same state, but each has private copy^ {
Including shared output file descriptor { Relative ordering of their print statements undefined
void
fork1() {
int x = 1;pid_t pid =
fork();
if (pid
== 0) {printf("Child has x = %d\n", ++x); } else {
printf("Parent has x = %d\n", --x); } printf("Bye
from process %d with x =
%d\n", getpid(), x);}^
exFork/fork1.c
Both parent and child can continue forking void fork2(){
printf("L0\n");fork();printf("L1\n");fork();printf("Bye\n"); }
L^
L1 L
ByeByeBye Bye
Both parent and child can continue forking void fork4(){
printf("L0\n");if (fork() != 0) {
printf("L1\n");if (fork() != 0) {
printf("L2\n");fork(); } } printf("Bye\n"); }
L^
L
Bye L
ByeBye Bye
Key Points^ z^
void fork5(){
printf("L0\n");if (fork() == 0) {
printf("L1\n");if (fork() == 0) {
printf("L2\n");fork(); } } printf("Bye\n"); }
L^
L1 Bye
Bye^ Bye L2^ Bye
Orphans and zombies {^
{^
{^
{^
{^
the child process’ return code will never be accepted z^
the process is a zombie z^
doesn’t have any code, data, or stack z^
inhabit the system’s fixed-size process table
void fork7(){
if (fork() == 0) {
/* Child */printf("Terminating Child, PID = %d\n",
getpid()); exit(0); } else {
printf("Running Parent, PID = %d\n",
getpid()); while (1)
; /* Infinite loop */
} }
linux>
./forks 7 & [1] 6639Running Parent, PID = 6639Terminating Child, PID = 6640linux>
ps PID TTY
TIME CMD
6585 ttyp
00:00:00 tcsh
6639 ttyp
00:00:03 forks
6640 ttyp
00:00:00 forks
6641 ttyp
00:00:00 ps
linux>
kill 6639 [1]^
Terminated linux>
ps PID TTY
TIME CMD
6585 ttyp
00:00:00 tcsh
6642 ttyp
00:00:00 ps
{^
ps
shows child process as“defunct”
wait
: Synchronizing with children
suspends current process until one ofits children terminates z return value is the
pid
of the child
process that terminated z^
if^
child_status != NULL
, then the
object it points to will be set to a statusindicating why the child processterminated
void
fork9()
{
int
child_status; if^
(fork()
==
{
printf("HC:
hello
from
child\n");
} else {
printf("HP:
hello
from
parent\n");
wait(&child_status);printf("CT:
child
has
terminated\n");
} printf("Bye\n");exit(); }
HC^ HP
Bye
CT^
Bye