Featured Post

Monday, August 1, 2016

Timers in UNIX


                                                                                                            http://cjuschools.blogspot.in/

UNIX operating system supports three types of clocks. One clock is what user see directly on the UNIX screen. Apart from this timer UNIX supports two more types of Timers i) one-short timers ii) repeaters
January 1,1970 is considered as birthday of the UNIX operating system.

time(): this function can be used to know the difference between the present time and January 1, 1970 UTC
time() function is used just to print difference between times but not to print present date.


ctime():  This function can be used to print present date.

char *ctime(const time_t clock).



#include<stdio.h>
#include<time.h>
void main()
{
  long now,time();
  char *ctime();
 time(&now);

  printf("it is now %s\n",ctime(&now));
}

output:
   it is now Tue May 3 22:57:07 2016




If we want to get specific information about current date and time the localtime() and gmtime() functions will be used. They do this by converting the long integer returned by time() into a data structure called tm.

struct tm
{
  int tm_sec;   /* seconds after the minute */
  int tm_min;   /* minutes after the hour */
  int tm_hour;  /* hours since midnight */
 int tm_today;  /* day of the month */
 int tm_mon;    /* months since January */
 int tm_year; /* years since 1900 */
 int tm_wday; /* days since sunday */
 int tm_yday; /* days since January 1*/
 int tm_isdst; /* daylight savings time flag */
};

One Shot Timers
   sleep() system call is an example of one shot timers

     int sleep(int seconds);

  When a process executing sleep, it blocks for the number of seconds specified.




unix timers, ctime() time() repeaters sleep() standard unix timers 

No comments:

Post a Comment