Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Inter-Process Communication Signals Moti Geva

Similar presentations


Presentation on theme: "Operating Systems Inter-Process Communication Signals Moti Geva"— Presentation transcript:

1 Operating Systems Inter-Process Communication Signals Moti Geva geva.biu@gmail.com

2 Introduction to signals Signals are asynchronous events The sources for a signal Another process The process itself The kernel Why are signal sent? IPC Kernel notification

3 Introduction to signals(2) There may be various reasons for sending a signal Notifying a process about an error Segmentation fault (SIGSEGV), Illegal instruction (SIGILL), Floating point error (SIGFPE)... Killing or stopping/continuing a process SIGKILL, SIGSTOP/SIGCONT IPC SIGUSR1, SIGUSR2 The full list can be found in ‘man 7 signal’

4 An example main() { char *a; *a=0; } $ gcc -o sig1 sig1.c $ sig1 Segmentation fault (core dumped)

5 Handling a signal Every signal has a signal handler which is called upon signal occurrence Usually the default signal handler will terminate the execution of the process Many signals can either Be handled and prevent termination Must get process attention (IPC) Run pre-termination procedures Some signals can’t be handled at all SIGKILL, SIGSTOP

6 Installing a signal handler SYNOPSIS #include typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); RETURN VALUE The signal() function returns the previous value of the signal handler, or SIG_ERR on error Special signal handlers SIG_IGN – ignore signal handler SIG_DFL – default signal handler

7 Signal default actions Term - Default action is to terminate the process. Ign - Default action is to ignore the signal. Core - Default action is to terminate the process and dump core. Stop - Default action is to stop the process.

8 Signal numbers & default actions Signal Value Action Comment or death of controlling process SIGINT2 Term Interrupt from keyboard SIGQUIT 3 Core Quit from keyboard SIGILL 4 Core Illegal Instruction SIGABRT 6 Core Abort signal from abort(3) SIGFPE 8 Core Floating point exception SIGKILL 9 Term Kill signal SIGSEGV 11 Core Invalid memory reference SIGPIPE 13 Term Broken pipe: write to pipe with no readers SIGALRM 14 Term Timer signal from alarm(2) SIGTERM 15 Term Termination signal SIGUSR1 30,10,16 Term User-defined signal 1 SIGUSR2 31,12,17 Term User-defined signal 2 SIGCHLD 20,17,18 Ign Child stopped or terminated SIGCONT 19,18,25 Cont if stopped SIGSTOP 17,19,23 Stop Stop process SIGTSTP 18,20,24 Stop Stop typed at tty SIGTTIN 21,21,26 Stop tty input for background process SIGTTOU 22,22,27 Stop tty output for background processabortalarm

9 Signal numbers & default actions SIGPOLL Term Pollable event (Sys V). Synonym of SIGIO SIGPROF 27,27,29 Term Profiling timer expired SIGSYS 12,-,12 Core Bad argument to routine (SVID) SIGTRAP 5 Core Trace/breakpoint trap SIGURG 16,23,21 Ign Urgent condition on socket (4.2 BSD) SIGVTALRM 26,26,28 Term Virtual alarm clock (4.2 BSD) SIGXCPU 24,24,30 Core CPU time limit exceeded (4.2 BSD) SIGXFSZ 25,25,31 Core File size limit exceeded (4.2 BSD) SIGEMT 7,-,7 Term SIGSTKFLT -,16,- Term Stack fault on coprocessor (unused) SIGIO 23,29,22 Term I/O now possible (4.2 BSD) SIGCLD -,-,18 Ign A synonym for SIGCHLD SIGPWR 29,30,19 Term Power failure (System V) SIGINFO 29,-,- A synonym for SIGPWR SIGLOST -,-,- Term File lock lost SIGWINCH 28,28,20 Ign Window resize signal (4.3 BSD, Sun) SIGUNUSED -,31,- Term Unused signal (will be SIGSYS)

10 Example #include void funct(); main() { char *a; signal(SIGSEGV, &funct); *a=0; } void funct() { printf("haha\n"); exit(1); } $ gcc -o sig2 sig2.c $ sig2 haha

11 Special signals Using a signal handler function for a signal is called "catching the signal". There are two special signals that can’t be caught SIGKILL SIGSTOP Therefore if a process gets one of these signals the default handler will be called SIGKILL 9 Term Kill signal SIGSTOP 23 Stop Stop process

12 Signal sending A process can send a signal to another process using the kill() system call SYNOPSIS #include #include sys/types.hsignal.h int kill(pid_t pid, int sig); RETURN VALUE On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

13 The pid in kill() The kill system call can be used to send any signal to any process group or process. If pid is positive, then signal sig is sent to pid. If pid equals 0, then sig is sent to every process in the process group of the current process. If pid equals -1, then sig is sent to every process except for process 1 (init). If pid is less than -1, then sig is sent to every process in the process group -pid. If sig is 0, then no signal is sent, but error checking is still performed.

14 Example #include void funct() { printf("haha\n"); exit(1); } main() { signal(11, &funct); while(1); } $ gcc -o sig3 sig3.c $ sig3 & [1] 19919 $ kill -SEGV 19919 $ haha [1] Exit 1 sig3 $ sig3 & [1] 19961 $ kill -11 19961 $ haha [1] Exit 1 sig3

15 Self signaling raise - send a signal to the current process SYNOPSIS #include signal.h int raise(int sig); DESCRIPTION The raise() function sends a signal to the current process. It is equivalent to kill(getpid(), sig); RETURN VALUE 0 on success, nonzero for failure.

16 Alarming alarm - set an alarm clock for delivery of a signal SYNOPSIS #include unistd.h unsigned int alarm(unsigned int seconds); DESCRIPTION alarm arranges for a SIGALRM signal to be delivered to the process in seconds seconds. If seconds is zero, no new alarm is scheduled. In any event any previously set alarm is cancelled. RETURN VALUE alarm returns the number of seconds remaining until any previously scheduled alarm was due to be delivered, or zero if there was no previously scheduled alarm.

17 Waiting for a signal... SYNOPSIS #include unistd.h int pause(void); DESCRIPTION The pause library function causes the invoking process (or thread) to sleep until a signal is received that either terminates it or causes it to call a signal-catching function. RETURN VALUE The pause function only returns when a signal was caught and the signal-catching function returned. In this case pause returns -1, and errno is set to EINTR.

18 Advanced signal handling The signal() system call allows only simple signal handling In order to a more complex signal handler you may want to use sigaction() system call There are also the sigprocmask, sigpending, sigsuspend system calls

19 sigaction() SYNOPSIS #include signal.h int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); If act is non-null, the new action for signal signum is installed from act. If oldact is non-null, the previous action is saved in oldact. struct sigaction { void (*sa_handler)(int); void (*sa_sigaction)(int, siginfo_t *, void *); sigset_t sa_mask; int sa_flags; void (*sa_restorer)(void); }

20 sigaction() (2) sa_flags SA_NOCLDSTOP If signum is SIGCHLD, do not receive notification when child processes stop (i.e., when child processes receive one of SIGSTOP, SIGTSTP, SIGTTIN or SIGTTOU). SA_ONESHOT or SA_RESETHAND Restore the signal action to the default state once the signal handler has been called. SA_ONSTACK Call the signal handler on an alternate signal stack provided by sigaltstack(2). If an alternate stack is not available, the default stack will be used.sigaltstack SA_RESTART Provide behaviour compatible with BSD signal semantics by making certain system calls restartable across signals. SA_NOMASK or SA_NODEFER Do not prevent the signal from being received from within its own signal handler. SA_SIGINFO The signal handler takes 3 arguments, not one. In this case, sa_sigaction should be set instead of sa_handler. (The sa_sigaction field was added in Linux 2.1.86.)

21 The siginfo_t parameter siginfo_t { int si_signo; /* Signal number */ int si_errno; /* An errno value */ int si_code; /* Signal code */ pid_t si_pid; /* Sending process ID */ uid_t si_uid; /* Real user ID of sending process */ int si_status; /* Exit value or signal */ clock_t si_utime; /* User time consumed */ clock_t si_stime; /* System time consumed */ sigval_t si_value; /* Signal value */ int si_int; /* POSIX.1b signal */ void * si_ptr; /* POSIX.1b signal */ void * si_addr; /* Memory location which caused fault */ int si_band; /* Band event */ int si_fd; /* File descriptor */ }

22 The siginfo_t parameter (2) si_signo, si_errno and si_code are defined for all signals. The rest of the struct may be a union, so that one should only read the fields that are meaningful for the given signal. kill(2), POSIX.1b signals and SIGCHLD fill in si_pid and si_uid. SIGCHLD also fills in si_status, si_utime and si_stime. kill si_int and si_ptr are specified by the sender of the POSIX.1b signal. SIGILL, SIGFPE, SIGSEGV and SIGBUS fill in si_addr with the address of the fault. SIGPOLL fills in si_band and si_fd.

23 Signals mask SYNOPSIS #include signal.h int sigprocmask(int how, const sigset_t *set, sigset_t *oldset); Description The sigprocmask call is used to change the list of currently blocked signals. The behaviour of the call is dependent on the value of how, as follows. SIG_BLOCK - The set of blocked signals is the union of the current set and the set argument. SIG_UNBLOCK - The signals in set are removed from the current set of blocked signals. It is legal to attempt to unblock a signal which is not blocked. SIG_SETMASK - The set of blocked signals is set to the argument set. If oldset is non-null, the previous value of the signal mask is stored in oldset.

24 Pending signals SYNOPSIS #include signal.h int sigpending(sigset_t *set); Description The sigpending call allows the examination of pending signals (ones which have been raised while blocked). The signal mask of pending signals is stored in set. /* A `sigset_t' has a bit for each signal. */ # define _SIGSET_NWORDS(1024 / (8 * sizeof (unsigned long int))) typedef struct { unsigned long int __val[_SIGSET_NWORDS]; } __sigset_t;

25 Waiting for a signal...again... SYNOPSIS #include signal.h int sigsuspend(const sigset_t *mask); Description The sigsuspend call temporarily replaces the signal mask for the process with that given by mask and then suspends the process until a signal is received.


Download ppt "Operating Systems Inter-Process Communication Signals Moti Geva"

Similar presentations


Ads by Google