U of I CS 241 - System Programming Timers & Signals

Unformatted text preview:

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 FebOutlineReview: SchedulingSignalsSignal/Timer Handlerspause(), sigwait()POSIX:TMR Timersclock_gettime()nanosleep()Scheduling (methods for MP2)Scheduler is another process that runs and schedules the other processes on a systemRound-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 ExampleSchedule the following threads with quantum of 2Scheduler only runs at quantum, so threads can overrunWhat 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 takenCTRL-C sends SIGINTDivision by Zero sends SIGFPESIGUSR1, SIGUSR2 we can define ourselvesUnix kill sends signals to processeskill –s <signal> <pid>C function pthread_kill sends to threadspthread_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 maskSIG_UNBLOCK removes set from current maskSIG_SETMASK sets current mask to be setoset will store the previous signal maskSignal Masks in Threads(Section 13.5)pthread_sigmaskTakes same parameters as sigprocmaskOnly has effect on the sigmask for a single threadSignal masks inherited during thread creationNeed to use in MP2Block the timer signal in non-scheduler threadsBlock resume signal in non-scheduler threads(potentially) Block non-timer signals in scheduler threadEffect of a Generated SignalTable 8.1 shows default actions for signalsSIGKILL/SIGSTOP cannot be changedEvery other signal we pick what action to takeLet it take default actionBlock/ignore it indefinitely (using sigmask)Setup a signal handler for itFunction that is executed when the process/thread receives the signalsigactionSetting up Signal Handlers(Section 8.4)Use a struct sigactionSimilarly to threads, we need to craft a function with a particular signaturesa_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 handlersa_maskNot necessary for MP2sa_handler vs sa_sigaction(Section 9.4)Cannot use both the handler and the sigaction inside the structBoth handlers are called in the same manner, but the latter receives extra informationCause of the signal (info->si_code)SI_USER – user created signal with raise/abort/kill/etcSI_TIMER – a POSIX:RTS timer expiredetcSetting 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_handlerSIG_DFL represents the default action (Table 8.1)SIG_IGN will make the process ignore the signalNot too useful with threads, as the signal will be ignored for all threadsNeed to use pthread_sigmask as we did earlierCan use sigaction to get old sigactionAs in example 8.15, if the handler was previously the default handler, now set the signal to be ignoredTimer HandlersWe will be using POSIX:TMR timersBy default they send the SIGALRM signalSetup a signal handler for SIGALRMBam! 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/ignoredIf a signal is generated (and does not terminate the process) before pause() is called, pause() will never see itIf we use sigmask to block the signal until pause() is called, it will be queued until we remove itHowever, pause() will sit waiting for the signal that is blocked; it will never check the queuepause() only returns if called before the signalEnter sigwait()(Section 8.5.3)Takes as parameter a sigset corresponding to which signals it should wait forBlock the signals firstsigwait() will remove a signal from the queue that is in its sigsetMust also pass a reference to an integer for it to store signal that was removedCANNOT PASS NULLsigwait(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

U of I CS 241 - System Programming Timers & Signals

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 System Programming Timers & Signals
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 System Programming Timers & Signals 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 System Programming Timers & Signals 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?