CS241 Operating Systems CPU Scheduling (6)ContentAdministrative NotesPOSIX:XSI Interval TimersItimersSleep FunctionsNanosleepSlide 8Slide 9Slide 10POSIX: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 processSlide 17POSIX:TMR Interval Timerstimer_createtimer_deleteDisplay Message every 2 seconds (R&R:P.328)Slide 22Slide 23Slide 24Using a POSIX:TMR to time functiontmrtimer.c (R&R P329)SummaryCS241 Operating SystemsCPU Scheduling (6)Klara NahrstedtLecture 152/22/200601/13/19 CS 241 - System Programming, Klara Nahrstedt2ContentXSI Interval TimersSleep functions Real-time SignalsTimers–POSIX TRM Interval TimersSummary01/13/19 CS 241 - System Programming, Klara Nahrstedt3Administrative NotesMP2 on scheduling is runningQuiz 4 on Friday, 2/24/06Material presented in this lecture covers material in the book R&R 9.2, 9.3, 9.4 and 9.501/13/19 CS 241 - System Programming, Klara Nahrstedt4POSIX:XSI Interval TimersGenerates 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);01/13/19 CS 241 - System Programming, Klara Nahrstedt5Itimersit_interval = 0 then timer won’t restart01/13/19 CS 241 - System Programming, Klara Nahrstedt6Sleep Functions#include <unistd.h>unsigned sleep(unsigned seconds);sleep interacts with SIGALRM01/13/19 CS 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 SIGALRM01/13/19 CS 241 - System Programming, Klara Nahrstedt8#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;}Print asterix every 2 seconds01/13/19 CS 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 */01/13/19 CS 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 signals01/13/19 CS 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; }01/13/19 CS 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.\n", diftime, diftime/(double)MILLION); return 0;}01/13/19 CS 241 - System Programming, Klara Nahrstedt13Real Time Signals#include <signal.h>struct sigaction { void (*sa_handler) (int); /* SIG_DFL, SIG_IGN, or pointer to function*/ sigset_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);POSIX:XSI and POSIX:RTS have expanded signal-handling capabilities1. To include the queuing of signals 2. To include passing information to signal handlers01/13/19 CS 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 as int sival_int; /*integer or pointer can be transmitted to the signal handler by the generator of the signal */ void *sival_ptr;01/13/19 CS 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 queuedMultiple signals created by kill may not be queued but they are if created by sigqueueSigno should be non-zero. If zero, will check for errors – can use this to see if pid valid, see p322SA_SIGINFO is in sa_flags of struct sigaction, then signal queued, otherwise signal sent at least once, but it is not queued.01/13/19 CS 241 - System Programming, Klara Nahrstedt16Send a queued signal to a process#include <signal.h>#include <stdio.h>#include <stdlib.h>int main(int argc, 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; }01/13/19 CS 241 - System
View Full Document