CS 241 System ProgrammingMore Timers, SignalsMidterm reviewDiscussion Section 627 Feb – 2 MarOutline Signals & Timers revisited Midterm Review Processes Threads Synchronization SchedulingTimers: sigevent POSIX:TMR timers by default send SIGALRM Use struct sigevent to change this if neededstruct sigevent {int sigev_notify; /* notification type */int sigev_signo; /* signal number */union sigval sigev_value; /* signal value */} sigev_notify SIGEV_NONE - No notification from timer SIGEV_SIGNAL - Default: Send a signal SIGEV_THREAD - Create a new thread and run it union sival Either int sival_int or void* sival_ptrAlarm signal – sending SIGUSR1timer_t timerid;void createTimer(){struct sigevent se;se.sigev_signo = SIGUSR1;if (timer_create(CLOCK_REALTIME, &se, &timerid)==-1){fprintf(stderr,"Failed to create timer\n”);exit(-1);}}Starting/stopping timervoid setTimer(int seconds){struct itimerspec timervals;timervals.it_value.tv_sec = seconds;timervals.it_value.tv_nsec = 0;timervals.it_interval.tv_sec = seconds;timervals.it_interval.tv_nsec = 0;if (timer_settime(timerid, 0, &timervals, NULL) == -1){fprintf(stderr,"Failed to start timer\n");exit(-1);}}sa_sigaction (Section 9.4)char* code = NULL;switch (info->si_code){case SI_USER: code = "USER"; break;case SI_QUEUE: code = "QUEUE"; break;case SI_TIMER: code = "TIMER"; break;case SI_ASYNCIO: code = "ASYNCIO“; break;case SI_MESGQ: code = "MESGQ"; break;default: code = "Unknown"; break;}Signal handler for multiple signalsvoid signalCatcher(int signo, siginfo_t* info, void* context){char message[30];char* code = NULL;switch (info->si_code) { <..SNIP..from previous slide..> }fprintf(stderr, "Signal=%3d(%s), si_signo=%3d, si_code=%d(%s), si_value=%d\n", signo, getSignal(signo), info->si_signo, info->si_code, code, info>si_value.sival_int);/* Actual action depends on signal */switch (signo){case SIGQUIT /*Ctrl-|*/: setTimer(0); break; /* Stop timer */case SIGINT /*Ctrl-C*/: setTimer(1); break; /* Start timer */default: break;}}Timers & threads: SIGEV_THREADvoid createTimer() {struct Value* value = (struct Value*)malloc(sizeof(struct Value));value->a = 100;value->b = 100.001;struct sigevent se;se.sigev_signo = SIGUSR1;se.sigev_notify = SIGEV_THREAD;se.sigev_notify_function = threadfunc0;se.sigev_value.sival_ptr = (void*)value;if (timer_create(CLOCK_REALTIME, &se, &timerid)== -1){fprintf(stderr,"Failed to create timer");exit(-1);}fprintf(stderr, "Timer Created\n");} At each timer event, create a thread and run threadfunc0Timer thread examplevoid threadfunc0(union sigval sv){struct Value* value = (struct Value*)sv.sival_ptr;int mycounter = counter++;fprintf(stderr, "Running thread: %ud, counter = %d, value->a = %d, value->b = %f\n",pthread_self(), mycounter, value->a, value->b);int i;for (i = 0; i < 10; i++){sleep (1);fprintf(stderr, "Running thread: %ud, counter = %d\n", pthread_self(), mycounter);}}Passing values to signal handlers kill – Sends a signal to a pid sigqueue – Queues & sends a signal with a value to a pid union sigval value can be int or void*int main(int argc, char* argv[]){int pid = atoi(argv[1]);int signal = getSignal(argv[2]);int sendValue = atoi(argv[3]);kill(pid, signal); /* Send signal to pid */union sigval value;value.sival_int = sendValue;/* Queues & sends signal with value */sigqueue(pid, signal, value);}Outline Signals & Timers revisited Midterm Review Processes Threads Synchronization SchedulingProcesses Process Instance of a program (currently executing) Has alterable state, variables, memory Process ID States New Running Blocked Ready Done Context SwitchesMemory LayoutForking processes (Chains & Fans) Process Chainfor (i = 1; i < n; i++)if (childpid = fork())break; Process Fanfor (i = 1; i < n; i++)if ((childpid = fork()) <= 0)break;Thread ADT representing thread of execution within a process Avoids context switches Has its own execution stack, program counter value, register set, and state Share process address space & process resources “Lightweight process”pThreadsvoid* threadfunction(void* arg);find out own thread IDpthread_selfwait for a threadpthread_joinsend a signal to a threadpthread_killexit a thread without exiting processpthread_exittest two thread IDs for equalitypthread_equalset thread to release resourcespthread_detachcreate a threadpthread_createterminate another threadpthread_cancelDescriptionPOSIX functionSynchronization - review Atomic operations Critical sections Mutual exclusion Mutex Semaphore Conditional Variable Read-write locksCPU Scheduling Algorithms First-Come First-Serve (FCFS) Shortest Job First (SJF) Round Robin (RR) Priority Preemptive & Non-preemptive Metrics Waiting Time Turnaround TimeQueuing Theory λ = arrival rate μ = service rate Wq= mean time a customer spends in the queue W = mean time a customer spends in the system Lq= number of customers in queue L = number of customers in the system Lq= λ wqL = λ W ( Little's theorem)Analysis of Single Server Queueμλρ=λμ−=1Wλμρ−=qWρρ−=12qL•Server Utilization:•Time in System:•Time in Queue:•Number in Queue (Little):Queuing ExampleArrival 3 job/sec from StartArrival 2 jobs/sec from Event queueProcessor 1 services 2 jobs/secProcessor 2 services 4 jobs/sec Compute the following: Utilization Time in system Time in queue Length of queue Now, what if processor 2 services 3 jobs/sec?Resource allocation & Wait for GraphsResource Allocation GraphResource allocation & Wait for GraphsResource Allocation Graph Corresponding Wait For GraphRecap Signals & Timers Midterm review Processes Threads Synchronization
View Full Document