Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Signals & Message queue Inter process mechanism in Linux system 3/24/2011 1."— Presentation transcript:

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

2 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

3 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

4 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

5 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

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

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

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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 Catching the signal 3/24/2011 17

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

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

20 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

21 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

22 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

23 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(…);

24 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

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

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

27 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: ") ; }

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

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

30 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 ; }

31 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 ; }

32 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


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

Similar presentations


Ads by Google