DOC PREVIEW
U of I CS 241 - Signals & Timers

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Signals & TimersOutlineReview: signalsWhat can we do with signals?Lots of signal functions!Process Signal MasksThread Signal MasksSignal Handlerssa_handler vs. sa_sigactionExample 1: Return of Hello Worldpause()sigwait()TimersAccessing the clockSetting a timerResetting (or disabling) a timerExample 2: Tick tockTiming your codeProblem 1 (d6-problem1.c)Problem 2 (d6-problem2.c)Problem 3 (d6-problem3.c)Signals & TimersCS241 Discussion Section Fall 2007Week 6OutlineSignals: processes and threadsBlocking & waiting on signalsHandling signalsTimersReview: signalsAsynchronous notification to a process indicating some action should be takenSending signals to a process:kill -<signal> <pid>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 <signal.h>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 MasksSetting SIGINT to be blockedif ((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 maskoldset will store the previous signal maskThread Signal Maskspthread_sigmask():Takes same parameters as sigprocmaskOnly affects the signal mask of a single threadSignal mask is inherited on thread creationSignal HandlersAllow us to change what happens when a signal is receivedvoid handler(int signo) { … }struct sigaction act;act.sa_flags = 0;act.sa_handler = handler;// additional signals blocked in the handlersigemptyset(&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_sigactionWe can get additional information about the signalvoid handler(int signo, siginfo_t* info, void* context);act.sa_flags = SA_SIGINFO;// fill sa_sigaction instead of sa_handleract.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 expiredetc.Example 1: Return of Hello WorldSee d6-example1.c and d6-example1-soln.cAnother “hello world” variantPrints out “Hello” and “World” periodicallyLet’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 handlerHint 2: what about the main thread?pause()Waits for any signal that is not blocked/ignoredBut if a signal is generated before pause() is called, pause() will never see itIf we use the sigmask to block the signal until pause() is called, it will be queued until we remove itHowever, pause() will just sit there waiting for the signal that is blocked; it will never check the queueIn summary: pause() only returns if called before the signal is generated!sigwait()Takes as parameter a sigset corresponding to which signals it should wait forYou should block the signals firstsigwait() will remove a signal from the queue that is in its sigsetMust also pass a pointer to an integer for it to store signal that was removedsigwait(sigset_t *set, int *signo);TimersUsing POSIX:TMR timers…Send the SIGALRM signal to the processIf 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 clockThe POSIX:TMR extension allows us to get and set time from the real-time clockstruct 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 timerCreate the timertimer_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 = 500000000L;value.it_value.tv_sec = 5;value.it_value.tv_nsec = 0;Start the timertimer_settime(timerid, 0, &value, NULL);Resetting (or disabling) a timerHow do we turn off a timer?Simple: just “set” it to 0We can also restart a timerJust call timer_settime with the same parameters as before; the timer will be resetOr pass in a different time to change its behaviorExample 2: Tick tockSee d6-example2.cLet’s see how timers are used in this simple example program#include <time.h>gcc –o p4 ds6-p4.c –lrtTiming your codeclock_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/actionUseful to keep track of how long threads are waiting or executingstruct 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;Problem 1 (d6-problem1.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 reall y 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_KERNELProblem 2 (d6-problem2.c)Counting signalsUse sigwait() to count how many times a process receives SIGUSR1 or SIGUSR2Don’t forget to block them first!Problem 3 (d6-problem3.c)Time how long 1e5, 1e6, etc. iterations of an empty for loop take to executeTime how long 1e5, 1e6, etc. sched_yields take to


View Full Document

U of I CS 241 - Signals & Timers

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Signals & Timers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Signals & Timers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Signals & Timers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?