Signals & Message queue Inter process mechanism in Linux system 3/24/2011 1.

Slides:



Advertisements
Similar presentations
Florida State UniversityCOP5570 – Advanced Unix Programming IPC mechanisms Pipes Sockets System V IPC –Message Queues –Semaphores –Shared Memory.
Advertisements

Inter-Process Communication: Message Passing Tore Larsen Slides by T. Plagemann, Pål Halvorsen, Kai Li, and Andrew S. Tanenbaum.
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Unix IPC and Synchronization. Pipes and FIFOs Pipe: a circular buffer of fixed size written by one process and read by another int pipe(int fildes[2])
XSI IPC Message Queues Semaphores Shared Memory. XSI IPC Each XSI IPC structure has two ways to identify it An internal (within the Kernel) non negative.
CS591 (Spring 2001) The Linux Kernel: Signals & Interrupts.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.
1 Signals COS Goals of Today’s Lecture Overview of signals  Notifications sent to a process  UNIX signal names and numbers  Ways to generate.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Signal Signal : - is a notification sent to a process to notify it of some event - interrupts whatever the process is doing and force it to handle a signal.
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
NCHU System & Network Lab Lab 10 Message Queue and Shared Memory.
Inter Process Communication. Introduction Traditionally describe mechanism for message passing between different processes that are running on some operating.
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
Inter-Process Communication: Message Passing
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Operating Systems Yasir Kiani. 22-Sep Agenda for Today Review of previous lecture Process management commands: bg, fg, ^Z, jobs, ^C, kill Thread.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Thread Synchronization with Semaphores
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Florida State UniversityCOP5570 – Advanced Unix Programming Today’s topics System V Interprocess communication (IPC) mechanisms –Message Queues –Semaphores.
* POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm Topics.
Slide 10-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 10.
1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
System calls for Process management
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Washington WASHINGTON UNIVERSITY IN ST LOUIS Core Inter-Process Communication Mechanisms (Historically Important) Fred Kuhns
Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth.
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
NCHU System & Network Lab Lab #8 Signals Operating System Lab.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
File descriptor table File descriptor (integer)File name 0stdin 1stdout 2stderr Use open(), read(), write() system calls to access files Think what happens.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
UNIX signals & pipes. UNIX Signals A UNIX signal corresponds to an event –It is raised by one process (or hardware) to call another process’s attention.
Interprocess Communication Mechanisms. IPC Signals Pipes System V IPC.
Operating Systems Recitation 4, April th, 2002 Signals.
Signals. Introduction r A signal is a mechanism for notifying a process that an event has occurred. m When a signal is sent to a process is normal execution.
Inter Process Comunication in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
KUKUM Real Time System Module #3 POSIX programming Lecture 2.
Today’s topic Environment variables Signal. The list of environment variables –try ‘env’ –Environment variables can be defined in shell setenv DISPLAY.
Slide 9-1 Copyright © 2004 Pearson Education, Inc. Inter process Communication Mechanisms  Allow arbitrary processes to exchange data and synchronize.
Today’s topics Signals and how to control the program behavior in handling signals. Terminal I/O.
Message Queues. Unix IPC Package ● Unix System V IPC package consists of three things – Messages – allows processes to send formatted data streams to.
ACCESS CONTROL. Components of a Process  Address space  Set of data structures within the kernel - process’s address space map - current status - execution.
System calls for Process management Process creation, termination, waiting.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
Operating Systems Summer Semester 2011 Practical Session 2, Signals 1.
UNIX signals.
G.Jyostna.
Signals What is a Signal?
Message queue Inter process communication primitive
Introduction to Distributed Programming System V IPC: Message Queues
Threads and Cooperation
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
CSC Advanced Unix Programming, Fall 2015
Message Queues.
Operating Systems Lecture 12.
Unix programming Term: Unit-V I PPT Slides
Signals Tanzir Ahmed CSCE 313 Fall 2018.
Inter-Process Communication ENCE 360
Signals.
Presentation transcript:

Signals & Message queue Inter process mechanism in Linux system 3/24/2011 1

What is signals ■ In short: Signals are a way of sending simple messages to processes. ■ Signals are usually used by the operating system to notify processes that some event occurred, without these processes needing to poll for the event. 3/24/2011 2

Definition : Signals ■ A signal is an asynchronous event which is delivered to a process. ■ Asynchronous means that the event can occur at any time ■ may be unrelated to the execution of the process ■ e.g. user types ctrl-C, when the system hangs 3/24/2011 3

Linux Signals ■ A LINUX signal corresponds to an event ■ It is raised by one process (or OSg) to call another process’s attention to an event ■ It can be caught (or ignored) by the subject process 3/24/2011 4

More on Signals ■ LINUX has a fixed set of signals ■ signal.h defines the signals in the OS ■ Each LINUX signal has an integer number and a symbolic name Defined in 3/24/2011 5

The command ‘kill –l’ lists all the signals that are available.

7 Signals Most signals have predefined meanings: ❑ sighup (HangUp): when a terminal is closed, the hangup signal is sent to every process in control terminal. ❑ sigint(interrupt): ask politely a process to terminate. ❑ sigquit(quit): ask a process to terminate and produce a core dump. ❑ sigkill (kill): force a process to terminate.

Signal Sources a process window manager shell command terminal driver memory management kernel other user processes SIGWINCH SIGKILL SIGINTSIGHUP SIGQUIT SIGALRM SIGPIPE SIGUSR1 8

Sending signals to process. (by keyboard) ■ Ctrl-c This causes the system to send an INT signal (SIGINT) to the running process which causes The process to immediately terminates. ■ Ctrl-z This causes the system to send an TSTP signal(SIGTSTP) to the running process this causes The process to Suspend execution. ■ Ctrl-\ This is same as ctrl-c but with better flexibility This sends ABRT Signal (SIGABRT) 3/24/2011 9

Signal transmission ■ Signal Generation: The kernel updates data structure of the destination process to represent that a new signal has been sent ■ Signal delivery: The kernel forces the destination process to react to the signal by changing its execution state, by starting the execution of a specified signal handler. ■ The mechanics consist of two distinct steps: 3/24/

Delivering a Signal ■ Actions performed upon Delivering a Signal 1. Explicitly ignore the signal 2. Execute the default action associated with the signal 3. Catch the signal by invoking a user defined signal handler function 3/24/

Ignoring the signal ■ Do_signal() simply continues with a new execution of the loop. ka= &current->sig->action[signr-1]; If (ka->sa.sa_handler== SIG_IGN ) Continue ; 3/24/

Default action for the signal ■ Execute the default action associated with the signal ■ The important actions are (based on signal type) ■ Terminate (kill) ■ Dump (core file for execution context is created) ■ Ignore (signal is ignored) ■ Stop (process is stopped. ■ Continue 3/24/

Pre-defined Signal Handlers ■ There are two pre-defined signal handler functions that we can use, instead of writing our own: ■ SIG_IGN: Causes the process to ignore the specified signal. For example, in order to ignore Ctrl-C completely, write this: signal(SIGINT, SIG_IGN); ■ SIG_DFL: Causes the system to set the default signal handler for the given signal signal(SIGTSTP, SIG_DFL); 3/24/

Executing the default action for the signal ■ The signals whose default action is stop may stop all processes in the thread group ■ The signals whose default action is dump may create a core file in the process working directory. ■ The default action of terminate is simply killing the process. 3/24/

Catching the signal ■ If a handler has been established for the signal, do_signal() function must enforces its execution. ■ Signal handlers are functions defined by User mode processes and included in the User mode Segment. ■ Handle_signal() function runs in kernel mode while signal handlers run in user mode. ■. 3/24/

Catching the signal 3/24/

Signal Handling ■ Use the signal handling library: signal.h ■ Then can use the signal call: #include void (*signal( int sig, void (*handler)(int))) (int) ; ■ signal returns a pointer to the PREVIOUS signal handler ■ #include typedef void Sigfunc(int); /* my defn */ Sigfunc *signal( int signo, Sigfunc *handler ); ■ Signal (SIGINT, handler); 3/24/

System calls related to signal handling ■ Kill() - sys call send signals to processes kill(pid,sig) #include Int kill (pid_t pid, int sig); // C library/wrapper function //returns 0 on success -1 on fail Sample programs 3/24/

Summary A signal is an asynchronous event which is delivered to a process Signals are a way of sending simple messages to processes Signal generation and delivery Various system calls related to signals 3/24/

Conclusion ❑ Knowledge of the signaling mechanism and familiarity with signal-related functions help one write programs more efficiently. ❑ In case of an error or any anomaly during the execution of a program, the kernel can use signals to notify the process. ❑ Signals also have been used to communicate and synchronize processes and to simplify inter-process communications (IPCs). 3/24/

Message Queue ■ Message queue. ■ A linked list of messages stored within the kernel and identified by a message queue identifier. ■ Every message has a type field, and a nonnegative length, and the actual data bytes. ■ Msgsnd puts a message at the end of the queue ■ Msgrcv gets a message, may not follow FIFO order (can be based on type) Operating Systems: A Modern Perspective, Chapter 9

Refined IPC Mechanism OS manages the mailbox space More secure message system Info to be shared Info copy Address Space for p 0 Address Space for p 1 Message Mailbox for p 1 send function receive function OS Interface send(… p 1, …); receive(…);

msgget ▪ Returns a message descriptor to be used by other system calls msgsnd ▪ Sends a message msgrcv ▪ Receives a message msgctl ▪ Controls the message queue

Syntax Example #include Int msgget(key_t key, int msgflg); msgid = msgget((key_t)1234,IPC_CREAT | IPC_EXCL) ; if(msgid < 0) { printf(“Message queue already exists\n") ; msgid = msgget((key_t)1234,0666) ; }

Syntax: Example int msgsnd(int msqid, const void *msg_ptr, size_t msg_sz, int msgflg); struct msgbuf { long mtype; /* message type, must be > 0 */ char mtext[5]; /* message data */ }; mbuf.mtype =1 ; strcpy(mbuf.mtext,"hello") ; retval = msgsnd(msgid,&mbuf,5,0) ;

Syntax: Example int msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int msgflg); retval = msgrcv(msgid,&mbuf,5,1,0) ; if(retval < 0) { perror("msgrcv: ") ; }

Syntax: The msqid_ds structure has at least the following members: struct msqid_ds { uid_t msg_perm.uid; uid_t msg_perm.gid mode_t msg_perm.mode; } Int msgctl(int msg_id, int command, struct shmid_ds *buf);

retval = msgctl(msgid,IPC_RMID,NULL) ; if(retval < 0) { printf("removing the message queue had failed\n") ; } CommandDescription IPC_STAT To retrieve the values associated with the shared memory. IPC_SET Sets the values IPC_RMID Deletes the shared memory segment.

int main() { int msgid, retval ; struct msgbuf mbuf ; if(msgid = msgget((key_t)5678,IPC_CREAT | IPC_EXCL) ) < 0); { printf("msgget already created\n") ; msgid = msgget((key_t)5678,0666) ; } mbuf.mtype =1 ; strcpy(mbuf.mtext,"hello") ; // trying to send hello If( (retval = msgsnd(msgid,&mbuf,5,0)) <0){ perror("msgsnd: ") ; } return 0 ; }

int main() { int msgid, retval ; struct msgbuf mbuf ; If((msgid = msgget((key_t)5678,IPC_CREAT | IPC_EXCL))<0) { printf("msgget already created\n") ; msgid = msgget((key_t)5678,0666) ; } if((retval = msgrcv(msgid,&mbuf,5,1,0)) < 0){ perror("msgrcv: ") ; } printf("message from process 1 is %s\n",mbuf.mtext) ; return 0 ; }

Pros ▪ Lies in the kernel space ▪ Doesn’t require any external synchronization. Cons ▪ Limit in the size of data to be sent ▪ Over all size limit in the complete system