0% found this document useful (0 votes)
3 views

module-2-the-process-concept-and-ipc

Uploaded by

sukrankurt87
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

module-2-the-process-concept-and-ipc

Uploaded by

sukrankurt87
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Process creation

in Unix/Linux
A single parent process executes.

The parent process uses the


fork() system call to create fork()
A new process, the child
a new child process. process is created. The
new child is a copy of its
parent.

The child may use the


exec() system call after a
exec() fork to replace the process’s
memory space with a new
program.

The parent may use The child uses the exit()


the wait() system wait() exit() system call to terminate.
call to wait for the
child to terminate.

The single parent


continue to execute.
fork()
In Unix-like operating system, fork is an operation
whereby a process creates a copy of itself.

★ The process calling fork() is called the parent.

★ The new process is called the child of the parent.

★ Fork is usually a system call, implemented in the


kernel.

★ Fork is the primary (and historically, only) method of


process creation on Unix-like operating systems.

Source: https://en.wikipedia.org/wiki/Fork_(system_call) 2016-01-25


exec()
In Unix-like operating systems, the exec family of system calls
runs an executable file in the context of an already existing process,
replacing the previous executable.

★ As a new process is not created, the process identifier


(PID) does not change, but the machine code, data,
heap, and stack of the process are replaced by those of
the new program.

★ A file descriptor open when an exec call is made remain


open in the new process image, unless was fcntled with FD_CLOEXEC. This
aspect is used to specify the standard streams (stdin, stdout and stderr) of the new program.

★ As a result of exec, all data in the old program that were


not passed to the new program, or otherwise saved,
become lost.

Source: https://en.wikipedia.org/wiki/Exec_(computing) 2016-01-25


wait()
A parent process can use the wait() system call to suspend its
execution until one of its children terminates and get the exit status
of the terminated child.

pid_t wait(int *status);


★ If there is no child process running when the call to wait()
is made, then this wait() has no effect at all.
★ Otherwise, executing wait() suspends the caller until
one of its children terminates.
★ Returns the PID of the terminated child process.
★ If status is not a null pointer, the exit status of the
terminated child will be stored at the location pointed to
by status.
Source: https://en.wikipedia.org/wiki/Wait_(system_call) 2016-01-25
exit()
When a process terminates it executes an exit() system call, either
directly in its own code, or indirectly via library code.

void exit(int status);

★ The exit() call has no return value as the process


that calls it terminates and so couldn't receive a
value anyway.

★ The exit() call resumes the execution of a waiting


parent process.

★ The exit() call also communicates the status


parameter value to the parent process who can get
the status using the wait() system call.
process
ready queue CPU termination

I/O I/O
I/O queue
event request

time slice
expires

a new child process is created


fork a
child
Create a new process using fork()

Parent Process Child Process


User Space

TEXT (instructions) TEXT (instructions)


DATA DATA

Parent calls fork() The child process is a


copy of the parent

Resources (open files, etc) Resources (open files, etc)


Kernel Space

OS creates a new process - a child process


The child process is a copy of the parent (copy of TEXT, DATA and Resources)
Parent Process Child Process
User Space

TEXT (instructions) TEXT (instructions)


DATA DATA

Parent calls fork() The child process is a


copy of the parent

Resources (open files, etc) Resources (open files, etc)


Kernel

After fork(), parent and child will execute the same


program (TEXT). How can they be distinguished
from each other?
On success, fork() return twice!
parent

t fork()
ren

ch
ild
pa

fork() returns pid > 0


if executing in the parent fork() returns 0 if
process. It’s the process executing in the new child
id (pid) of the child that is process.
returned.
On failure, fork() returns -1 in the parent

parent

nt
fork()
re
pa

fork() returns -1 in
the parent if a new
process could not be
created.
Parent Process Child Process
STACK STACK
pid = 766611 pid = 0

TEXT TEXT
pid = fork(); pid = fork();

switch (pid) { switch (pid) {


case -1: case -1:
// ERROR // ERROR
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
case 0: case 0:
// CHILD // CHILD
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
default: default:
// PARENT // PARENT
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
Process
termination
process
ready queue CPU termination

I/O I/O
I/O queue
event request

time slice
expires

a new child process is created


fork a
child
exit() and wait()
When a process terminates it executes an exit() system
call, either directly, or indirectly via library code. This leaves
an exit status value (typically an integer) in the PCB of the
terminated process for the parent process to read later.

The parent may use the wait() system call to wait for
the child to terminate and read the exit status value of the
terminated child process.
Zombie
processes
Zombie
A terminated process is said to be a zombie or defunct until the
parent does wait() on the child.

★ When a process terminates all of the memory and


resources associated with it are deallocated so they can be
used by other processes.
★ However, the exit status is maintained in the PCB until
the parent picks up the status using wait() and deletes
the PCB.
★ A child process always first becomes a zombie before being
removed from the resource table.
★ In most cases, under normal system operation zombies are
immediately waited on by their parent.
★ Processes that stay zombies for a long time are generally
an error and cause a resource leak.
Source: https://en.wikipedia.org/wiki/Zombie_process 2016-01-25
Orphan
processes
Orphan
An orphan process is a process whose parent process has
terminated, though it remains running itself.

★ Any orphaned process will be immediately adopted by the


special init system process.

★ Even though technically the process has the init process as


its parent, it is still called an orphan process since the
process that originally created it no longer exists.

★ A zombie process is not the same as an orphan process. An


orphan process is a process that is still executing, but
whose parent has died. An orphan process do not become
zombie processes; instead, they are adopted
by init (process ID 1), which automatically waits on all its
child process.

Source: https://en.wikipedia.org/wiki/Orphan_process 2016-01-25


Inter process
communication
(IPC)
How can we make
processes communicate
(sharing information)?

Two methods: message passing and shared memory.


Message Passing Shared Memory

Communication take place by A region of memory that is shared by


means of messages exchanged cooperating process is established.
between the cooperating Process can then exchange
processes. information by reading and writing
data to the shared region.
Message Passing Shared Memory
Messages can be exchanged Processes can communicate by
between processes either directly or reading and writing to shared
indirectly using a common mailbox. memory.

The recipient process usually must Normally, the OS tries to prevent one
give its permission for process from accessing another
communication to take place with process’s memory. Shared-memory
an accept connection system call. requires that two or more processes
agree to remove this restriction.
Message-passing is useful for
exchanging smaller amounts of Shared memory allows maximum
data, because no conflicts need be speed and convenience of
avoided. communication, since it can be done
at memory transfer speeds.
Easier to implement compared to
the shared memory approach. Problems: protection and
synchronization between the
processes sharing memory.
Signals
Signals (1)
Signals are a limited form of inter-process communication used in
Unix, Unix-like, and other POSIX-compliant operating systems

★ A signal is an notification sent to a process in order


to notify it of an event that occurred.

★ When a signal is sent, the operating system


interrupts the target process's normal flow of
execution to deliver the signal.

★ If the process has previously registered a signal


handler, that routine is executed. Otherwise, the
default signal handler is executed.

Source: https://en.wikipedia.org/wiki/Unix_signal 2016-01-25


Signals (2)
Signals can be synchronous or asynchronous.

Synchronous signals

★ Are delivered to the same process that performed the


operation that caused the signal.

★ Examples of synchronous signals: illegal memory access


and division by zero.

Asynchronous signals

★ Are generated by an event external to a running process.

★ Typically, an asynchronous signal is sent to another process.

★ Examples of asynchronous signals: terminating a process


(ctrl-c), timer expire.
Signals (3)
Typing certain key combinations at the controlling terminal of a
running process causes the system to send it certain signals

★ Ctrl-C sends an INT signal (SIGINT); by default, this


causes the process to terminate.

★ Ctrl-Z sends a TSTP signal (SIGTSTP); by default,


this causes the process to suspend execution.
Signals (4)
The kernel can generate a signal to notify the process of an event.

★ Exceptions such as division by zero or a


segmentation violation will generate signals.

★ Divison by zero will generate a SIGFPE signal.

★ Segmentation violation will generate a SIGSEGV


signal.

★ If not explicitly caught, SIGFPE and SIGSEGV will


cause a core dump and a program exit.
kill()
In Unix-like operating systems, the kill system call is
used to send signals to processes.

There are many different signals that can be sent, although the
signals in which users are generally most interested are
SIGTERM and SIGKILL.

★ The SIGKILL signal is sent to a process to cause it to


terminate immediately (kill). In contrast to SIGTERM and SIGINT, this signal
cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving
this signal.

★ The SIGTERM signal is sent to a process to request its


termination. Unlike the SIGKILL signal, it can be caught and interpreted or ignored by the
process. This allows the process to perform nice termination releasing resources and saving state if
appropriate. SIGINT is nearly identical to SIGTERM.

Source: https://en.wikipedia.org/wiki/Kill_(command) 2016-01-25


https://en.wikipedia.org/wiki/Unix_signal 2016-01-25
Pipes
Pipes
An (anonymous) pipe is a simplex FIFO
communication channel that may be used for
one-way interprocess communication (IPC).

★ An implementation is often integrated into the


operating system's file IO subsystem.

★ Pipes where one of the first IPC mechanisms in early


UNIX systems.

★ Typically a parent program opens anonymous pipes,


and creates a new process that inherits the other
ends of the pipes, or creates several new processes
and arranges them in a pipeline.
Source: https://en.wikipedia.org/wiki/Anonymous_pipe 2016-01-25
Producer and consumer
Anonymous pipes allow two processes to communicate in standard
producer consumer fashion.

★ The producer writes to one end of the pipe (the write end).

★ The consumer reads from one end of the pipe (the read end)

★ As a result, ordinary pipes are unidirectional, allowing only


one-way communication.

★ If two-way communication is required, two pipes must be


used, with each pipe sending data in a different direction.
pipe()
In Unix-like operating systems pipes are created using the pipe()
system call.

★ As a result of the pipe() system call, a


pipe object is created. 0 read 0

FIFO
★ A pipe is a FIFO buffer that can be
used for inter process communication
(IPC). 1 write 1

★ The pipe() system call returns a pair


of file descriptors referring to the
read and write ends of the pipe.

Source: https://en.wikipedia.org/wiki/Anonymous_pipe 2016-01-25


Using pipe()

Parent Process (1)


User Space

int pfd[2]; As a result of the pipe() system call,


pipe(pfd); a pipe object is created. A pipe is a
FIFO buffer that can be used for inter
// pfd[0] = 3
// pfd[1] = 4 process communication (IPC).

(2)
As a result of the pipe() system call,
the elements of the array pfd are
assigned descriptor numbers to the
Descriptors created pipe.
0 stdin

1 stdout 0 read 0
Now a single
Kernel Space

2 stderr
process can write
FIFO

3 pipe read data to, and later


read data from the
4 pipe write
pipe ...
1 write 1
Pipe and Fork

+
Using pipe() together with fork()

Parent Process Child Process


User Space

int pfd[2];
pipe(pfd); After fork(), the child
process have the same
// pfd[0] = 3
set of descriptors
// pfd[1] = 4 including read and
write descriptors to the
fork();
pipe.

fork()

Descriptors Descriptors

0 stdin 0 stdin

1 stdout 0 read 0 1 stdout


Kernel Space

2 stderr 2 stderr
FIFO

3 pipe read 3 pipe read

4 pipe write 4 pipe write


1 write 1
Using pipe() together with fork()
The parent can close
Parent Process Child Process
the read descriptor to
User Space

the pipe.
int pfd[2];
pipe(pfd); The child can close
the write descriptor
// pfd[0] = 3 to the pipe.
// pfd[1] = 4 Now, the parent can act
as a single producer and
fork(); the child as a single
consumer of data close(pfd[1]);
close(pfd[0]);
through the pipe using
the read() and write()
system calls - just as if
it was a file.

Descriptors Descriptors

0 stdin 0 stdin

1 stdout 0 read 0 1 stdout


Kernel Space

2 stderr 2 stderr
FIFO

3 pipe read 3 pipe read

4 pipe write 4 pipe write


1 write 1
Blocking
Reading and writing to a pipe may block a process.

Attempt Conditions Result

Read Empty pipe, writer attached Reader blocked

Write Full pipe, reader attached Writer blocked

Empty pipe, no writer


Read EOF returned
attached

Write No reader ?
Pipes and signals

+
SIGPIPE
The SIGPIPE signal is used to notify a producer (writer) when there
is no longer a consumer (reader) attached to a pipe.

Attempt Conditions Result

Read Empty pipe, writer attached Reader blocked

Write Full pipe, reader attached Writer blocked

Empty pipe, no writer


Read EOF returned
attached

Write No readers attached SIGPIPE


By default, SIGPIPE causes the process to terminate.
Don’t forget to close unused pipe file descriptors using
the close() system call.

You might also like