Beruflich Dokumente
Kultur Dokumente
(InterProcress Communcation)
One Process
IPC in UNIX modulo
Communication of
modules in a
process
• global variables
CS 454/554 modulo • function calls
– parameters
– result
1
Several Different Methods of Signals (on Unix)
IPC (in UNIX)
• Software interrupts
Signals (Software Interrupt)
• A notification to a process that an event
Pipes and FIFOs has occurred
Message Queues – usually the process doesn’t know ahead of
Semaphores time exactly when a signal will occur
Shared Memory • Signals can be sent by:
Sockets – a process to another process (or to itself)
RPC (Remote Procedure Calls) – the kernel to a process
Mutex and Conditional Variables
Signals Generation
• Every signal has a name specified in <signal.h> Signals Generation (cont)
Some examples of how signals are generated. (3) Certain hardware conditions generate
(1) The kill command can be used to send signals. signals
• floating point arithmetic errors generate a signal
(2) Certain terminal characters generate signals
called SIGFPE
• an interrrupt character (<cntrl>-c or Delete) generates a
signal called SIGINT (4) Certain software conditions can generate
• a quit character (<cntrl>-backslash) signals
– terminates a process • SIGURG signal is generated when some urgent
– generates a signal called SIGQUIT to process and generates a data arrives on a socket
core image of the process
(a core file image can be used with a debugger for analysis)
2
What can a process do with a What can a process do with a
signal? signal?
1. A process can provided a function that is 3. A process can allow the default to
called whenever a specific type of signal happen. Normally, a process is
occurs. This function, called a signal terminated on receipt of a signal with
handler, can do whatever the process wants certain signals generating a core image
to handle the condition. (“catch” the signal)
of the process in the current working
2. A process can choose to ignore the signal. directory.
All signals, except SIGKILL, can be ignored.
– SIGKILL is special, it guarantees the system
administrator a way of terminating any process
3
No user signals generated (i.e. <cntrl> - c)
> sigex
sleep call #1
sleep call #2
Pipes
sleep call #3
• A pipe provides a one-way flow of data
Exiting
- example: who | sort| lpr
>
+ output of who is input to sort
User generates a signal. + output of sort is input to lpr
> sigex
• The difference between a file and a pipe:
sleep call #1
- pipe is a data structure in the kernel.
(user presses <cntrl> - c)
• A pipe is created by using the pipe system call
CATCHINT: signo = 2 int pipe(int* filedes);
CATCHINT: returning
• Two file descriptors are returned
sleep call #2
- filedes[0] is open for reading
sleep call #3
- filedes[1] is open for writing
Exiting
• Typical size is 512 bytes (Minimum limit defined by
>
POSIX)
main()
{
user process int pipefd[2], n;
char buff[100];
readfd if (pipe(pipefd) < 0) {
error ("pipe error");
writefd }
printf ("readfd = %d, writefd = %d\n",
pipefd[0], pipefd[1]);
if (write(pipefd[1], "hello world\n", 12) != 12) {
error ("write error");
}
pipe if ((n=read(pipefd[0], buff, sizeof[buff])) < 0) {
error ("read error");
flow of data }
write (1, buff, n);
kernel exit (0); 0: stdin
} 1: stdout
2: stderr
4
Question UNIX Pipe Implementation
If pipefd[0] = 3 and pipefd[1] = 4, what is the output of Pipes are typically used to communicate between two
the above code? different (but related) processes. They are implemented
using UNIX domain sockets.
Pipe Examples
Pipe Creation
Parent opens file, child reads file
• First, a process creates a pipe, and then forks to
• parent closes read end of pipe
create a copy of itself.
• child closes write end of pipe
parent process child process
fork parent process child process
readfd readfd fork readfd
writefd writefd
writefd
pipe pipe
flow of data flow of data
kernel kernel
5
who | sort | lpr
Parent sends file name to child. Child opens and reads
file, then returns contents to parent.
• who process writes to pipe1
• sort process reads from pipe1, writes to pipe2 • parent creates
• lpr process reads from pipe2 parent process child process
pipe 1, pipe 2
readfd fork readfd
• fork
who process sort process lpr process • parent closes read writefd writefd
readfd readfd end of pipe1
writefd writefd • parent closes write
end of pipe2 pipe1
• child closes write flow of data
pipe1 pipe2 end of pipe1
flow of data flow of data pipe2
• child closes read
kernel end of pipe2 flow of data
kernel
6
Semaphore (on UNIX)
• is a synchronization primitive
• is a form of IPC, semaphore is not used
for exchanging large amounts of data,
as is pipe; but are intended to let
multiple processes synchronize their
operations.