Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.

Slides:



Advertisements
Similar presentations
Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
Advertisements

Week Fourteen Agenda Announcements Final Exam True/False -100 questions (1 point per question) Multiple Choice - 40 questions (2 points per question)
15-213, Fall 06 Outline Shell Lab Processes Signals.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
Page 1 Task Control: Signals and Alarms Havilland and Salama’s Unix Systems Programming B. Ramamurthy.
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)
Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.
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
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
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
COMP5102 Lecture 4 Operating Systems (OS) Inter-process Communication phones off (please)
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Operating Systems Chapter 2
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Operating Systems CSE 411 CPU Management Sept Lecture 9 Instructor: Bhuvan Urgaonkar.
* 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,
System calls for Process management
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 (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 *
UNIX signals & pipes. UNIX Signals A UNIX signal corresponds to an event –It is raised by one process (or hardware) to call another process’s attention.
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.
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.
Linux/UNIX Programming APUE (Signals) [Ch. 10] 최미정 강원대학교 컴퓨터과학전공.
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/
ACCESS CONTROL. Components of a Process  Address space  Set of data structures within the kernel - process’s address space map - current status - execution.
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)
System calls for Process management Process creation, termination, waiting.
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, Abdelzaher, Caccamo1 Signals.
Operating Systems: Inter-Process Communication Pål Halvorsen 13 March 2016 INF1060: Introduction to Operating Systems and Data Communication.
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
Unix Process Management
OS – Process Creation and Destruction
Linux/UNIX Programming
Exceptional Control Flow Part II
Exceptional Control Flow
CSC Advanced Unix Programming, Fall 2015
Signals Tanzir Ahmed CSCE 313 Fall 2018.
Exceptional Control Flow
Inter-Process Communication ENCE 360
Task Control: Signals and Alarms Chapter 7 and 8
Signals.
Presentation transcript:

Signals Hua LiSystems ProgrammingCS2690Signals

Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example program Other signal functions Hua LiSystems ProgrammingCS2690Signals

When a process terminates abnormally it usually tries to send a signal indicating what went wrong. C programs (and UNIX) can trap these for diagnostics. Also user can use signals to specify communication. Hua LiSystems ProgrammingCS2690Signals

Signals are software/hardware generated interrupts that are sent to a process when a event happens. Signals can be synchronously generated by an error in an application, such as SIGFPE (an arithmetic exception, e.g., divided-by-0) and SIGSEGV (invalid memory reference), but most signals are asynchronous. Signals can be posted to a process when the system detects a software event, such as a user entering an interrupt (Press DELETE Key, Control-C) or a kill request from another process. Hua LiSystems ProgrammingCS2690Signals

Signals can also be come directly from the OS kernel when a hardware event such as a bus error or an illegal instruction is encountered. The system defines a set of signals that can be posted to a process. Most signals cause termination of the receiving process if no action is taken by the process in response to the signal. Some signals stop the receiving process and other signals can be ignored. Hua LiSystems ProgrammingCS2690Signals

Each signal has a default action which is one of the following: (see Figure 10.1, page 266 for details, text book) The signal is discarded after being received The process is terminated after the signal is received A core file is written, then the process is terminated Stop the process after the signal is received Hua LiSystems ProgrammingCS2690Signals

Each signal defined by the system falls into one of five classes: 1.Hardware conditions 2.Software conditions 3.Input/output notification 4.Process control 5.Resource control Hua LiSystems ProgrammingCS2690Signals

Macros are defined in header file for common signals. These include: SIGHUP 1 /* hangup, disconnect is detected by the terminal interface */ SIGINT 2 /* interrupt (DELETE, Control-C*/ SIGQUIT 3 /* generated when type terminal quit key Control-backslash (Ctl-\) */ SIGILL 4 /* illegal instruction */ SIGABRT 6 /* generated by calling abort function*/ SIGKILL 9 /* kill any process */ SIGALRM 14 /* alarm clock */ SIGCONT 19 /* continue a stopped process */ SIGCHLD 20 /* whenever a process terminates or stops, this signal is sent to the parent */ Signals can be numbered from 0 to 31. Hua LiSystems ProgrammingCS2690Signals

Sending Signals -- kill(), raise() There are two common functions used to send signals int kill(int pid, int signal) - a system call that send a signal to a process, pid. If pid is greater than zero, the signal is sent to the process whose process ID is equal to pid. If pid is 0, the signal is sent to all processes, except system processes. kill() returns 0 for a successful call, -1 otherwise and sets errno accordingly. Hua LiSystems ProgrammingCS2690Signals

int raise(int sig) -- sends the signal sig to the executing program. raise() actually uses (call) kill() to send the signal to the executing program: kill(getpid(), sig); There is also a UNIX command called kill that can be used to send signals from the command line - see man pages for details. NOTE: that unless caught or ignored, the kill signal ( SIGKILL 9 ) terminates the process. Hua LiSystems ProgrammingCS2690Signals

Signal Handling -- signal() An application program can specify a function called a signal handler to be invoked when a specific signal is received. When a signal handler is invoked on receipt of a signal, it is said to catch the signal. A process can deal with a signal in one of the following ways: 1.The process can let the default action happen 2.The process can block the signal (some signals cannot be ignored) 3.the process can catch the signal with a handler. Hua LiSystems ProgrammingCS2690Signals

Signal function definition: Void (*signal(int sig, void (*func)(int)) ) (int) -- that is to say the function signal() will call the func functions if the process receives a signal sig. Signal function requires two arguments and returns a pointer to a function (which takes an integer argument, the final (int), and returns nothing (void)). (The signal handler is passed a signal number to the function func) refer page The first argument, sig, is an integer (signal). The second argument, func, is a pointer to a function that takes a single integer argument and return nothing. Hua LiSystems ProgrammingCS2690Signals

func() can have three values: SIG_DFL : a pointer to a system default function SID_DFL(), which will terminate the process upon receipt of sig. SIG_IGN: a pointer to system ignore function SIG_IGN() which will disregard the sig action (UNLESS it is SIGKILL). A function address: a user specified function. SIG_DFL and SIG_IGN are defined in signal.h (standard library) header file. Hua LiSystems ProgrammingCS2690Signals

Thus to ignore a ctrl-c command from the command line. we could do: signal(SIGINT, SIG_IGN); /*IGN: ignore*/ To reset system so that SIGINT causes a termination at any place in our program, we would do: signal(SIGINT, SIG_DFL); /*DFL: default action*/ Hua LiSystems ProgrammingCS2690Signals

Lets write a program to trap a ctrl-c (SIGINT) but not quit on this signal. We have a function sigproc() that is executed when we trap a ctrl-c. We will also set another function to quit the program if it traps the SIGQUIT signal (CTL-\ (backslash) ) so we can terminate our program: Hua LiSystems ProgrammingCS2690Signals

#include #name: sig_eg2.c cs2690/hua/program void sigproc(void); void quitproc(void); main() { signal(SIGINT, sigproc); signal(SIGQUIT, quitproc); printf("ctrl-c disabled use ctrl- \\to quit \n"); for(;;); /* infinite loop */ } Hua LiSystems ProgrammingCS2690Signals

Hua LiSystems ProgrammingCS2690Signals void sigproc() { printf("you have pressed ctrl-c \n"); } void quitproc() { printf("ctrl- \\pressed to quit\n"); exit(0); /* normal exit status */ }

Hua LiSystems ProgrammingCS2690Signals sig_talk.c -- complete example program An example of communicating process using signals. Let us now write a program that communicates between child and parent processes using kill() and signal(). fork() creates the child process from the parent. The pid can be checked to decide whether it is the child (== 0) or the parent (pid = child process id). The parent can then send messages to child using the pid and kill(). The child picks up these signals with signal() and calls appropriate functions.

Hua LiSystems ProgrammingCS2690Signals /* sig_talk2.c --- Example of how 2 processes can talk */ /* to each other using kill() and signal() */ /* We will fork() 2 process and let the parent send a few */ /* signals to it`s child */ /* cc sig_talk2.c -o sig_talk2 (cs2690/hua/program)*/ #include void sighup(); /* routines child will call upon sigtrap */ void sigint(); void sigquit();

Hua LiSystems ProgrammingCS2690Signals main() { int pid; /* get child process */ if ((pid = fork()) < 0) { perror("fork"); exit(1); } if (pid == 0) { /* child */ signal(SIGHUP,sighup); /* set function calls */

Hua LiSystems ProgrammingCS2690Signals signal(SIGINT,sigint); signal(SIGQUIT, sigquit); for(;;); /* loop for ever */ } else /* parent */ { /* pid hold id of child */ printf("\nPARENT: sending SIGHUP\n\n"); kill(pid,SIGHUP); sleep(3); /* pause for 3 secs */ printf("\nPARENT: sending SIGINT\n\n"); kill(pid,SIGINT); sleep(3); /* pause for 3 secs */ printf("\nPARENT: sending SIGQUIT\n\n"); kill(pid,SIGQUIT); sleep(3); }

Hua LiSystems ProgrammingCS2690Signals void sighup() { printf("CHILD: I have received a SIGHUP\n"); } void sigint() { printf("CHILD: I have received a SIGINT\n"); } void sigquit() { printf("My DADDY has Killed me!!!\n"); exit(0); }

Hua LiSystems ProgrammingCS2690Signals Other signal functions There are a few other functions defined in signal.h: int sighold(int sig) -- adds sig to the calling process's signal mask int sigrelse(int sig) -- removes sig from the calling process's signal mask int sigignore(int sig) -- sets the disposition of sig to SIG_IGN int sigpause(int sig) -- removes sig from the calling process's signal mask and suspends the calling process until a signal is received