Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks.

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

CS 6560 Operating System Design Lecture 7: Kernel Synchronization Kernel Time Management.
Chapter 9 Times and Timers Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
System Files and Process Environment Password file Group file System identification Time Process environment.
I/O Multiplexing Road Map: 1. Motivation 2. Description of I/O multiplexing 3. Scenarios to use I/O multiplexing 4. I/O Models  Blocking I/O  Non-blocking.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX.
Page 1 Task Control: Signals and Alarms Chapter 7 and 8 B. Ramamurthy.
Timers Timer – Keeps track of the passage of time Simple Timer – Measures elapsed time, reporting it when queried Interval Timer – –Generates an interrupt.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
CS Lecture 17 Outline Named pipes Signals Lecture 17
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Signal Signal : - is a notification sent to a process to notify it of some event - interrupts whatever the process is doing and force it to handle a signal.
Timers Timer – Keeps track of the passage of time Simple Timer – Measures elapsed time, reporting it when queried Interval Timer – –Generates an interrupt.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 26P. 1Winter Quarter Handy Board Lecture.
Week 8 - Friday.  What did we talk about last time?  String to int conversions  Users and groups  Password files.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
4061 Session 17 (3/19). Today Time in UNIX Today’s Objectives Define what is meant when a system is called “interrupt-driven” Describe some trade-offs.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks II.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
Real-Time Systems Design1 Priority Inversion When a low-priority task blocks a higher-priority one, a priority inversion is said to occur Assume that priorities:
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Timers and Clocks.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 26P. 1Winter Quarter Handy Board Lecture 26.
Thread Implementations; MUTEX Reference on thread implementation –text: Tanenbaum ch. 2.2 Reference on mutual exclusion (MUTEX) –text: Tanenbaum ch
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks II.
Signals (Chap 10 in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l Univ.
NCHU System & Network Lab Lab #8 Signals Operating System Lab.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Interacting with Unix. Getting the Process ID u Synopsis #include pid_t getpid(void); u Example: #include int main(){ pid_t n = getpid(); printf("Process.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Files and file allocation.
1 Inter Process Communication & Timers. 2 Time.h (page R:Ch9 pp ) #include time_t time(time_t *calptr); Epoch: 00:00 (midnight), Jan 1, 1970 GMT.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Signals.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
Week 9 - Wednesday.  What did we talk about last time?  structs.
Advanced Programming in the UNIX Environment Hop Lee.
Timers and Time Management Ok-Kyun Ha
Linux/UNIX Programming APUE (Process Control) 문양세 강원대학교 IT 대학 컴퓨터과학전공.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Lesson One – Creating a thread
Time Sources and Timing
Week 9 - Wednesday CS222.
Linux Processes & Threads
UNIX System Overview.
Tarek Abdelzaher Vikram Adve Marco Caccamo
Measuring Program Performance Matrix Multiply
Thread Implementations; MUTEX
Operating Systems Lecture 3.
Time Sources and Timing
Thread Implementations; MUTEX
NETWORK PROGRAMMING CNET 441
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
Signals.
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 3: Process Concept
Measuring Program Performance Matrix Multiply
Presentation transcript:

Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks

Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 Example of race condition with signals This example shows what can happen if a signal handler runs in the middle of modifying a variable. struct two_words { int a, b; } memory; void handler(int signum) { printf ("%d,%d\n", memory.a, memory.b); alarm (1); } int main (void) { static struct two_words zeros = { 0, 0 }, ones = { 1, 1 }; signal (SIGALRM, handler); memory = zeros; alarm (1); while (1) { memory = ones; memory = zeros; } This program can print a pair of zeros or a pair of ones. But are we sure these are the only two cases that can happen? The signal function provides a simple interface for establishing an action for a particular signal. Use of this function is unspecified in a multi-threaded process (see man pages!)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 Example of race condition with signals On most machines, it takes several instructions to store a new value in memory, and the value is stored one word at a time. If the signal is delivered in between these instructions, the handler might find that memory.a is zero and memory.b is one (or vice versa). struct two_words { int a, b; } memory; void handler(int signum) { printf ("%d,%d\n", memory.a, memory.b); alarm (1); } int main (void) { static struct two_words zeros = { 0, 0 }, ones = { 1, 1 }; signal (SIGALRM, handler); memory = zeros; alarm (1); while (1) { memory = zeros; memory = ones; } Note that calling printf in the handler is safe in this program because printf is never called outside the handler when the signal happens

Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 POSIX signals and threads In previous lectures, we studied POSIX signals. We assumed that each process had only one single thread of execution. We now need to understand how we can use properly POSIX signals in the presence of multi-threaded applications. We need to be cautious when using threads and signals together, since there are some new subtle rules to remember. We now describe the main aspects of it.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 Signals & Threads GLOBAL: Signal handler installed by sigaction is global for all the threads. GLOBAL: A signal is delivered only once to any thread (in the process) whose mask is not blocking that particular signal LOCAL: Signal mask is installed on a per thread basis. INHERITANCE: Child threads inherit the mask of parent thread by default. A thread inherits its parent’s mask; but you can change it by using: int pthread_sigmask(int how, const sigset_t *newmask, sigset_t *oldmask); Don’t use sigprocmask! That is for PROCESS

Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Signals & Threads Problem: if a signal is sent to a process and more than one thread signalwait for this signal, only one of them get it and the order is random. SOLUTION 1. When a thread is created, it inherits the signal mask of the thread that created it. So if we mask all the signals in main’s thread, no thread will receive any signal after it is created. 2. Dedicate a thread to the purpose of catching signals by using sigwait. This thread will now be in charge of handling the signal. For sigwait to work reliably, we need to follow above solution. That is, the signals being waited for must be masked by all threads. If more than one thread sigwait for the same signal, the signal will be randomly delivered to one of them.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 Time.h #include time_t time(time_t *tloc); The time() function shall return the value of time in seconds since the Epoch. The tloc argument points to an area where the return value is also stored. If tloc is a null pointer, no value is stored. Epoch: 00:00 (midnight), Jan 1, 1970 GMT time_t is usually a long UNIX “Y2K” Problem: If the long is 32 bits, when will time overflow? Implementations in which time_t is a 32-bit signed integer (many historical implementations) fail in the year 2038.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 Timing a function (Example 1) #include void function_to_time(void); int main(void) { time_t tstart; tstart = time(NULL); function_to_time(); printf(“function_to_time took %f seconds of elapsed time\n”, difftime(time(NULL), tstart)); return(0); }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 Time struct tm *localtime(const time_t *timer); Takes time since epoch, returns date char *asctime(const struct tm *timeptr); 26 byte date string in ascii (ex. "Wed Jun 30 21:49: \n") char *ctime(const time_t *clock); 26 byte date string in ascii (ex. "Wed Jun 30 21:49: \n") int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst;

Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Gettimeofday() struct timeval time_t tv_sec; /* seconds since the Epoch*/ time_t tv_usec /* and microsoeconds*/ #include int gettimeofday(struct timeval *tv, struct timezone *tz); gives the number of seconds and microseconds since the Epoch Notice that, even though timeval supports microseconds, most operating systems would not support that high resolution since many machines run a 100Hz clock tick tz is null, historical

Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 Measure running time using gettimeofday (Example 2) #include #define MILLION L void function_to_time(void); int main(void) { long timedif; struct timeval tpend; struct timeval tpstart; if (gettimeofday(&tpstart, NULL)) { fprintf(stderr, “Failed to get start time\n”); return 1; }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 Measure running time using gettimeofday function_to_time(); /* timed code goes here */ if (gettimeofday(&tpend, NULL)) { fprintf(stderr, “Failed to get end time\n”); return 1; } timedif = MILLION*(tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec – tpstart.tv_usec; printf(“The function_to_time took %ld microseconds\n”, timedif); return 0; }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 Gettimeofday limitations Expect a resolution on the order of the clock tick of your operating system (1-10 msec) Quiz: What does happen if you have consecutive calls of gettimeofday (within few microseconds)?

Copyright ©: Nahrstedt, Angrave, Abdelzaher 14 Gettimeofday limitations Expect a resolution on the order of the clock tick of your operating system (1-10 msec) Quiz: What does happen if you have consecutive calls of gettimeofday (within few microseconds)?  It will return the same value!

Copyright ©: Nahrstedt, Angrave, Abdelzaher 15 Sleep #include unsigned sleep(unsigned seconds); Returns 0 if successful. Process can be awakened by a SIGNAL (sleep returns unslept time) sleep() may be implemented using SIGALRM; mixing calls to alarm(2) and sleep() is a bad idea.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 Nanosleep int nanosleep(const struct timespec *req, struct timespec *rem); nanosleep() delays the execution of the program for at least the time specified in *req. The function can return (with error) earlier if a signal has been delivered to the process. In this case, it writes the remaining time into the structure pointed to by rem unless rem is NULL. The structure timespec is used to specify intervals of time with nanosecond precision. It is specified in and has the form struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ };

Copyright ©: Nahrstedt, Angrave, Abdelzaher 17 Nanosleep Compared to sleep(3), nanosleep() has the advantage of having higher resolution, not affecting any signals, and it is standardized by POSIX. nanosleep() is based on the kernel timer mechanism, which has normally a resolution of 1-10 msec (see man 7 time). nanosleep() pauses always for at least the specified time, however it can take up to 10 ms longer than specified until the process becomes runnable again.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 18 Elapsed time versus processor time The time function measures elapsed time or wall clock time. The virtual time for a process is the amount of time that the process spends in the running state #include clock_t times(struct tms *buffer); The times() function shall fill the tms structure pointed to by buffer with time-accounting information. The tms structure is defined in. All times are measured in terms of the number of clock ticks used. The tms_utime (of type clock_t) structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime (of type clock_t) structure member is the CPU time charged for execution by the system on behalf of the calling process.