Signals & Timers CS241 Discussion Section Spring 2009 Week 6.

Slides:



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

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.
CS591 (Spring 2001) The Linux Kernel: Signals & Interrupts.
CSE 451: Operating Systems Section 6 Project 2b; Midterm Review.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
SignalsSignals. What is a Signal? A signal is a notification that some event has occurred. Usually a signal is sent to a process asynchronously and whatever.
Project 2 Roadmap. Background – Context Switching One processor and multiple threads running concurrently – How?!! Give each thread a small time quantum.
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)
Timers Timer – Keeps track of the passage of time Simple Timer – Measures elapsed time, reporting it when queried Interval Timer – –Generates an interrupt.
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.
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
Chapter 5. TCP Client-Server Example. Contents –Introduction –TCP Echo Server –TCP Echo Client –Normal Startup and Termination –Posix Signal Handling.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks II.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
1GWU CS 259 Brad Taylor Spring 2004 Systems Programming Meeting 5: Signals, Time, Timers, and Token Rings.
* POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm Topics.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Timers and Clocks.
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.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks II.
Concurrency & Context Switching Process Control Block What's in it and why? How is it used? Who sees it? 5 State Process Model State Labels. Causes of.
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.
SignalsSignals. What is a Signal? A signal is a notification that some event has occurred. Usually a signal is sent to a process asynchronously and whatever.
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.
CSC Advanced Unix Programming, Fall, 2008 Welcome back to UNIX System Programming! Monday, September 22, class 5.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks.
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.
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.
Chapter 8 Signals Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
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)
Signals and daemon processes Prepared by : Department of CSE Engineered for Tomorrow Course code: 10CS62.
CS 241 Section Week #10 (04/01/10) ‏. Topics This Section  MP5 Overview  Signals.
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.
CSE 451: Operating Systems Section 6 Project 2b. Midterm  Scores will be on Catalyst and midterms were handed back on Friday(?) in class  Talk to Ed,
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Operating Systems Practical Session 3, Signals 1.
Operating Systems Inter-Process Communication Signals Moti Geva
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo
G.Jyostna.
Signals What is a Signal?
Chapter 5. TCP Client-Server Example
Unix Process Management
HW1 and Synchronization & Queuing
CS 3733 Operating Systems Topics: Signals (USP Chapter )
UNIT-IV Process and Signals
CSC Advanced Unix Programming, Fall 2015
Signals Tanzir Ahmed CSCE 313 Fall 2018.
Signals.
Presentation transcript:

Signals & Timers CS241 Discussion Section Spring 2009 Week 6

Outline  SMP4  Signals: processes and threads  Blocking & waiting on signals  Handling signals  Timers

SMP4 3 scheduling functions: –new_job() –job_finished() –quantum_expired() Job queue Statistic functions –response_time(), wait_time(), turnaround_time() Note about memory –valgrind –MALLOC_CHECK

Review: signals Asynchronous notification to a process indicating some action should be taken Sending signals to a process: kill - int kill(pid_t pid, int sig); We can signal individual threads, too: int pthread_kill(thread_t tid, int sig);

What can we do with signals? Handle them! –Default or Ignore –Custom function with sigaction Block them! –Delay delivery with masks –Then we can sigwait to get them.

Lots of signal functions #include int sigemptyset(sigset_t *set); int sigfillset(sigset_t *set); int sigaddset(sigset_t *set, int signo); int sigdelset(sigset_t *set, int signo); int sigismember(const sigset_t *set, int signo); int sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict oset) int sigaction(int signo, const struct sigaction *act, struct sigaction *oact); int sigwait(const sigset_t *restrict sigmask, int *restrict signo);

Process Signal Masks Setting SIGINT to be blocked if ((sigemptyset(&set) == -1) || (sigaddset(&set, SIGINT) == -1)) perror(“Failed init signal set”); else if (sigprocmask(SIG_BLOCK, &set, &oldset) == -1) perror(“Failed to block SIGINT”); SIG_BLOCK adds set to current mask oldset will store the previous signal mask

Thread Signal Masks pthread_sigmask(): Takes same parameters as sigprocmask Only affects the signal mask of a single thread Signal mask is inherited on thread creation

Signal Handlers Allow us to change what happens when a signal is received void handler(int signo) { … } struct sigaction act; act.sa_flags = 0; act.sa_handler = handler; // additional signals blocked in the handler sigemptyset(&act.sa_mask); sigaction(SIGUSR1, &act, NULL); sa_handler can also be set to SIG_DFL (default) or SIG_IGN (ignore)

sa_handler vs. sa_sigaction We can get additional information about the signal void handler(int signo, siginfo_t* info, void* context); act.sa_flags = SA_SIGINFO; // fill sa_sigaction instead of sa_handler act.sa_sigaction = handler; Extra information contains, e.g., the source of the signal ( info- >si_code ): SI_USER – user-created signal (with abort, kill, etc.) SI_TIMER – a POSIX:RTS timer expired etc.

Example 1: Return of Hello World See d6-example1.c and d6-example1-soln.c Another “hello world” variant Prints out “Hello” and “World” periodically Let’s change the code to stop the hello thread when receiving SIGUSR1 and the world thread when receiving SIGUSR2. Hint 1: we do not have to change the signal handler Hint 2: what about the main thread?

pause() Waits for any signal that is not blocked/ignored But if a signal is generated before pause() is called, pause() will never see it If we use the sigmask to block the signal until pause() is called, it will be queued until we remove it However, pause() will just sit there waiting for the signal that is blocked; it will never check the queue In summary: pause() only returns if called before the signal is generated!

sigwait() Takes as parameter a sigset corresponding to which signals it should wait for You should block the signals first sigwait() will remove a signal from the queue that is in its sigset Must also pass a pointer to an integer for it to store signal that was removed sigwait(sigset_t *set, int *signo);

Timers Using POSIX:TMR timers… Send the SIGALRM signal to the process If we set up a signal handler for SIGALRM, we have a programmable timer! !! Signals sent for timers or interrupts need to be unblocked for the thread that will be receiving them !!

Accessing the clock The POSIX:TMR extension allows us to get and set time from the real-time clock struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ } Timers need two of these: one for how long from now to begin, another for how often to generate the interrupt (timer interval) struct itimerspec { struct timespec it_interval; /* period */ struct timespec it_value; }

Setting a timer Create the timer timer_t timerid; timer_create(CLOCK_REALTIME, NULL, &timerid); Set up the structs (fire first at 5 s, then every 2.5 s) struct itimerspec value; value.it_interval.tv_sec = 2; value.it_interval.tv_nsec = L; value.it_value.tv_sec = 5; value.it_value.tv_nsec = 0; Start the timer timer_settime(timerid, 0, &value, NULL);

Resetting (or disabling) a timer How do we turn off a timer? Simple: just “set” it to 0 We can also restart a timer Just call timer_settime with the same parameters as before; the timer will be reset Or pass in a different time to change its behavior

Example 2: Tick tock See d6-example2.c Let’s see how timers are used in this simple example program #include gcc –o p4 ds6-p4.c –lrt

Timing your code clock_gettime() fills in a struct timespec with elapsed time in nanoseconds since the epoch (Jan 1, 1970) Difference between two structs can time a function/action Useful to keep track of how long threads are waiting or executing struct timespec tend, tstart; clock_gettime(CLOCK_REALTIME, &tstart); function_to_time(); clock_gettime(CLOCK_REALTIME, &tend); double timediff = tend.tv_sec – tstart.tv_sec +((double)tend.tv_nsec – tstart.tv_nsec)/1e9;

Example 3 (d6-example3.c) Windows users will sometimes press Ctrl-C (for copy to clipboard), inadvertently killing a UNIX process. Let’s change the SIGINT handler to kill the process only if the user really means it! I.e., presses Ctrl-C three times within a 5-second “tick” Hint: Use sa_sigaction And let’s only count signals sent by the kernel (based on keyboard input) info->si_code == SI_KERNEL

Example 4 (d6-example4.c) Counting signals Use sigwait() to count how many times a process receives SIGUSR1 or SIGUSR2 Don’t forget to block them first!

Example 5 (d6-example5.c) Time how long 1e5, 1e6, etc. iterations of an empty for loop take to execute Time how long 1e5, 1e6, etc. sched_yields take to execute