Operating Systems Inter-Process Communication Signals Moti Geva

Slides:



Advertisements
Similar presentations
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
Advertisements

2.3 InterProcess Communication (IPC) Part A. IPC methods 1. Signals 2. Mutex (MUTual EXclusion) 3. Semaphores 4. Shared memory 5. Memory mapped files.
1 Select and poll and Signals CS 241 April 6, 2012 University of Illinois.
CS591 (Spring 2001) The Linux Kernel: Signals & Interrupts.
CPSC 451 Editors and Systems Calls1 Minix editors Mined - (mined) is a simple screen editor. Elle - (elle) is a clone of Emacs. Elvis - (elvis, ex, vi)
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
CS Lecture 17 Outline Named pipes Signals Lecture 17
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.
B. RAMAMURTHY Pag e 1 Task Control: Signals and Alarms Chapter 7 and 8 7/2/2015.
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Process states inWindows 2000 and Linux Module 2.1.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
The kernel considers each program running on your system to be a process A process lives as it executes, with a lifetime that may be short or long A process.
* POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm Topics.
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R S I X Exception Handling.
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
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 and Signal Handling. Signals A predefined message sent between two processes or from the kernel to a process, or by a user to a process A software.
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.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
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.
CSC Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 22, class 5.
1 UNIX System Programming Signals. 2 Overview 1. Definition 2. Signal Types 3. Generating a Signal 4. Responding to a Signal 5. Common Uses of Signals.
KUKUM Real Time System Module #3 POSIX programming Lecture 2.
© 숙대 창병모 1 제 10 장 신호 (Signal). © 숙대 창병모 2 Contents 1. Signal Concepts 2. signal() 3. Interrupted System Calls 4. kill() /raise() 5. alarm() pause() 6.
Today’s topic Environment variables Signal. The list of environment variables –try ‘env’ –Environment variables can be defined in shell setenv DISPLAY.
Today’s topics Signals and how to control the program behavior in handling signals. Terminal I/O.
Signals & Message queue Inter process mechanism in Linux system 3/24/
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
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, Abdelzaher1 Signals.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
Operating Systems Practical Session 3, Signals 1.
Operating Systems Summer Semester 2011 Practical Session 2, Signals 1.
UNIX signals.
G.Jyostna.
Signals What is a Signal?
Task Control: Signals and Alarms Chapter 7 and 8
Linux/UNIX Programming
Practical Session 3, Signals
Threads and Cooperation
Exceptional Control Flow Part II
UNIT-IV Process and Signals
CSC Advanced Unix Programming, Fall 2015
Signals.
Signals Tanzir Ahmed CSCE 313 Fall 2018.
Exceptional Control Flow
Inter-Process Communication ENCE 360
Task Control: Signals and Alarms Chapter 7 and 8
CSE 451: Operating Systems Spring 2006 Module 4 Processes
Signals.
Presentation transcript:

Operating Systems Inter-Process Communication Signals Moti Geva

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

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’

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

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

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

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.

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

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)

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

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

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.

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.

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

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.

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.

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.

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

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

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 )

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 */ }

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.

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.

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;

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.