CS241 Operating SystemsCPU Scheduling (6)ContentAdministrative NotesPOSIX:XSI Interval TimersItimersSleep FunctionsNanosleepPOSIX:XSI Interval Timer used to time function (R&R: P. 320)POSIX:XSI Interval Timer used to time function (R&R:P.320)Real Time SignalsSIGINFO_T structurePOSIX:RTS signal queuingSend a queued signal to a processSend a queued signal to a processPOSIX:TMR Interval Timerstimer_createtimer_deleteDisplay Message every 2 seconds (R&R:P.328)Using a POSIX:TMR to time functiontmrtimer.c (R&R P329)SummaryCS241 Operating SystemsCPU Scheduling (6)Klara NahrstedtLecture 152/22/20062/22/2006CS 241 - System Programming, Klara Nahrstedt2Contentz XSI Interval Timersz Sleep functions z Real-time Signalsz Timers–POSIX TRM Interval TimerszSummary2/22/2006CS 241 - System Programming, Klara Nahrstedt3Administrative NoteszMP2 on scheduling is runningz Quiz 4 on Friday, 2/24/06z Material presented in this lecture covers material in the book R&R 9.2, 9.3, 9.4 and 9.52/22/2006CS 241 - System Programming, Klara Nahrstedt4POSIX:XSI Interval TimerszGenerates a signal after a time interval repeatedly – periodicstruct timeval it_value; /* time until next expiration*/struct timeval it_interval; /* value to reload into the timer */Timeval structrue has fields for seconds and microseconds#include <sys/time.h>int getitimer(int which, struct itimerval *value);int setitimer(int which, const struct itimerval *restrict value, struct itimerval *restrict ovalue);2/22/2006CS 241 - System Programming, Klara Nahrstedt5Itimerszit_interval = 0 then timer won’t restart2/22/2006CS 241 - System Programming, Klara Nahrstedt6Sleep Functions#include <unistd.h>unsigned sleep(unsigned seconds);sleep interacts with SIGALRM2/22/2006CS 241 - System Programming, Klara Nahrstedt7Nanosleep#include <time.h>int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);Resolution of CLOCK_REALTIME is of order 10msDoes not affect SIGALRM2/22/2006CS 241 - System Programming, Klara Nahrstedt8Print asterix every 2 seconds#include <errno.h>#include <signal.h>#include <stdio.h>#include <unistd.h> #include <sys/time.h>/* ARGSUSED */static void myhandler(int s) {char aster = '*';int errsave;errsave = errno;write(STDERR_FILENO, &aster, 1);errno = errsave;}2/22/2006CS 241 - System Programming, Klara Nahrstedt9static int setupinterrupt(void) { /* set up myhandler for SIGPROF */struct sigaction act;act.sa_handler = myhandler;act.sa_flags = 0;return (sigemptyset(&act.sa_mask) || sigaction(SIGPROF, &act, NULL));}/* Create signal handler */static int setupitimer(void) /* set ITIMER_PROF for 2-second intervals*struct itimerval value;value.it_interval.tv_sec = 2;value.it_interval.tv_usec = 0;value.it_value = value.it_interval;return (setitimer(ITIMER_PROF, &value, NULL));}/* Create timer to generate signals */2/22/2006CS 241 - System Programming, Klara Nahrstedt10int main(void) {if (setupinterrupt()) {perror("Failed to set up handler for SIGPROF");return 1;}if (setupitimer() == -1) {perror("Failed to set up the ITIMER_PROF interval timer"); return 1;}for ( ; ; ); /* execute rest of main program here */}Set up signal and handler to create *s. Set up interval timer to create signals2/22/2006CS 241 - System Programming, Klara Nahrstedt11POSIX:XSI Interval Timer used to time function (R&R: P. 320)#include <stdio.h>#include <sys/time.h>#define MILLION 1000000Lvoid function_to_time(void);int main(void) {long diftime;struct itimerval ovalue, value;ovalue.it_interval.tv_sec = 0;ovalue.it_interval.tv_usec = 0;ovalue.it_value.tv_sec = MILLION; /* a large number */ovalue.it_value.tv_usec = 0;if (setitimer(ITIMER_VIRTUAL, &ovalue, NULL) == -1) {perror("Failed to set virtual timer");return 1;}2/22/2006CS 241 - System Programming, Klara Nahrstedt12POSIX:XSI Interval Timer used to time function (R&R:P.320)function_to_time(); /* timed code goes here */if (getitimer(ITIMER_VIRTUAL, &value) == -1) {perror("Failed to get virtual timer");return 1;}diftime = MILLION*(ovalue.it_value.tv_sec - value.it_value.tv_sec) +ovalue.it_value.tv_usec - value.it_value.tv_usec;printf("The function_to_time took %ld microseconds or %f seconds.\diftime, diftime/(double)MILLION);return 0;}2/22/2006CS 241 - System Programming, Klara Nahrstedt13Real Time SignalsPOSIX:XSI and POSIX:RTS have expanded signal-handling capabilities1. To include the queuing of signals 2. To include passing information to signal handlers#include <signal.h>struct sigaction {void (*sa_handler) (int); /* SIG_DFL, SIG_IGN, or pointer to functiosigset_t sa_mask; /*additional signals to be blocked *//* during execution of handler*/int sa_flags; /* special flags and options*/void(*sa_sigaction) (int, siginfo_t *, void *); /* realtime handler*//* Handler looks like*/void func(int signo, siginfo_t * info, void *context);2/22/2006CS 241 - System Programming, Klara Nahrstedt14SIGINFO_T structureint si_signo; /* signal number */int si_code; /* cause of signal */union sigval si_value; /* signal value*/Causes of signal (si_code): •SI_USER (kill or raise generated signal), •SI_QUEUE (sigqueue caused signal)•SI_TIMER (interval timer caused signal)•others (SI_ASYNCIO, SI_MESGQ) si_value defined only in POSIX:RTS and if si_code isSI_QUEUE, SI_TIMER, …union sigval is defined asint sival_int; /*integer or pointer can be transmitted to the signal handler by the generator of the signal */void *sival_ptr;2/22/2006CS 241 - System Programming, Klara Nahrstedt15POSIX:RTS signal queuing#include <signal.h>int sigqueue(pid_t pid, int signo, const union sigval value); sigqueue extends kill function that permits signals to be queued Multiple signals created by kill may not be queued but they are if created by sigqueue Signo should be non-zero. If zero, will check for errors – can use this to see if pid valid, see p322 SA_SIGINFO is in sa_flags of struct sigaction, then signal queued, otherwise signal sent at least once, but it is not queued.2/22/2006CS 241 - System Programming, Klara Nahrstedt16Send a queued signal to a process#include <signal.h>#include <stdio.h>#include <stdlib.h>intmain(intargc, char *argv[]) {int pid;int signo;int sval;union sigval value;if (argc != 4) {fprintf(stderr, "Usage: %s pid signal value\n", argv[0]);return 1; }2/22/2006CS 241 - System Programming, Klara Nahrstedt17Send a queued signal to a processpid = atoi(argv[1]);signo = atoi(argv[2]);sval = atoi(argv[3]);fprintf(stderr, "Sending signal
View Full Document