1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”

Slides:



Advertisements
Similar presentations
Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
Advertisements

Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
15-213, Fall 06 Outline Shell Lab Processes Signals.
System Programming Project 2 Due : 4/19...?. Foreground & Background Foreground job Foreground job Only one at moment Only one at moment Having user’s.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
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)
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.
Section A (March 14) Outline Exceptions Process Signals Non-local jumps Reminders Lab4: Due Next Thursday TA: Kun Gao Shamelessly Modified from Minglong.
Signals, Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Exceptional Control Flow Part II Topics Process Hierarchy Signals CS213.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: 10/21/2013 Tommy Klein Section B.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
Introduction to Processes CS Intoduction to Operating Systems.
The process concept (section 3.1, 3.3 and demos)  Process: An entity capable of requesting and using computer resources (memory, CPU cycles, files, etc).
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,
* POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm Topics.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
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 *
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.
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.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: Monday, Oct. 21, 2013 Marjorie Carlson Section.
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)
CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail.
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.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
Process Manipulation. Process Manipulation in UNIX Basic process manipulation: creation, program loading, exiting, … fork(), exec(), wait(), exit() Process.
G.Jyostna.
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Signals What is a Signal?
Protection of System Resources
Unix Process Management
Exceptional Control Flow
Exceptional Control Flow
Practical Session 3, Signals
Exceptional Control Flow Part II
Exceptional Control Flow
CSC Advanced Unix Programming, Fall 2015
Signals Tanzir Ahmed CSCE 313 Fall 2018.
Exceptional Control Flow
Signals.
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Presentation transcript:

1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel” (OReilly) in the reference material section

2 Signal: An IPC Mechanism With pipes we communicated data With signal, we can communicate control command The only information is:  The number identifying the signal Interrupts a process and forces it to handle the event immediately kill –l can be used to view all the signals supported by your system

3 Sending signals to process From keyboard  Ctrl-C, Ctrl-Z etc From the command line  kill -  fg :On most shells, using the 'fg' command will resume execution of the process (that was suspended with Ctrl-Z), by sending it a CONT signal. Using the kill() system call Also used by the kill and fg command #include /* signal name macros, and the kill() prototype */ /* first, find my own process ID */ pid_t my_pid = getpid(); /* now that i got my PID, send myself the STOP signal. */ kill(my_pid, SIGSTOP);

4 Actions Performed upon Receiving a Signal There are three ways in which a process can respond to a signal: 1.Explicitly ignore the signal. 2.Execute the default action associated with the signal. 3.Catch the signal by invoking a corresponding signal-handler function.

5 Signal Handler Corresponding to each signal is a signal handler Called when a process receives a signal The function is called “asynchronously” When the signal handler returns the process continues, as if it was never interrupted Signal are different from interrupts as:  Interrupts are sent to OS by H/W  Signals are sent to a process by the OS, or by other processes  Note that signals have nothing to do with software interrupts, which are still sent by the hardware (the CPU itself, in this case).

6 Catching a Signal

7 The signal() System Call Used to set signal handler for a signal type

8Example void main() {/* set the INT (Ctrl-C) signal handler to 'catch_int' */ signal(SIGINT, catch_int); /* now, lets get into an infinite loop of doing nothing. */ for ( ;; ) pause(); } /* first, here is the signal handler */ void catch_int(int sig_num) { /* re-set the signal handler again to catch_int, for next time */ signal(SIGINT, catch_int); /* and print the message */ printf("Don't do that"); }

9 Signal Handlers Pre-defined Signal Handlers SIG_IGN:  Causes the process to ignore the specified signal.  To ignore Ctrl-C completely signal(SIGINT, SIG_IGN); SIG_DFL:  Causes the system to set the default signal handler for the given signal signal(SIGTSTP, SIG_DFL);

10 Race conditions The occurrence of a second signal while the signal handler function executes Might be of a different type then the one being handled Or even of the same type

11sigprocmask() Allows us to specify a set of signals to block Returns the list of signals that were previously blocked int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);

12sigprocmask() int how  SIG_BLOCK Add set to the set of currently blocked signals  SIG_UNBLOCK  The signals in set are removed from the current set of blocked signals.  SIG_SETMASK The set of blocked signals is set to the argument set. const sigset_t *set  The set of signals to be blocked, or to be added to the current mask, or removed from the current mask (depending on the 'how' parameter). sigset_t *oldset  If this parameter is not NULL, then it'll contain the previous mask.  Can be used later restore back the situation, before sigprocmask() was called.

13 Signal functions Define a new mask set sigset_t mask_set; Clear the set (i.e. make it contain no signal numbers) sigemptyset(&mask_set); Add a signal sigaddset(&mask_set, SIGTSTP); Remove the TSTP signal from the set sigdelset(&mask_set, SIGTSTP); Check if the TSTP signal is defined in our set if (sigismember(&mask_set, SIGINT) printf("signal INT is in our set\n"); else printf("signal INT is not in our set - how strange...\n"); Make the set contain ALL signals available on the system sigfillset(&mask_set)

14 Count_CTRL_C.c

15 #include /* standard I/O functions */ #include /* standard unix functions, like getpid()*/ #include /* signal name macros, and the signal() prototype */ /* first, define the Ctrl-C counter, initialize it with zero. */ int ctrl_c_count = 0; #defineCTRL_C_THRESHOLD5 int main(int argc, char* argv[]) { /* set the Ctrl-C and Ctrl-Z signal handlers */ signal(SIGINT, catch_int); signal(SIGTSTP, catch_suspend); /* enter an infinite loop of waiting for signals */ for ( ;; ) pause(); return 0; }

16 /* the Ctrl-C signal handler */ void catch_int(int sig_num) { sigset_t mask_set;/* used to set a signal masking set. */ sigset_t old_set;/* used to store the old mask set. */ /* mask any further signals while we're inside the handler. */ sigfillset(&mask_set); sigprocmask(SIG_SETMASK, &mask_set, &old_set); /* increase count, and check if threshold was reached */ ctrl_c_count++; if (ctrl_c_count >= CTRL_C_THRESHOLD) { char answer[30]; /* prompt the user to tell us if to really exit or not */ printf("\nRealy Exit? [y/N]: "); fflush(stdout);

17 gets(answer); if (answer[0] == 'y' || answer[0] == 'Y') { printf("\nExiting...\n"); fflush(stdout); exit(0); } else { printf("\nContinuing\n"); fflush(stdout); /* reset Ctrl-C counter */ ctrl_c_count = 0; } } /* restore the old signal mask */ sigprocmask(SIG_SETMASK, &old_set, NULL); }

18 /* the Ctrl-Z signal handler */ void catch_suspend(int sig_num) { sigset_t mask_set;/* used to set a signal masking set. */ sigset_t old_set;/* used to store the old mask set. */ /* mask any further signals while we're inside the handler. */ sigfillset(&mask_set); sigprocmask(SIG_SETMASK, &mask_set, &old_set); /* print the current Ctrl-C counter */ printf("\n\nSo far, '%d' Ctrl-C presses were counted\n\n", ctrl_c_count); fflush(stdout); /* restore the old signal mask */ sigprocmask(SIG_SETMASK, &old_set, NULL); }

19 Implementing Timers Using Signals alarm : set a process alarm clock #include unsigned alarm(unsigned sec); Sends the signal SIGALRM to the calling process after the sec number of seconds have elapsed If sec is 0, any previously made alarm request is canceled.

20 use-alarms_c.c

21 #include /* signal name macros, and the signal() prototype */ char user[40];/* buffer to read user name from the user */ int main(int argc, char* argv[]) { /* set a signal handler for ALRM signals */ signal(SIGALRM, catch_alarm); /* prompt the user for input */ printf("Username: "); fflush(stdout); /* start a 30 seconds alarm */ alarm(30); /* wait for user input */ gets(user); /* remove the timer, now that we've got the user's input */ alarm(0); printf("User name: '%s'\n", user); }`

22 /* define an alarm signal handler. */ void catch_alarm(int sig_num) { printf("Operation timed out. Exiting...\n\n"); exit(0); }