DOC PREVIEW
U of I CS 241 - System Programming

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 241 System Programming More Timers, Signals Midterm reviewOutlineTimers: sigeventAlarm signal – sending SIGUSR1Starting/stopping timersa_sigaction (Section 9.4)Signal handler for multiple signalsTimers & threads: SIGEV_THREADTimer thread examplePassing values to signal handlersSlide 11ProcessesMemory LayoutForking processes (Chains & Fans)ThreadpThreadsSynchronization - reviewCPU SchedulingQueuing TheoryAnalysis of Single Server QueueQueuing ExampleResource allocation & Wait for GraphsSlide 23RecapCS 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);POSIX function Descriptionpthread_cancelterminate another threadpthread_createcreate a threadpthread_detachset thread to release resourcespthread_equaltest two thread IDs for equalitypthread_exitexit a thread without exiting processpthread_killsend a signal to a threadpthread_joinwait for a threadpthread_selffind out own thread IDSynchronization - 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 =  wqL =  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


View Full Document

U of I CS 241 - System Programming

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
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 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 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?