You are on page 1of 25

Operating Systems (OS)

Process Management

Dr. Najar Yousra


Email : yousra.najar@isi.utm.tn
Yousra.najar@medtech.tn

Med Tech First Year Eng 2022/2023

1
Road Map
 Introduction to operating systems

 Process Management

 InterprocessusCommunication and
Synchronisation

 Memory Management

 File System Management


CS303 Dr. Najar Yousra
Fall 2021 2
Week 5 (05/10/2022)
 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 writer C
programs handling them.

3
Outlines of the section

• Interprocesses communication
• Signals
• Pipes
• Shared Memory
1. 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 information
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 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 Process


PIB
the Process PCB are three :

- Sent signals vector : 1 for Table of

received signals
Delivered
signals Mask rotines

- Mask vector : 1 for masked


signals
- Handlers vector : a pointer to
the function to execute when the
signal is received (handler)
Interprocesses communication
1. Signals
Sending a signal

#include<signal.h>

int kill(int pid, int nsig)

returns 0 or -1

Sending the signal n 2 to the process with id = 35

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

void sig_hand(int sig)


int main(void) { printf (« received signals %d \n",sig);}
{ signal(SIGINT, SIG_DFL);
raise (SIGINT); int main(void)
sleep (10); { signal(SIGINT, &sig_hand);
exit(0); raise (SIGINT);
} sleep (10);
exit(0);}

int main(void) Executes a new routine


{ signal(SIGINT, SIG_IGN);
raise (SIGINT);
Restaures default routine
sleep (10);
exit(0);
}

Ignores the signal


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

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

- 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 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.

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
Interprocesses communication
3. Shared Memory

Shared Memory creation

Shared Memory mapping

Shared Memory detachment

You might also like