All about programming in GNU/LINUX

Illustration of time,diffitme and sleep system call in GNU/LINUX

//download link for the file:http://www.slideshare.net/hackintoshrao/timesleep
/* HACKINTOSHRAO , CODES FOR GNU/LINUX \m/
   simple illustration of time and difftime system call in GNU/LINUX
   system calls used: 1)time_t time(time *) ;go to /usr/include and use “grep time_t *”
                      2)double difftime(time2,time1);go to /usr/include and use “grep -l difftime *”
                      3)void sleep(int sec);
                      */

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.h>

int main()
{
    time_t time_1970,time2_1970;//time_t is a built in data type and is used to store very large numbers
    int i;
    double timedifference;
    for(i=0;i<10;i++)
    {

        time_1970=time((time_t *)0);
        /*time system call takes in a NULL argument and returns no.of.seconds eclapsed since 1970*/

        printf(“\nThe no.of.seconds passed from 1970 is:%ld “,time_1970);
        sleep(3);
        /*sleep system call halts or pauses the execution for no.of.seconds mentioned as arguments to the call*/
        time2_1970=time((time_t *)0);
        timedifference=difftime(time2_1970,time_1970);
        /*difftime is a system call which returns back the time difference in seconds as a double b/w its 1st and 2nd argument of type time_t*/
        printf(“\n\nHey !! The program was paused for %f seconds”,timedifference);

    }
    exit(0);
}
Image

Advertisements

2 responses

  1. Nice one buddy ! Can u tell me why have u included unistd.h? I guess stdlib.h is enough right ?

    January 5, 2012 at 12:16 am

    • ya , unistd is not required in this case , all the system calls in this program comes under time.h and stdlib is for exit(0) , but i feel with the range function calls unistd provides in system programming in linux its better to have it …and the program stilll works fine with just stdlib.h , i am not able to figure out the reason ??? can u ???

      January 5, 2012 at 2:52 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s