Professional Documents
Culture Documents
Cha 3 Interprocess Communication
Cha 3 Interprocess Communication
Process Management
1
Road Map
Introduction to operating systems
Process Management
InterprocessusCommunication and
Synchronisation
Memory Management
3
Outlines of the section
• Interprocesses communication
• Signals
• Pipes
• Shared Memory
1. Interprocesses Communication
Interprocesses communication
received signals
Delivered
signals Mask rotines
#include<signal.h>
returns 0 or -1
P1 P2
Pid=35
kill(SIGINT, 35)
kill(2, 35)
Interprocesses communication
1. Signals
Handling signal
#include <signal.h>
typedef void (*sighandler_t)(int);
Sighandler_t signal(int signum, sighandler_t handler);
#include <unistd.h>
int pause(void) // blocks the calling process until the reception of a signal
#include <unistd.h>
unsigned int alarm(unsigned int nb_sec);
// Sets an alarm after nb_sec seconds. It will send a SIGALARM signal to the calling
process after nb_sec seconds to kill him. You can change the treatment.
Interprocesses communication
1. signals
Example
Too late
Signal(SIGALARM, &beep);
Interprocesses communication
1. Signals
Exercice
Write a program C that creates a child. The child prints odd integers from 1 to 100
and the parent prints even intgers from 1 to 100. Printed integers must be ordered.
Interprocesses communication
2. Pipes
A pipe acts as a conduit allowing two processes to communicate. Pipes were one of
the first IPC mechanisms in early UNIX systems. They typically provide one of the
simpler ways for processes to communicate with one another.
Interprocesses communication
2. Pipes
#include<unistd.h>
Table where the system stores the file
int pipe(int pipedes[2]); descriptors that you can use to write and
read from the pipe
Write a C program that creates a child and communicate with him through a pipe.
The communication flow is from the father to the child.
#include <sys/types.h> // types
#include <unistd.h> close(fd[R]);
#include <stdio.h> write(fd[W],phrase, strlen(phrase)+1) ;
#define R 0 close ( fd[W]) ;
#define W 1 }
int main() else
{ {
int fd [2];
char message[100]; close(fd[W]) ;
int nbbytes; nboctets = read (fd[R], message,100) ;
char phrase = « Message sent from parent to child" ; printf ( « Reading %d bytes : %s\n", nbbytes , message) ;
pipe(fd ); close ( fd[R ]) ;
if ( fork () ==0) }
{ Exit(0);
}
Interprocesses communication
2. Pipes
Exercice
Write a program C that creates a child. Use unamed pipes to realize a bidirectionnel
communication between parent and child.
Interprocesses communication
3. Shared Memory
The problem with pipes, fifo and message queue is that for two process to
exchange information. The information has to go through the kernel.