Operating Systems - Week 5 - IPC
Operating Systems - Week 5 - IPC
Process Management
Process Management
Interprocessus Synchronisation
Memory Management
• Interprocesses communication
• Signals
• Pipes
• Shared Memory
5. Interprocesses Communication
Interprocesses communication
returns 0 or -1
raise (SIGINT);
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>
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
Principale
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
Principale
#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 the child to father.
#include <sys/types.h> // types
#include <unistd.h> close(fd[R]); // he is a writer
#include <stdio.h> write(fd[W],phrase, strlen(phrase)+1) ;
#define R 0 close ( fd[W]) ;// end of the communication
#define W 1 }
int main() else
{ { // the parent is the reader from he pipe
int fd [2]; //it will close fd[1]
char message[100]; //it will read with fd[0]
int nboctets;
char *phrase = " Message sent from child to parent" ; close(fd[W]) ;//he is a reader
pipe(fd ); //create a pipe nboctets = read (fd[R], message,100) ;
if ( fork () ==0)// created a child printf ( "Lecture %d octets : %s\n", nboctets , message) ;
{// the child is the writer in the pipe close ( fd[R ]) ;// end of communiaction
The child will use fd[1] so it will close the fd[0] }
Exit(0);
}
Interprocesses communication
1. signals
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
Principales
The problem with pipes, fifo and message queue – is that for two
process to exchange information. The information has to go through
the kernel.