Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Timers and Clocks.

Slides:



Advertisements
Similar presentations
Time Measurement Topics Time scales Interval counting Cycle counters K-best measurement scheme time.ppt CS 105 Tour of Black Holes of Computing.
Advertisements

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.
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.
SignalsSignals. What is a Signal? A signal is a notification that some event has occurred. Usually a signal is sent to a process asynchronously and whatever.
CSE 222 Systems Programming Time, Errors, and File Access Dr. Jim Holten.
Timers Timer – Keeps track of the passage of time Simple Timer – Measures elapsed time, reporting it when queried Interval Timer – –Generates an interrupt.
Time Measurement October 25, 2001 Topics Time scales Interval counting Cycle counters K-best measurement scheme class18.ppt.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
1 Signals COS Goals of Today’s Lecture Overview of signals  Notifications sent to a process  UNIX signal names and numbers  Ways to generate.
CS4514 Assignment 2 Help Session – Data Link Layer Client and Server Processes Speaker: Hao Shang Date: Nov. 11th, 2004.
Concurrency Sharing of resources in the same time frame Apparent concurrency is sharing the same CPU, memory, or I/O device Real concurrency is sharing.
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.
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.
Real-Time Systems Specification and Analysis ITV Real-Time Systems Anders P. Ravn Aalborg University February 2009.
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.
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.
Real-Time Systems Lecture 8 Lärare: Olle Bowallius Telefon: Anders Västberg Telefon:
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks II.
1 CS3516 Project 3 Help Session (B14) Dongqing Xiao Dec 04, 2014.
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 Semaphore and Mutex Operations.
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
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks II.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
System Calls & Signals. setsockopt n Used to set socket options n SO_LINGER - Sets or gets the SO_LINGER option. The argument is a linger structure. n.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks.
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.
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.
1 1 Nov. 24, 2015 Kyu Ho Park Lecture 11 Time Handling,GPIO and I/O Systems.
1/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Timing Tutorial #5 CPSC 261. Clocks Most computer systems have a number of clocks – Give time signals to the pipeline and memory Tick at 2.67 GHz (or.
1 System Programming Chapter 6. 2 Administration Misc. The mid-term will be held on April 26, 2006 in class. It will cover Chapters 1 to 6. MP2 is due.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
1 CS3516 Project 3 Help Session (A15) Dongqing Xiao Sep 30th,2015.
1 Run-to-Completion Non-Preemptive Scheduler. 2 In These Notes... What is Scheduling? What is non-preemptive scheduling? Examples Run to completion (cooperative)
Stdlib.h & time.h. String to numbers #include double atof(char *str); // 把 string 轉為 double #include int atoi(char *str); // 把 string 轉為 integer #include.
Signals and daemon processes Prepared by : Department of CSE Engineered for Tomorrow Course code: 10CS62.
Advanced loop controls. Loop Controls Recall from last week loop controls Event-controlled loops using sentinels repeats until a control variable takes.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Signals.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
Week 9 - Wednesday.  What did we talk about last time?  structs.
Using Libraries The make utility UNIX System Calls Further Topics C Programming:Part 4.
Advanced Programming in the UNIX Environment Hop Lee.
Measuring Time Alan L. Cox Some slides adapted from CMU slides.
Computer Science 516 Addressing Modes. Addressing modes are how our programs get to data Multiple addressing modes created for specific uses Function.
Timing Measurements 國立中正大學 資訊工程研究所 羅習五 老師 0. Chapter 11 – Timing Measurements kernel notion of time the tick rate: HZ jiffes hardware clocks and timers.
CHAPTER 6. SYSTEM DATA FILES AND INFORMATION System Programming 本份投影片大量參考熊博安教授的系統程式投影片 羅習五 國立中正大學資訊工程學系
Linux/UNIX Programming APUE (Process Control) 문양세 강원대학교 IT 대학 컴퓨터과학전공.
Writing a Pong Game ● screen control ● tty control ● time ● asynchronous program.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo
Performance Analysis Tools
Time Sources and Timing
Time Handling,GPIO and I/O Systems
Week 9 - Wednesday CS222.
Measuring Program Performance Matrix Multiply
Input/Output and the Operating Systems
Time Sources and Timing
CSCE Systems Programming
Signals.
Measuring Program Performance Matrix Multiply
閨怨 ~王昌齡 閨中少婦不知愁, 春日凝妝上翠樓; 忽見陌頭楊柳色, 悔教夫婿覓封侯。.
閨怨 ~王昌齡 閨中少婦不知愁, 春日凝妝上翠樓; 忽見陌頭楊柳色, 悔教夫婿覓封侯。.
Presentation transcript:

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Timers and Clocks

Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 Time.h #include time_t time(time_t *calptr); Epoch: 00:00 (midnight), Jan 1, 1970 GMT Day is 86,400 seconds time_t is usually a long UNIX “Y2K” Problem: If the long is 32 bits, when will time overflow? Extensions POSIX:XSI microseconds POSIX:TMR nanoseconds

Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 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 4 Time struct tm *localtime(const time_t *timer); Takes time since epoch, returns date struct tm *gmtime(const time_t *timer); Takes time since epoch, returns UTC char *asctime(const struct tm *timeptr); 26 byte date string in ascii char *ctime(const time_t *clock); 26 byte date string in ascii 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 5 Example 2 What value should the ctime argument point to for the resulting string to be: Mon May 3 14:33:

Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 POSIX XSI struct timeval time_t tv_sec; /* seconds since the Epoch*/ time_t tv_usec /* and microsoeconds*/ #include int gettimeofday(struct timeval *restrict tp, void *restrict tzp); tzp is null, historical

Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 Measure running time using gettimeofday (Example 3) #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 8 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 9 Gettimeofday limitations Resolution small number of microsecs Many consecutive calls of get? Will return same value

Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 A program to test the resolution of gettimeofday (Example 4) #include #define MILLION L #define NUMDIF 20 int main(void) { int i; int numcalls = 1; int numdone = 0; long sum = 0; long timedif[NUMDIF]; struct timeval tlast; struct timeval tthis; if (gettimeofday(&tlast, NULL)) { fprintf(stderr, “Failed to get gettimeofday\n”); return 1; }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 A program to test the resolution of gettimeofday while (numdone < NUMDIF) { numcalls++; if (gettimeofday(&tthis, NULL)) { fprintf(stderr, “Failed to get a later gettimeofday.\n”); return 1; } timedif[numdone] = MILLION*(tthis.tv_sec – tlast.tv_sec) + tthis.tv_usec –tlast.tv_usec; if (timedif[numdone] != 0) { numdone++; tlast=tthis; } ………………….

Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 Realtime clocks POSIX:TMR Library –l rt clockid_t variables --- Can create virtual clocks System wide clock in all implementations is CLOCK_REALTIME #include struct timespec time_t tv_sec; long tv_nsec; /*NANOSECS*/ #include int clock_getres(clockid_t clock_id, struct timespec *res); int clock_gettime(clockid_t clock_id, struct timespec *tp); int clock_settime(clockid_t clock_id, struct timespec *tp); Clock_getres displays nominal resolution in nanoseconds of timers and timer interrupts – usually larger and unrelated to clock_gettime for timing -

Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 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); struct tms: clock_t tms_utime /* User (process)*/ clock_t tms_stime /* System (process)*/ clock_t tms_cutime /* User (proc+child)*/ clock_t tms_cstime /* System (proc+child)*/

Copyright ©: Nahrstedt, Angrave, Abdelzaher 14 Sleep Functions (Example 6) #include unsigned sleep(unsigned seconds); Returns 0 if successful. Process can be awakened by a SIGNAL (sleep returns unslept time) sleep interacts with SIGALRM: can’t use both in the same process concrurrently

Copyright ©: Nahrstedt, Angrave, Abdelzaher 15 Nanosleep #include int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); Resolution of CLOCK_REALTIME is of order 10ms Does not affect SIGALRM

Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 POSIX:XSI Interval Timers Generates a signal after a time interval repeatedly – periodic struct 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 int getitimer(int which, struct itimerval *value); int setitimer(int which, const struct itimerval *restrict value, struct itimerval *restrict ovalue);

Copyright ©: Nahrstedt, Angrave, Abdelzaher 17 POSIX:XSI Interval Timers Generates a signal after a time interval repeatedly – periodic struct 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 int getitimer(int which, struct itimerval *value); int setitimer(int which, const struct itimerval *restrict value, struct itimerval *restrict ovalue); It_interval = 0 then timer wont restart

Copyright ©: Nahrstedt, Angrave, Abdelzaher 18 Interval Timers Set an Alarm to send a signal Set a signal handler to do something with the signal POSIX:XSI extension provides each process a small fixed number of timers. POSIX:TMR has a small number of clocks, but a process can create many independent timers

Copyright ©: Nahrstedt, Angrave, Abdelzaher 19 XSI Interval Timers ITIMER_REAL - decrements in real time; generates SIGALRM when it expires ITIMER_VIRTUAL - virtual time; SIGVTALRM ITIMER_PROF – virtual & system time; SIGPROF

Copyright ©: Nahrstedt, Angrave, Abdelzaher 20 #include /* ARGSUSED */ static void myhandler(int s) { char aster = '*'; int errsave; errsave = errno; write(STDERR_FILENO, &aster, 1); errno = errsave; } Print asterix every 2 seconds

Copyright ©: Nahrstedt, Angrave, Abdelzaher 21 static 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)); }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 22 int 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 signals