Docsity

UNIX Systems Programming: Forking, Process Management, and Error Handling, Study notes of Computer Science

Partial preview of the text

Download UNIX Systems Programming: Forking, Process Management, and Error Handling and more Study notes Computer Science in PDF only on Docsity!

  • COMP 4/6262: Programming UNIX Lecture 22 Systems programming April 19,

Outline {^

system calls

{^

error handling: perror

{^

process management

System Calls

Application program

Processor

Main memory

I/O devices

Operating System

UNIX System Calls Classes {^

File management^ z

File system operations

{^

Process management^ z

Duplicate, differentiate, suspend, andterminate processes z Inteprocess communication (IPC):pipes and sockets z Signals

{^

Error handling

UNIX Process Management { A UNIX process is a unique instance of a runnableprogram/application { Every process has attributes

z^

The code z^

Data z^

A stack z^

A unique process id (

pid

)

z^

Etc. {^

When UNIX starts there is only one process:
init (PID=1)

{^

The only way to create a new process in UNIX is toduplicate an existing process^ z^

init

is the ancestor of any UNIX process

Process States {^ Running

: 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

Fork Example

{^

Key Points^ z^

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

Fork Example #2^ {

Key Points^ z

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

Fork Example #4^ {^

Key Points^ z^

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

Fork Example #5^ {^

Key Points^ z^

Both parent and child can continue forking

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 {^

it is dangerous for a parent to terminate without waitingfor the death of its child

{^

if a parent process dies before its child, the child isautomatically adopted by the original “init” process, PID 1

{^

a process that terminates cannot leave the system untilits parent accepts its return code

{^

if the parent process is already dead, it’ll already havebeen adopted by “init”, which always accept itschildren’s return code

{^

if the parent is alive, but never executes a wait()^ z^

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

Zombie Example

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

{^

int wait(int *child_status)^ z

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

wait

: Synchronizing with children

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