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

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
Real-Time Library: RTX
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
Linux Scheduler. Linux is a multitasking OS Deciding what process runs next, given a set of runnable processes, is a fundamental decision a scheduler.
CS 6560 Operating System Design Lecture 7: Kernel Synchronization Kernel Time Management.
The Process Control Block From: A Process Control Block (PCB, also called Task Control Block or Task Struct) is.
Timers Timer – Keeps track of the passage of time Simple Timer – Measures elapsed time, reporting it when queried Interval Timer – –Generates an interrupt.
1 Soft Timers: Efficient Microsecond Software Timer Support For Network Processing Mohit Aron and Peter Druschel Rice University Presented By Jonathan.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6 Threads and Scheduling 6.
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.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Dr. Hugh Melvin, Dept. of IT, NUI,G1 POSIX POSIX: Portable OS Interface –IEEE standard –Mandatory + Optional parts Mostly based on and adopted by Unix.
Process Description and Control A process is sometimes called a task, it is a program in execution.
Timers Timer – Keeps track of the passage of time Simple Timer – Measures elapsed time, reporting it when queried Interval Timer – –Generates an interrupt.
1 Performance Measurement CSE, POSTECH 2 2 Program Performance Recall that the program performance is the amount of computer memory and time needed to.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Signals & Timers CS241 Discussion Section Spring 2009 Week 6.
Introduction to Embedded Systems A/D & D/A Conversion Lecture 20.
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.
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:
The Functions of Operating Systems Interrupts. Learning Objectives Explain how interrupts are used to obtain processor time. Explain how processing of.
Tami Meredith, Ph.D. CSCI  Devices need CPU access  E.g., NIC has a full buffer it needs to empty  These device needs are often asynchronous.
CE Operating Systems Lecture 11 Windows – Object manager and process management.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Timers and Clocks.
Time Management.  Time management is concerned with OS facilities and services which measure real time, and is essential to the operation of timesharing.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Scheduling II: priority scheduling.
4P13 Week 2 & 3 Talking Points 1. Kernel Processes 2.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks.
Processes and Virtual Memory
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Computer Organization Instruction Set Architecture (ISA) Instruction Set Architecture (ISA), or simply Architecture, of a computer is the.
Time Management.  Time management is concerned with OS facilities and services which measure real time.  These services include:  Keeping track of.
1 Run-to-Completion Non-Preemptive Scheduler. 2 In These Notes... What is Scheduling? What is non-preemptive scheduling? Examples Run to completion (cooperative)
Time Sources and Timing David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
Copyright © Curt Hill More on Operating Systems Continuation of Introduction.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Scheduling.
Timers and Time Management Ok-Kyun Ha
Event Sources and Realtime Actions
Chapter 4 – Thread Concepts
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Interrupts and signals
CS 6560: Operating Systems Design
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Time Sources and Timing
Protection of System Resources
Mechanism: Limited Direct Execution
Chapter 4 – Thread Concepts
R
Time Sources and Timing
Tarek Abdelzaher Vikram Adve Marco Caccamo
Processor Fundamentals
Operating Systems Lecture 3.
Midterm Review Brian Kocoloski
Time Sources and Timing
Signals.
In Today’s Class.. General Kernel Responsibilities Kernel Organization
Presentation transcript:

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

Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 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 3 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 4 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 1 extra clock tick (1- 10 msec) for the process to become runnable again.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 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.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Interval Timers Set an alarm: on its expiration send a signal Set a signal handler to do something with the signal POSIX:TMR has at least 1 real-time clock, but a process can create many independent timers Most systems require the program be linked with the librt (-lrt) library to use these functions.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 Real Time Clock & timers Timers can be used to send a signal to a process after a specified period of time has elapsed. Timers may be used in one of two modes: one-shot or periodic: when a one-shot timer is set up, a value time is specified. When that time has elapsed, the operating system sends the process a signal and deletes the timer. when a periodic timer is set up, both a value and an interval time are specified. When the value time has elapsed, the operating system sends the process a signal and reschedules the timer for interval time in the future. When the interval time has elapsed, the OS sends another signal and again reschedules the timer for interval time in the future. This will continue until the process manually deletes the timer. By default a timer will send the SIGALRM signal. If multiple timers are used in one process, however, there is no way to determine which timer sent a particular SIGALRM. Therefore, an alternate signal, like SIGUSR1, may be specified when the timer is created.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 POSIX.4 Real Time Clock & timers Resolution of timers: Timers are maintained by the operating system, and they are only checked periodically. A timer that expires between checks will be signaled (and rescheduled if periodic) at the next check. As a result, a process may not receive signals at the exact time(s) that it requested. The period at which the timers are checked, called the clock resolution, is operating system and hardware dependent (~1 msec in PC without add-on high resolution timers). The actual value can be determined at runtime by calling clock_getres() on the system-wide real-time clock (CLOCK_REALTIME). According to POSIX, there is at least 1 real-time clock (CLOCK_REALTIME)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 Real Time Clock & timers Resolution of timers: For example, suppose the OS checks the timers every 10 milliseconds and a process schedules a periodic timer with value = 5 milliseconds and interval = 21 milliseconds. Quiz: Will the timer periodically expire every 21 milliseconds?

Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Real Time Clock & timers Resolution of timers: For example, suppose the OS checks the timers every 10 milliseconds and a process schedules a periodic timer with value = 5 milliseconds and interval = 21 milliseconds. Quiz: Will the timer periodically expire every 21 milliseconds? Answer: If the period of timer is not an exact multiple of the granularity of underlying clock (see man 7 time), then the interval will be rounded up to the next multiple.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 Real Time Clock & timers High Resolution Timers (require hardware support): How to use high resolution timers? There are no special requirements, except a recent glibc, to make use of high resolution timers. When the high resolution timers are enabled in the (Linux) kernel, then nanosleep, itimers and posix timers provide the high resolution mode without changes to the source code.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 High Resolution Timers in Linux Before Linux , the accuracy of timer and sleep system calls (see below) was limited by the resolution of software clock (~1-10 msec) Since kernel , Linux supports high-resolution timers (HRTs), optionally configurable via CONFIG_HIGH_RES_TIMERS. On a system that supports HRTs, the accuracy of sleep and timer system calls is no longer constrained by the software clock, but instead can be as accurate as the hardware allows (microsecond accuracy is typical of modern hardware). You can determine whether high-resolution timers are supported by checking the resolution returned by a call to clock_getres(2) or looking at the "resolution" entries in /proc/timer_list. See man 7 time:

Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 Real Time Clock & timers Operations: Create_timer() is used to create a new timer. As with clock_getres(), the system-wide real-time clock (CLOCK_REALTIME) should be used. The following code shows how to create a timer that sends the default SIGALRM signal. timer_t timer1; // create a new timer that sends the default SIGALARM signal if (timer_create (CLOCK_REALTIME, NULL, &timer1) != 0) { perror(“timer create”); exit(1); } NULL specifies that default SIGALARM will be delivered!

Copyright ©: Nahrstedt, Angrave, Abdelzaher 14 Timers: struct sigevent If a different signal needs to be sent on timer expiration, then the second argument of timer_create takes a pointer to struct sigevent to specify a different signal to be sent. struct sigevent { int sigev_notify; /* notification type */ int sigev_signo; /* signal number */ union sigval sigev_value; /* Extra data to be delivered with a real-time signal! */ } sigev_notify SIGEV_NONE- No notification from timer SIGEV_SIGNAL- Send a signal

Copyright ©: Nahrstedt, Angrave, Abdelzaher 15 Real Time Clock & timers Operations: The following code shows how to create a timer that sends the SIGUSR1 signal: timer_t timerid; struct sigevent se; // Zero out the data structure and configure it for using SIGUSR1 signal memset(&se, 0, sizeof(se)); se.sigev_signo = SIGUSR1; se.sigev_notify = SIGEV_SIGNAL; // Create a new timer that will send the SIGUSR1 signal if (timer_create(CLOCK_REALTIME, &se, &timerid) != 0) { perror("Failed to create timer”); exit(1); }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 Real Time Clock & timers The timer_settime() function is used to schedule a timer. The struct itimerspec definition taken from /usr/include/linux/time.h is seen here. The it_value member sets the time until the timer first expires. If it is set to 0, the timer will never go off. The it_interval member sets the period of the timer after it first expires. If it is set to 0, the timer will be one-shot. struct itimerspec { struct timespec it_interval; /* Timer period */ struct timespec it_value; /* Timer initial expiration */ }; struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ };

Copyright ©: Nahrstedt, Angrave, Abdelzaher 17 Real Time Clock & timers Example of scheduling timer1 (created in a preceding example) to go off in 2.5 seconds, and then every 100 milliseconds thereafter. struct itimerspec timervals; // The it_value member sets the time until the timer first goes off (2.5 seconds). // The it_interval member sets the period of the timer after it first goes off (100 ms). timervals.it_value.tv_sec = 2;// 2 seconds timervals.it_value.tv_nsec = ;// 0.5 seconds (5e8 nanoseconds) timervals.it_interval.tv_sec = 0;// 0 seconds timervals.it_interval.tv_nsec = ;// 100 milliseconds (1e8 nanoseconds) // Schedule the timer if (timer_settime(timerid, 0, &timervals, NULL) != 0) { perror("Failed to start timer"); exit(1); } Pointer to old itimerspec! It specifies a “relative timer”: it does not use absolute time!

Copyright ©: Nahrstedt, Angrave, Abdelzaher 18 POSIX Real Time Clocks POSIX defines the structure of time representation. There is at least 1 real time clock. We can check the current time with clock_gettime and check the clock resolution with clock_getres. See the following example (compile with -lrt): #include struct timespec current_time, clock_resolution; return_code = clock_gettime( CLOCK_REALTIME, &current_time); //check return_code... Printf(“current time in CLOCK_REALTIME is %ld sec, %ld nsec \n”, current_time.tv_sec,// the second portion current_time.tv_nsec); // the fractional portion in nsec representation return_code = clock_getres( CLOCK_REALTIME, &clock_resolution) //check return_code... printf(“ CLOCK_REALTIME’s resolution is %ld sec, %ld nsec \n”, clock_resolution.tv_sec, clock_resolution.tv_nsec);