CS 241 System Programming Timers & SignalsOutlineScheduling (methods for MP2)Scheduling ExampleSignals (Table 8.1)Signal Masks in Processes (Section 8.3, Example 8.10)Signal Masks in Threads (Section 13.5)Effect of a Generated SignalSetting up Signal Handlers (Section 8.4)sa_handler vs sa_sigaction (Section 9.4)Setting up Signal Handlers (Example 8.16)Another way to ignore signals (Example 8.15, Example 8.18)Timer Handlerspause() (Section 8.5.1, Exercise 8.21, Exercise 8.22)Enter sigwait() (Section 8.5.3)Counting signals (Program 8.11)Accessing the ClockSetting up the Timer (Example 9.16)Resetting (or disabling) a TimerUsing the timer effectively (MP2 advice)clock_gettime() (Example 9.8)nanosleep() (Section 9.2)RecapCS 241 System ProgrammingTimers & SignalsDiscussion Section 520 Feb – 23 FebOutlineReview: SchedulingSignalsSignal/Timer Handlerspause(), sigwait()POSIX:TMR Timersclock_gettime()nanosleep()Scheduling (methods for MP2)Scheduler is another process that runs and schedules the other processes on a systemRound-robin – When the scheduler runs, it suspends the current thread, and starts the one at the front of the queue.Priority – Except when preempted, scheduler keeps the present thread running until it is done. When a thread of higher priority enters the queue, next time the scheduler runs, that thread will preempt the current thread.Scheduling ExampleSchedule the following threads with quantum of 2Scheduler only runs at quantum, so threads can overrunWhat happens under Round-Robin scheduling? Priority?Average wait time, turn-around time?What effect does preemption have?Process Duration Priority # Arrival TimeP1 6 4 2P2 4 1 1P3 7 2 6P4 5 3 0Signals(Table 8.1)System signals sent to processes/threads indicating some action should be takenCTRL-C sends SIGINTDivision by Zero sends SIGFPESIGUSR1, SIGUSR2 we can define ourselvesUnix kill sends signals to processeskill –s <signal> <pid>C function pthread_kill sends to threadspthread_kill(thread_t tid, int signo)Signal Masks in Processes(Section 8.3, Example 8.10)Setting SIGINT to be blocked:if ((sigemptyset(&set) == -1) || (sigaddset(&set, SIGINT) == -1)) perror(“Failed init signal set”);else if (sigprocmask(SIG_BLOCK, &set, &oset) == -1) perror(“Failed to block SIGINT”);SIG_BLOCK adds set to current maskSIG_UNBLOCK removes set from current maskSIG_SETMASK sets current mask to be setoset will store the previous signal maskSignal Masks in Threads(Section 13.5)pthread_sigmaskTakes same parameters as sigprocmaskOnly has effect on the sigmask for a single threadSignal masks inherited during thread creationNeed to use in MP2Block the timer signal in non-scheduler threadsBlock resume signal in non-scheduler threads(potentially) Block non-timer signals in scheduler threadEffect of a Generated SignalTable 8.1 shows default actions for signalsSIGKILL/SIGSTOP cannot be changedEvery other signal we pick what action to takeLet it take default actionBlock/ignore it indefinitely (using sigmask)Setup a signal handler for itFunction that is executed when the process/thread receives the signalsigactionSetting up Signal Handlers(Section 8.4)Use a struct sigactionSimilarly to threads, we need to craft a function with a particular signaturesa_handler = handler, sa_flags = 0void handler(int signo)sa_sigaction = handler, sa_flags = SA_SIGINFOvoid handler(int signo, siginfo_t*, void* context)Can also specify a set of additional signals to block while executing the handlersa_maskNot necessary for MP2sa_handler vs sa_sigaction(Section 9.4)Cannot use both the handler and the sigaction inside the structBoth handlers are called in the same manner, but the latter receives extra informationCause of the signal (info->si_code)SI_USER – user created signal with raise/abort/kill/etcSI_TIMER – a POSIX:RTS timer expiredetcSetting up Signal Handlers(Example 8.16)Catching CTRL-C and running handlervoid catchctrlc(int signo) { write(STDERR_FILENO, “CTRL-C\n”, 7);}struct sigaction act;act.sa_handler = catchctrlc;act.sa_flags = 0;if ((sigemptyset(&act.sa_mask) == -1) || (sigaction(SIGINT, &act, NULL) == -1)) perror(“Failed to set handler for CTRL-C”);Another way to ignore signals(Example 8.15, Example 8.18)There are a few special values for sa_handlerSIG_DFL represents the default action (Table 8.1)SIG_IGN will make the process ignore the signalNot too useful with threads, as the signal will be ignored for all threadsNeed to use pthread_sigmask as we did earlierCan use sigaction to get old sigactionAs in example 8.15, if the handler was previously the default handler, now set the signal to be ignoredTimer HandlersWe will be using POSIX:TMR timersBy default they send the SIGALRM signalSetup a signal handler for SIGALRMBam! Now we have a timer handler.!! Signals sent for timers or interrupts need to be unblocked for the thread that will be receiving them !!Or we can use a special function, sigwait()pause()(Section 8.5.1, Exercise 8.21, Exercise 8.22)Waits for any signal that is not blocked/ignoredIf a signal is generated (and does not terminate the process) before pause() is called, pause() will never see itIf we use sigmask to block the signal until pause() is called, it will be queued until we remove itHowever, pause() will sit waiting for the signal that is blocked; it will never check the queuepause() only returns if called before the signalEnter sigwait()(Section 8.5.3)Takes as parameter a sigset corresponding to which signals it should wait forBlock the signals firstsigwait() will remove a signal from the queue that is in its sigsetMust also pass a reference to an integer for it to store signal that was removedCANNOT PASS NULLsigwait(sigset_t *set, int *signo)Counting signals(Program 8.11)int main(void) { int signalcount = 0, signo, signum = SIGUSR1; sigset_t sigset; if ((sigemptyset(&sigset) == -1) || (sigaddset(&sigset, signum) == -1) || (sigprocmask(SIG_BLOCK, &sigset, NULL) == -1)) perror("Failed to block signals before sigwait"); fprintf(stderr, "This process has ID %ld\n", (long)getpid()); for ( ; ; ) { if (sigwait(&sigset, &signo) == -1) { perror("Failed to wait using sigwait"); return 1; } signalcount++;
View Full Document