Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.

Similar presentations

Presentation on theme: "Operating Systems Course Hebrew University Spring 2007 Signals & User Thread."— Presentation transcript:

1 Operating Systems Course Hebrew University Spring 2007 Signals & User Thread

2 Signals Bach 7.2 Signals are notifications sent to a process in order to notify the process of events. –Kernel. –Processes (system call kill) The kernel send a signal by setting a bit in the field of the process table entry. A process can remember different types of signals, but not the number of signals from each type.

3 Handling Signals The kernel handles signals in the context of the process that receives them so process must run to handle signals. There are three case for handling signals: –The process exits. (default action) signal(SIGINT, SIG_DFL); –The process ignores. signal(SIGTSTP, SIG_IGN); –The process execute particular function. oldfun = signal(signum,newfun); Most signals may be handled by the process, but there are a few signals that the process cannot catch, and cause the process to terminate. –For example: KILL and STOP.

4 Summary 1.Each signal may have a signal handler, which is a function that gets called when the process receives that signal. 2.When the signal is sent to the process, the operating system "forces" it to call the signal handler function. 3.When that signal handler function returns, the process continues execution from wherever it happened to be before the signal was received, as if this interruption never occurred.

5 Signal Handlers - Example #include #include //symbolic name void catch_int(int sig_num) { signal(SIGINT, catch_int); //install again! printf("Don't do that\n"); fflush(stdout); } int main(int argc, char* argv[]) { signal(SIGINT, catch_int); for ( ;; ) pause();//wait till receives a signal. }

6 Avoiding Signal Races - Masking Signals The occurrence of a second signal while the signal handler function executes. –The second signal can be of different type than the one being handled, or even of the same type. –Thus, we should take some precautions inside the signal handler function, to avoid races. The system also contains some features that will allow us to block signals from being processed.

7 Sigprocmask() The system call allows to specify a set of signals to block, and returns the list of signals that were previously blocked. sigprocmask(int how, const sigset_t *set, sigset_t *oldset) how : –Add ( SIG_BLOCK ) –Delete ( SIG_UNBLOCK ) –Set ( SIG_SETMASK ). 2.const sigset_t *set : –The set of signals. 3.sigset_t *oldse: –If this parameter is not NULL, then it'll contain the previous mask.

8 Sigaction The sigaction() function allows the calling process to examine and/or specify the action to be associated with a specific signal. int sigaction(int sig, struct sigaction *new_act, struct sigaction *old_act);

9 Sigaction Cont. A new signal mask is calculated and installed only for the duration of the signal-catching function, which includes the signal being delivered. Once an action is installed for a specific signal, it remains installed until another action is explicitly requested.

10 #include void termination_handler (int signum) { //do something! } int main (void) { struct sigaction new_action, old_action; new_action.sa_handler = termination_handler; sigemptyset (&new_action.sa_mask); new_action.sa_flags = 0; sigaction (SIGINT, NULL, &old_action); if (old_action.sa_handler != SIG_IGN) sigaction (SIGINT, &new_action, NULL); }

11 User Threads Implementation

12 Attributes A thread can possess an independent flow of control and be schedulable because it maintains its own: –Stack. –Registers. (CPU STATE!) –Access to the resources of its process.

13 Kernel Level Threads Kernel level threads (lightweight processes) –thread management done by the kernel

14 User Level Threads User level threads –implemented as a thread library which contains the code for thread creation, termination, scheduling and switching –kernel sees one process and it is unaware of its thread activity.

15 POSIX Threads In the UNIX environment a thread: Exists within a process and uses the process resources. Has its own independent flow of control as long as its parent process exists and the OS supports it. May share the process resources with other threads that act equally independently (and dependently). Dies if the parent process dies - or something similar (user thread).

Download ppt "Operating Systems Course Hebrew University Spring 2007 Signals & User Thread."

Similar presentations

Ads by Google