Lecture 06 Time and Process Info Outline • • • • Time Time System Calls Time Conversions System and Process Information • Live Coding 1 July 24, 2016 Time • Within a program, two types of time: 1. Real time 2. Process time 2 July 24, 2016 Real Time Time measured from some standard point in process’s life • Calendar time- useful for database interactions • Start of process- useful for periodic actions 3 July 24, 2016 Process Time Amount of CPU time used by the process • Useful for checking program performance • Useful for optimizing algorithm performance 4 July 24, 2016 Functions for Time Functions to: • Set time • Get time • Convert time from internal representation to readable format • Convert time from readable format to internal representation 5 July 24, 2016 Calendar Time UNIX represents time internally as seconds since Epoch • Epoch => midnight 1/1/1970 • “birth” of UNIX • Time stored in variables of type time_t (integer) 7 July 24, 2016 Outline • • • • Time Time System Calls Time Conversions System and Process Information • Live Coding 8 July 24, 2016 gettimeofday() int gettimeofday( struct timeval *tv, struct timezone *tz ); • • • • • #include <sys/time.h> Returns 0 on success, -1 on error Fills calendar time in buffer pointed to by tv tz is historical artifact (just set to NULL) System call 9 July 24, 2016 struct timeval struct timeval { time_t suseconds_tv }; 10 July 24, 2016 tv_sec; tv_usec; //seconds since 1/1/1970 //additional microseconds time() time_t time(time_t *timep); • • • • • • #include <time.h> Returns number of seconds since Epoch Returns -1 on error If timep == NULL, current time placed in timep Often use t = time(NULL); without error checking! System call 11 July 24, 2016 time() time() gets time_t from Kernel 12 July 24, 2016 Outline • • • • Time Time System Calls Time Conversions System and Process Information • Live Coding 13 July 24, 2016 ctime() char *ctime(const time_t *timep); • • • • #include <time.h> Returns NULL on error Returns pointer to statically allocated string String terminated by newline and ‘\0’ • 26-byte string: e.g., “Wed Jun 8 14:22:34 2011” • Automatically accounts for timezone and DST settings • Future calls to ctime() overwrites string 14 July 24, 2016 ctime() ctime() converts time_t to fixed format string 15 July 24, 2016 gmtime() struct tm *gmtime( const time_t *timep ); • • • • • #include <time.h> Converts calendar time into broken-time time for UTC Returns pointer to allocated broken-down time structure Returns NULL on error gm -> Greenwich Mean Time 16 July 24, 2016 gmtime() gmtime() converts time_t to broken-down time format (struct tm) 17 July 24, 2016 struct tm Contains date and time broken-down into individual parts struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday int tm_mon; int tm_year; … } 18 July 24, 2016 // seconds //minutes //hours //day //month //year localtime() struct tm *localtime( const time_t *timep ); • #include <time.h> • Converts calendar local time to broken-down time • Accounts for timezone and DST settings • Returns pointer to allocated broken-down time structure • Returns NULL on error 19 July 24, 2016 localtime() localtime() converts time_t to broken-down time format (struct tm) 20 July 24, 2016 mktime() time_t mktime( struct tm *timeptr ); • #include <time.h> • Returns seconds since Epoch (time_t) from corresponding to broken-down time (struct tm) • Returns -1 on error • Adjusts for out of range data fields in struct tm • E.g., -1 seconds => 59 seconds of previous minute • Very useful for time calculations 21 July 24, 2016 mktime() mktime() converts broken-down time (struct tm) to Epoch seconds (time_t) 22 July 24, 2016 asctime() char *asctime( const struct tm *timeptr ); • #include <time.h> • Takes pointer to broken-down time (struct tm) => returns pointer to string containing time • Returns NULL on error • Returns pointer to allocated string (terminated by ‘\n\0’) 23 July 24, 2016 asctime() asctime() converts broken-down time (struct tm) to string (char *) 24 July 24, 2016 strftime() size_t strftime( char *outstr, size_t maxsize, const char *format, const struct tm *timeptr ); • #include <time.h> • More precise control when converting broken-down time into printable format • String returned in outstr formatted according to format • Similar to printf • Uses % for placeholders • Table 10-1 has list of placeholders 25 July 24, 2016 strftime() size_t strftime( char *outstr, size_t maxsize, const char *format, const struct tm *timeptr ); • • • • Returns number of bytes placed in outstr Returns 0 on error maxsize specifies maximum space in outstr strftime() does NOT include a newline in string • Unless specified in format 26 July 24, 2016 strftime() strftime() converts broken-down time (struct tm) to user-formatted string (char *) 27 July 24, 2016 Accuracy of Time • Accuracy of time-related system calls limited to resolution of system software clock • jiffies- measure of time units (Hz) • Defined within the kernel source 28 July 24, 2016 Process Time • Amount of CPU time used by process since creation • Separated into user and system CPU time • User CPU time: time spent executing in user mode • System CPU time: time spent executing in kernel mode • System calls • Performing other tasks on behalf o program (e.g., page faults) 29 July 24, 2016 times() clock_t times( struct tms *buf ); • • • • #include <sys/time.h> Returns number of clocks ticks since arbitrary past time Returns -1 on error clock_t integer time in units of clock ticks • Must convert to time • tms structure holds • user time • system time • (time spent waiting on) child’s user and system time 30 July 24, 2016 times() clock_t times( struct tms *buf ); • Return value of times() is unreliable!! • Begins at arbitrary point • More reliable method: use gettimeofday() 31 July 24, 2016 clock() clock_t clock(); • #include <time.h> • Returns -1 on error • Returns total CPU clocks (user & system) of calling process • Must convert return value (clocks) to time • Divide clock_t return value by CLOCK_PER_SEC => time 32 July 24, 2016 Outline • • • • Time Time System Calls Time Conversions System and Process Information • Live Coding 33 July 24, 2016 System and Process Information May want to answer following questions: • • • • How many processes are running on system? Who owns? What files does a process have open? What files are locked? Which processes hold the locks? What sockets are being used by the system? 34 July 24, 2016 /proc Modern UNIX provides /proc virtual filesystem • Resides under /proc • Contains various files that expose kernel info • Allows process to read using normal file I/O calls • /proc is virtual => does NOT reside on disk 35 July 24, 2016 /proc/PID For each process, kernel provides directory named /proc/pid • pid is process identifier • Directory contains various files and subdirectories with information about that process • E.g., Information for PID 1 (init) • Look at files uner /proc/1 • File named “status” => /proc/1/status 36 July 24, 2016 /proc/PID Many files in /proc/pid directory, e.g., • cmdline: command line arguments used • cwd: symbolic link to current working directory • fd: directory containing symbolic links to open file descriptors • mem: process virtually memory • Any process can access its own /proc/pid using /proc/self 37 July 24, 2016 System Info Under /proc • Various files and subdirectories => access to system-wide info • E.g., • /proc/net - status info about networking and sockets • /proc/sys/kernel – kernel settings • /proc/sys/vm – memory management settings • See Table 12-2 for more 38 July 24, 2016 Accessing /proc Files • Most are read-only (owned by root) • Some can be written by user • /proc/pid directories are volatile • Each pid directory created when process created • Directory disappears when process is terminated • Must handle this accordingly.. (check existence first) 39 July 24, 2016 Outline • • • • Time Time System Calls Time Conversions System and Process Information • Live Coding 40 July 24, 2016 Live Coding • Print current time • Print current time using custom formatted string • Print a made up time • Explore /proc directory structure 41 July 24, 2016