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

Preview:

DESCRIPTION

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

Citation preview

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

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

windowmanager

shell command

terminaldriver

memorymanagement

kernel

other userprocesses

SIGWINCH

SIGKILL

SIGINT SIGHUPSIGQUIT

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 causesThe process to immediately terminates.■ Ctrl-zThis 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 flexibilityThis 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/2011 10

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/2011 11

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/2011 12

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/2011 13

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/2011 14

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/2011 15

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/2011 16

Catching the signal

3/24/2011 17

Signal Handling

■Use the signal handling library: signal.h

■Then can use the signal call:

#include <signal.h>

void (*signal( int sig, void (*handler)(int))) (int) ;

■signal returns a pointer to the PREVIOUS signal handler

■ #include <signal.h> typedef void Sigfunc(int); /* my defn */ Sigfunc *signal( int signo, Sigfunc *handler );■ Signal (SIGINT, handler);

3/24/2011 18

System calls related to signal handling

■Kill() - sys call send signals to processes kill(pid,sig)

#include<sys/types.h>#include<signal.h>Int kill (pid_t pid, int sig); // C library/wrapper function//returns 0 on success -1 on fail

Sample programs

3/24/2011 19

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/2011 20

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/2011 21

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

Operating Systems: A Modern Perspective, Chapter 9

Refined IPC Mechanism• OS manages the mailbox space• More secure message system

Info to beshared Info copy

Address Space for p0 Address Space for p1

MessageMessage

Message

Mailbox for p1

send function receive function

OS Interfacesend(… p1, …); 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<sys/msg.h>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.gidmode_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") ;}

Command DescriptionIPC_STAT To retrieve the values associated with the

shared memory.IPC_SET Sets the valuesIPC_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

Recommended