#include int main(int argc, char *argv[]) { signal(SIGHUP, SIG_IGN); execvp(argv[1], argv+1); return 0; } 2. Parent and child print ordered integers using signals: #include #include #include int even = 2, odd = 1; pid_t pid; void handler() { kill(get"> #include int main(int argc, char *argv[]) { signal(SIGHUP, SIG_IGN); execvp(argv[1], argv+1); return 0; } 2. Parent and child print ordered integers using signals: #include #include #include int even = 2, odd = 1; pid_t pid; void handler() { kill(get">
Nothing Special   »   [go: up one dir, main page]

Operating Systems - Interprocess Communication - Signals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Operating Systems (OS)

Process Management
Inter Process Communication

Dr. Najar Yousra


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

Med Tech First Year Eng 2023/2024

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 4 (25/09/2023)
 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 events. 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 Process


PIB
in the Process PCB are three :

- Sent signals vector : 1 for Delivered Mask


Table of

received signals signals 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) : sends the signal nsig to the process with the pid
passed as argument.

It returns 0 or -1

Sending the signal number 2 (SIGINT) to the process with


P1 pid = 35

Pid=35 P2
kill(SIGINT, 35)
kill(2, 35)
SIGINT

When P2 receives the signal SIGINT, it will execute the hundler associated
with it (which is ending its execution).
Interprocesses communication
1. Signals
Handling signal : Changing the behaviour of a process when it receives a 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);
sleep (10); Restaures default
routine
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 any 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
Application 1

Write a C program that executes a command with its arguments after protecting the
process from the SIGHUP signal.

Application 2

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.
Use signals.To manage them.

You might also like