Sie sind auf Seite 1von 7

IPC in UNIX

(InterProcress Communcation)
One Process
IPC in UNIX modulo
Communication of
modules in a
process
• global variables
CS 454/554 modulo • function calls
– parameters
– result

IPC On a Single Computer IPC on Two Computers


Process A Process B
Process A Process B

DOS (Distributed OS)


Kernel
Kernel Kernel

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

How to specify the signal sigex.C


#include <signal.h>
main()
handler? {
• Use signal system call int catchint();

i.e. #include <signal.h> signal (SIGINT, catchint);


printf(“sleep call #1\n”);
– Signal is a function that returns a pointer to a sleep(1);
function that returns an integer. printf(“sleep call #2\n”);
sleep(1);
– The function argument specifies the address of
printf(“sleep call #3\n”);
the function that doesn’t return anything. sleep(1);
– There are two special cases that provide special printf (“Exiting \n”);
exit(0);
values for the function argument }
• SIG_DFL to specify the signal is to be handled in the
default way /* trivial function to handle SIGINT */
void catchint (int signo)
• SIG_IGN to specify that the signal is to be ignored.
{
• If the signal handler returns, the process that printf(“\nCATCHINT: signo=%d\n”, signo);
printf(“CATCHINT: returning\n\n”);
received the signal continues where it was }
interrupted. (same as ctxsw())

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.

Answer: Two types of sockets in UNIX:


Note: "printf" is buffered, but "write" is not, so output is: 1. internet -- for two processes on different machines
hello world 2. UNIX domain -- for two processes on a single machine
readfd = 3, writefd = 4

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

Pipe Size Example


int count=0;
main()
• The size of a pipe is finite, i.e., only a certain {
char c=‘x’;
amount of bytes can remain in the pipe if (pipe(p) < 0)
without being read error(“pipe call”);
signal(SIGALRM,alarm_action);
• If a write is made on a pipe and there is for(;;) {
alarm(20);
enough space, then the data is sent down the write(p[1],&c,1);
alarm(0);
pipe and the call returns immediately. if((++count%1024)==0)
printf(“%d chars in pipe\n, count”);
• If, however, a write is made that will overfill }
the pipe, process execution is suspended }
alarm_action()
until room is made by another process {
printf(“write blocked after %d chars \n”, count);
reading from the pipe. exit(0)
}

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.

Das könnte Ihnen auch gefallen