0% found this document useful (0 votes)
12 views

Operating Systems - Week 5 - IPC

This document discusses interprocess communication in operating systems. It covers signals, pipes, and shared memory as methods for processes to communicate. Signals provide minimal communication by notifying processes of events. Pipes allow two processes to communicate by acting as a conduit between their file descriptors. Shared memory allows processes to directly read and write to the same region of memory.

Uploaded by

Dali Belaiba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Operating Systems - Week 5 - IPC

This document discusses interprocess communication in operating systems. It covers signals, pipes, and shared memory as methods for processes to communicate. Signals provide minimal communication by notifying processes of events. Pipes allow two processes to communicate by acting as a conduit between their file descriptors. Shared memory allows processes to directly read and write to the same region of memory.

Uploaded by

Dali Belaiba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Operating Systems (OS)

Process Management

Dr. Najar Yousra


Email : [email protected]

Med Tech First Year Eng 2021/2022


CS303 Dr. Najar Yousra
Fall 2021 1
Road Map
 Introduction to operating systems

 Process Management

 Interprocessus Synchronisation

 Memory Management

 File System Management


CS303 Dr. Najar Yousra
Fall 2021 2
Week 5 (04/10/2021)
 The Lecture explains some
interprocesses Communication tools :
Signals, pipes and shared memory.
 The student will be able to understand
the mechanism of the IPC and to writr C
programs handling them.
 Recitation : Processes Scheduling

CS303 Dr. Najar Yousra


Fall 2021 3
Outlines of the section

• Interprocesses communication
• Signals
• Pipes
• Shared Memory
5. Interprocesses Communication
Interprocesses communication

 Processes executing concurrently in the operating system


may be either independent processes or cooperating
processes.
 A process is independent if it cannot affect or be affected
by the other processes executing in the system. Any
process that does not share data with any other process
is independent.
 A process is cooperating if it can affect or be affected by
the other processes executing in the system.
 Processus could
◦ Communicate with signals
◦ Share data and informations
Interprocesses communication (IPC)

 Signals are the minimal level of communication between


processes.

 Pipes, messages, memory are used to communicate data


between processes.
Interprocesses communication
1. Signals

 Signals are used to notify a process of an synchronous or


asynchronous event. Normally, they are software interruptions.
Hardware interruption (from devices ) are interrupts.
 Signals are a limited form of inter-process communication (IPC).
 When a signal is sent, the operating system interrupts the target
process’ normal flow of execution to deliver the signal.
 Each signal is associated to a default routine (An action to
execute).
 A signal is identified by an integer value < NSIG. Instead of using
the numeric values directly, the named constants defined
in signals.h should be used.
Interprocesses communication
1. Signals
Interprocesses communication
1. Signals (example UNIX)
$kill -l
Interprocesses communication
1. Signals

A signal has a state it could be :

• Delivired : sent to the process


• Masked : not treatable by the processes
• Waiting : sent, not masked but not yet treated
• Pending : sent and masked
• Notified : sent and executing its treatment
• Treated: sent, not masked and finished its treatement
Interprocesses communication
1. Signals
Data structures to manage signals in
the Process PCB are three :
Process
PIB
- Sent signals vector : 1 for received
signals
- Mask vector : 1 for masked signals
- Handlers vector : a pointer to the Delivered
signals Mask
Table of
rotines

function to execute when the


signal is received (handler)
Interprocesses communication
1. Signals
Sending a signal
Sending the
#include<signal.h> signal n 2 to the
process
Pid=35
Kill(35,SIGINT)
int kill(int pid, int nsig)

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);

void sig_hand(int sig)


int main(void) { printf ("singal reçu %d \n",sig);}
int main(void) { signal(SIGINT, SIG_DFL);
{ signal(SIGINT, SIG_IGN); raise (SIGINT); int main(void)
raise (SIGINT); sleep (10); { signal(SIGINT, &sig_hand);
sleep (10); exit(0); raise (SIGINT);
exit(0); } sleep (10);
} exit(0);}

Executes a new routine


Ignores the signal

Restaures default routine


Interprocesses communication
1. Signals
Pause / sleep / alarm

sleep() and pause() could be used :


#include <unistd.h>
int pause(void) // blocks the calling process until the reception of a signal
int sleep(unsigned int n) // blocks the calling process for n seconds

#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

- Pipe is charged in the RAM and it has a capacity (MAX)


- Pipe allows just one-way communication.
- Pipe has two file descriptors one to write the other to read from the pipe
- When the pipe is empty the reading process is blocked (when a process reads a
charachter it will be deleted from the pipe)
- When the pipe is full the writng process is blocked
- Two types of pipes exist : named pipes and unamed pipes
- Unamed pipes permit communication between process and its children
- Named pipes permit communication between unrelated processes
Interprocesses communication
2. Pipes (unamed 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

- Creates an unamedpipe pipedes[0] : fd to read


- The pipe could be used by the pipedes[1] : fd to write
process and all its children created
#include <sys/types.h>
after the creation of the pipe.
#include <sys/stat.h>
- A process and its children could
communicate data through the read(fd, void *buf, size_t bufsize)
pipe.
write(fd, void *buf, size_t bufsize)
Interprocesses communication
2. Pipes (unamed pipes)
Example of Parent / Child communication with unamedpipe

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.

Shared memory provides a way by letting two or more processes


share a memory segment. With Shared Memory the data is only
copied twice – from input file into shared memory and from shared
memory to the output file.

Inter Process Communication through shared memory is a concept


where two or more process can access the common memory. And
communication is done via this shared memory where changes
made by one process can be viewed by another process.

Shared memory is the fastest mechanism for interprocess


communication.
Interprocesses communication
3. Shared Memory
Principales
Interprocesses communication
3. Shared Memory
Principales

Shared Memory creation

Shared Memory mapping

Shared Memory detachment

You might also like