Week 8 - Friday.  What did we talk about last time?  String to int conversions  Users and groups  Password files.

Slides:



Advertisements
Similar presentations
Chapter 9 Times and Timers Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
Advertisements

Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
System Files and Process Environment Password file Group file System identification Time Process environment.
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.
CSE 222 Systems Programming Time, Errors, and File Access Dr. Jim Holten.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
Guide To UNIX Using Linux Third Edition
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
Debugger Presented by 李明璋 2012/05/08. The Definition of Bug –Part of the code which would result in an error, fault or malfunctioning of the program.
1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member.
Homework Reading Programming Assignments
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.
Php – 日期與時間 php.ini (in C:\xampp\php\) date.timezone = Asia/Taipei.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Copyright © 2009 Techtronics'09 by GCECT 1 Presents, De Code C De Code C is a C Programming competition, which challenges the participants to solve problems.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
CSE 332: C++ debugging in Eclipse C++ Debugging in Eclipse We’ve now covered several key program features –Variable declarations, expressions and statements.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
Week 2 - Wednesday.  What did we talk about last time?  C compilation model  Lab 1.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Timers and Clocks.
A Tutorial on Introduction to gdb By Sasanka Madiraju Graduate Assistant Center for Computation and Technology.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
1 SEEM3460 Tutorial Compiling and Debugging C programs.
CSE 351 GDB Introduction. Lab 1 Status? How is Lab 1 going? I’ll be available at the end of class to answer questions There are office hours later today.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 11 – gdb and Debugging.
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/15/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 11 (C-5)
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
ADVANCES ISSUES How to time C Code Debugging Dynamic Allocation of Matrices.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Week 9 - Wednesday.  What did we talk about last time?  structs.
HP-SEE Debugging with GDB Vladimir Slavnic Research Assistant SCL, Institute of Physics Belgrade The HP-SEE initiative.
Advanced Programming in the UNIX Environment Hop Lee.
Computer Science 516 Addressing Modes. Addressing modes are how our programs get to data Multiple addressing modes created for specific uses Function.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
CHAPTER 6. SYSTEM DATA FILES AND INFORMATION System Programming 本份投影片大量參考熊博安教授的系統程式投影片 羅習五 國立中正大學資訊工程學系
Institute of Radio Physics and Electronics ILug-Cal Introduction to GDB Institute of Radio Physics and Electronics and Indian GNU/Linux Users Group Kolkata.
DEBUG.
Computer System Laboratory
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo
Week 9 - Monday CS222.
Performance Analysis Tools
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
A bit of C programming Lecture 3 Uli Raich.
CSE 374 Programming Concepts & Tools
Week 9 - Wednesday CS222.
UNIX System Overview.
Lab: ssh, scp, gdb, valgrind
Lab: ssh, scp, gdb, valgrind
Measuring Program Performance Matrix Multiply
Assignment Operators Topics Increment and Decrement Operators
When your program crashes
Assignment Operators Topics Increment and Decrement Operators
Homework Reading Programming Assignments Finish K&R Chapter 1
Time Sources and Timing
CSCE Systems Programming
Measuring Program Performance Matrix Multiply
閨怨 ~王昌齡 閨中少婦不知愁, 春日凝妝上翠樓; 忽見陌頭楊柳色, 悔教夫婿覓封侯。.
Debugging.
Assignment Operators Topics Increment and Decrement Operators
Chapter 15 Debugging.
Presentation transcript:

Week 8 - Friday

 What did we talk about last time?  String to int conversions  Users and groups  Password files

Measuring programming progress by lines of code is like measuring aircraft building progress by weight. Bill Gates Measuring programming progress by lines of code is like measuring aircraft building progress by weight. Bill Gates

 In the systems programming world, there are two different kinds of time that are useful  Real time  This is also known as wall-clock time or calendar time  It's the human notion of time that we're familiar with  Process time  Process time is the amount of time your process has spent on the CPU  There is often no obvious correlation between process time and real time (except that process time is never more than real time elapsed)

 For many programs it is useful to know what time it is relative to some meaningful starting point  Internally, real world system time is stored as the number of seconds since midnight January 1, 1970  Also known as the Unix Epoch  Possible values for a 32-bit value range from December 13, 1901 to January 19, 2038  Systems and programs that use a 32-bit signed int to store this value may have strange behavior in 2038

 The time() function gives back the seconds since the Unix Epoch  Its signature is:  time_t is a signed 32-bit or 64-bit integer  You can pass in a pointer to a time_t variable or save the return value (both have the same result)  Typically we pass in NULL and save the return value  Include time.h to use time() time_t seconds = time(NULL); printf("%d seconds have passed since 1970", seconds); time_t seconds = time(NULL); printf("%d seconds have passed since 1970", seconds); time_t time(time_t* timePointer);

 We are going to talk about structs next week  However, many time functions need different structs that can hold things  Think of them as classes with no methods and fields that are all public  One such struct is defined as follows: struct timeval { time_t tv_sec; // Seconds since Epoch suseconds_t tv_usec; // Extra microseconds }; struct timeval { time_t tv_sec; // Seconds since Epoch suseconds_t tv_usec; // Extra microseconds };

 The gettimeofday() function offers a way to get higher precision timing data  Its signature is:  From the previous slide, timeval has a tv_secs member which is the same as the return value from time()  It also has a tv_usec member which gives microseconds (millionths of a second)  The timezone pointer tz is obsolete and should have NULL passed into it  Include sys/time.h (not the same as time.h ) to use this function int gettimeofday(struct timeval *tv, struct timezone *tz);

 gettimeofday() is a reliable way to see how long something takes  Get the start time, the end time, and subtract them double start; double end; struct timeval tv; gettimeofday(&tv, NULL); start = tv.tv_sec + tv.tv_usec/ ; someLongRunningFunction(); gettimeofday(&tv, NULL); end = tv.tv_sec + tv.tv_usec/ ; printf("Your function took %.3f seconds", end – start); double start; double end; struct timeval tv; gettimeofday(&tv, NULL); start = tv.tv_sec + tv.tv_usec/ ; someLongRunningFunction(); gettimeofday(&tv, NULL); end = tv.tv_sec + tv.tv_usec/ ; printf("Your function took %.3f seconds", end – start);

 What about printing out a human-readable version of the time?  ctime() takes a time_t value and returns a string giving the day and time  Alternatively, strftime() has a set of specifiers (similar to printf() ) that allow for complex ways to format the date and time printf(ctime(time(NULL)); //prints Fri Mar 15 14:22: printf(ctime(time(NULL)); //prints Fri Mar 15 14:22:

struct tm { int tm_sec; // Seconds (0-60) int tm_min; // Minutes (0-59) int tm_hour; // Hours (0-23) int tm_mday; // Day of the month (1-31) int tm_mon; // Month (0-11) int tm_year; // Year since 1900 int tm_wday; // Day of the week (Sunday = 0) int tm_yday; // Day in the year (0-365; 1 Jan = 0) int tm_isdst; /* Daylight saving time flag > 0: DST is in effect; = 0: DST is not effect; < 0: DST information not available */ }; struct tm { int tm_sec; // Seconds (0-60) int tm_min; // Minutes (0-59) int tm_hour; // Hours (0-23) int tm_mday; // Day of the month (1-31) int tm_mon; // Month (0-11) int tm_year; // Year since 1900 int tm_wday; // Day of the week (Sunday = 0) int tm_yday; // Day in the year (0-365; 1 Jan = 0) int tm_isdst; /* Daylight saving time flag > 0: DST is in effect; = 0: DST is not effect; < 0: DST information not available */ };

 gmtime() and localtime() convert a time_t value to a struct that contains "broken down" time  gmtime() gives UTC time (used to be called Greenwich Mean Time)  localtime() gives the local time, assuming it is set up correctly  mktime() can convert from a broken down time back into time_t time_t seconds = time(NULL); struct tm* brokenDownTime = NULL; brokenDownTime = localtime(&seconds); if( (*brokenDownTime).tm_wday == 1 ) printf("It's just another manic Monday.\n"); time_t seconds = time(NULL); struct tm* brokenDownTime = NULL; brokenDownTime = localtime(&seconds); if( (*brokenDownTime).tm_wday == 1 ) printf("It's just another manic Monday.\n");

 How accurate is the microsecond part of gettimeofday() ?  It depends on the accuracy of the software clock in your system  This clock measures time in units called jiffies  A jiffy used to be 10 milliseconds (100 Hz)  They raised the accuracy to 1 millisecond (1000 Hz)  Now, it can be configured for your system to 10, 4 (the default), , and 1 milliseconds

 For optimization purposes, it can be useful to know how much time a process spends running on the CPU  This time is often broken down into  User time: the amount of time your program spends executing its own code  System time: the amount of time spent in kernel mode executing code for your program (memory allocation, page faults, file opening)

 You can time a program's complete execution by running it with the time command  It will give the real time taken, user time, and system time  Let's say you've got a program called timewaster  Run it like this:  Output might be: time./timewaster real 0m4.84s user 0m1.030s sys 0m3.43s real 0m4.84s user 0m1.030s sys 0m3.43s

 If you want this data inside your program, there are two functions to get it  times() returns clock ticks in real time since some point in the past  It also takes a reference to a struct and fills in values corresponding to the system time and user time used by the process  clock() is a simpler function that returns the clock ticks spent on system and user time together  Converting clock ticks to time requires dividing by system defined constants

 GDB (the GNU Debugger) is a debugger available on Linux and Unix systems  It is a command line utility, but it still has almost all the power that the Eclipse debugger does:  Setting breakpoints  Stepping through lines of code  Examining the values of variables at run time  It supports C, C++, Objective-C, Java, and other languages

 C doesn't run in a virtual machine  To use GDB, you have to compile your program in a way that adds special debugging information to the executable  To do so, add the -ggdb flag to your compilation  Note: You will not need to do this on today's lab gcc –ggdb program.c –o program

 GDB can step through lines of source code, but it cannot magically reconstruct the source from the file  If you want to step through lines of code, you need to have the source code file in the same directory as the executable where you're running GDB

 The easiest way to run GDB is to have it start up a program  Assuming your executable is called program, you might do it like this:  It is also possible to attach GDB to a program that is running already, but you have to know its PID  You can also run GDB on a program that has died, using the core file (which is why they exist) gdb program

CommandShortcutDescription runr Start the program running list 135l List the code near line 135 list functionl List the code near the start of function() print variablep Print the value of an expression backtracebt List a stack trace break 29b Set a breakpoint on line 29 break functionb Set a breakpoint at the start of function() continuec Start running again after stopping at a breakpoint nextn Execute next line of code, skipping over a function steps Execute next line of code, stepping into a function quitq Quit using GDB

 Set breakpoints before running the code  The print command is absurdly powerful  You can type print x = 10, and it will set the value of x to 10  This kind of manipulation will be key to solving today's lab  For more information, use the help command in GDB  You can also list your breakpoints by typing info breakpoints

 structs

 Finish Project 3  Due tonight by midnight!  Read K&R chapter 6