module-2-the-process-concept-and-ipc
module-2-the-process-concept-and-ipc
in Unix/Linux
A single parent process executes.
I/O I/O
I/O queue
event request
time slice
expires
t fork()
ren
ch
ild
pa
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();
I/O I/O
I/O queue
event request
time slice
expires
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.
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
Synchronous signals
Asynchronous signals
There are many different signals that can be sent, although the
signals in which users are generally most interested are
SIGTERM and SIGKILL.
★ The producer writes to one end of the pipe (the write end).
★ The consumer reads from one end of the pipe (the read end)
FIFO
★ A pipe is a FIFO buffer that can be
used for inter process communication
(IPC). 1 write 1
(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
+
Using pipe() together with fork()
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
2 stderr 2 stderr
FIFO
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
2 stderr 2 stderr
FIFO
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.