Rts Programs 1st Course

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

-LAB -1

Real time applications can be made in almost any language. The


environment (operating system, runtime and runtime libraries)
must however be compliant to real time constraints. In most
cases real-time means that there's always a deterministic time in
. which something happens

In applications, a program has to not only produce the correct


result, but to do so reliably in a deterministic amount of time. The
code needs to satisfy real-time constraints, complicated by the
fact that it will typically run on a non-real-time OS kernel. Writing
code for such constraints significantly differs from writing code
. that maximizes for bandwidth or overall performance

There are many programming language that can be used in


designing the application that used real time system , We will use
. “ DEV C++ “ programming language

DEV C++ : The full-featured Integrated Development Environment


(IDE) , It used in code completion , project manager , tools
manager, class browser and wide range of features.. It has
functions , variables and parameters . When we used it with real
time system we need to use the variables and functions that deal
with the time and date… for example we need to add the library
so we can use the related functions called (time .h) ” The time .h
header defines four variable types, two macro and various
“.functions for manipulating date and time

<include<time.h#
-LAB -2
And before we start, we need to know that computer doesn’t
save time or date directly on memory, it used a system called
Unix time” where it depends on how many the seconds are”
passed from 1 1 1970, 00:00:00 where this date is the factory
. date for all computers

So, Unix time is a system for representing a point in time. It is the


number of seconds that have elapsed since January 1st, 1970
. 00:00:00 UTC

Examples

Date Time Unix Time

January 1st, 1970 00:00:00 0

June 6th, 1983 00:00:00 423705600

June 6th, 1983 16:00:00 423763200

September 9, 2001 1:46:40 1000000000

July 20th, 1969 20:17:40 14182940-


Library Variables
Following are the variable types defined in the header time. h

Sr.No. Variable & Description

1 size_t

This is the unsigned integral type and is the result of the size
of keyword.

2 clock_t

This is a type suitable for storing the processor time.

3 time_t is

This is a type suitable for storing the calendar time.

4 struct tm

This is a structure used to hold the time and date.


: () Time
This function is used to returns the current time and it used also
to returns the number of the seconds since “Unix time” till the
.current time in the computer

/* time() example */
#include <stdio.h> /* printf */
#include <time.h> /* time_t, time() */

int main ()
{
time_t tt;

tt=time (NULL); [OR time(&tt); OR tt=time(0);]


printf ("The number of the second since 1970= %d \n", tt);

return 0;
}
: “C Time() “Convert time_t value to string
Interprets the value pointed by timer as a calendar time and
converts it to a C-string containing a human-readable version of
. the corresponding time and date, in terms of local time

:The returned string has the following format

Www Mmm dd hh:mm:ss yyyy

,(Where Www is the weekday, Mmm the month (in letters

.dd the day of the month, hh:mm:ss the time, and yyyy the year

/* ctime example */
#include <stdio.h> /* printf */
#include <time.h> /* time_t, time, ctime */

int main ()
{
time_t rawtime;

time (&rawtime);
printf ("The current local time is: %s", ctime (&rawtime));

return 0;
}
Lab -3-
Gmtime () function :
The gmtime() function in C++ return the time from the computer
in the UTC (Universal Time Coordinated) time. It define in ctime
library and deals with (struct tm).

The tm structure has the following definition −


struct tm {
tm_sec; /* seconds, range 0 to 59 */
tm_min; /* minutes, range 0 to 59 */
tm_hour; /* hours, range 0 to 23 */
tm_mday; /* day of the month, range 1 to 31 */
tm_mon; /* month, range 0 to 11 */
tm_year; /* The number of years since 1900 */
tm_wday; /* day of the week, range 0 to 6 */
tm_yday; /* day in the year, range 0 to 365 */
tm_isdst; /* daylight saving time */
/* gmtime example */
#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, gmtime */

int main ()
{
time_t rawtime;
struct tm * ptm;

time ( &rawtime );

ptm = gmtime ( &rawtime );

printf ("The time Is : %d:%d:%d\n", ptm->tm_hour, ptm-


>tm_min, ptm->tm_sec);

return 0;
}
Example for changes the time according to the time of
: another country
/* gmtime example */
#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, gmtime */
#define IND (-5)

int main ()
{
time_t rawtime;
struct tm * ptm;

time ( &rawtime );

ptm = gmtime ( &rawtime );

printf ("The time in India : %d:%d:%d\n", (ptm->tm_hour+IND),


ptm->tm_min,ptm->tm_sec);

return 0;
}
: Asctime () function
This function returns the time and date in a calendar time and
converts it to a C-string containing a human-readable version of
. the corresponding date and time

:The returned string has the following format

Www Mmm dd hh:mm:ss yyyy

Where Www is the weekday, Mmm the month (in letters), dd the
.day of the month, hh:mm:ss the time, and yyyy the year
/*example of asctime () */
#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, gmtime */

int main ()
{
time_t rawtime;
struct tm * ptm;

time ( &rawtime );

ptm = gmtime ( &rawtime );

printf ("The time Is : %s\n",asctime(ptm) );

return 0;
}

This function doesn’t work alone it must be used with*


other functions. In this programa it will work with
.gmtime() function
-Lab -4
: Localtime() function
This function returns the time and date in a calendar time
The local time” , It works like “Time” and “Ctime” functions but”
it differs from them that this functioan couldn’t return the date
and the time in a readable form therefore it works with “Asctime”
.function. It deals with struct tm and Time library
/*example of localtime () */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t tt;
struct tm * timeinfo;

time (&tt);
timeinfo = localtime (&tt);

printf ("Current local time and date: %s", asctime(timeinfo));

return 0;
}
: Mktime() function
The mktime function is an inbuilt c++ returns the Unix time stamp
for date function which convert the local date and time to the
time science epoch and returns the value as an object of type
time _t. The function accept amendatory parameter pointer that
points to tm object structure that contains a calendar time which
.is to be converted

/*example of mktime () */
#include <stdio.h> /* printf, scanf */
#include <time.h> /* time_t, struct tm, time, mktime */

int main ()
{
time_t rawtime;
struct tm * timeinfo;
int year, month ,day;
char weekday[7][10] = { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};

printf ("Enter year: "); scanf ("%d",&year);


printf ("Enter month: "); scanf ("%d",&month);
printf ("Enter day: "); scanf ("%d",&day);

time ( &rawtime );
timeinfo = localtime ( &rawtime );
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;

mktime ( timeinfo );

printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);

return 0;
}
-Lab -6
: () clock function
The function clock() defined in the time .h header. returns the
number of clock ticks elapsed since the program was launched. To
get the number of seconds used by the CPU , you will need to
divide by CLOCKS_PER_SEC constant, which is also defined in
the time .h header returns the number of elapsed CPU clock
cycles.. On a 32 bit system where CLOCKS_PER_SEC equals
1000000 .
/*example of clock () */
#include <stdio.h>
#include <time.h>
int main () {
clock_t start, end;
float total;
int i;
start = clock();
printf("Starting of the program = %d\n", start);

printf("starting of the loop =%d\n",start);


for(i=0; i< 10000000; i++) {
}
end = clock();
printf("End of the loop = %d\n", end);

total = (float)(end - start) / CLOCKS_PER_SEC;


printf("Total time taken by CPU: %f\n", total );

return 0;}
/*example of clock () */
#include <stdio.h> /* printf */
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
#include <math.h>

int add (int n) {


int i;
int x=0;
for (i=1;i<=n ; i++){
x=x+ i;
}

return x;
}

int main ()
{
clock_t start ,end;
int f;
float t;
start = clock();

f = add (5000000);
printf ("The addition of the numbers : %d\n", f);
end=clock();
t =(float) (end-start)/CLOCKS_PER_SEC;
printf ("It took %f clicks \n", t);
return 0;
}
-Lab-5
: Difftime () function
The difftime() function is defined in ctime header file. The
difftime() function is used to calculate the difference between
two times in second. This method accepts two parameters ,

start: time_t object for start time. end:

.time_t object for end time

/*example of difftime () */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t now;
struct tm * newyear;
int seconds;

time(&now);

newyear = localtime(&now);

newyear->tm_hour = 0;
newyear->tm_min = 0;
newyear->tm_sec = 0;
newyear->tm_mon = 0;
newyear->tm_mday = 1;
seconds = difftime(now,mktime(newyear));

printf ("seconds since new year in the current timezon:%d.\n",


seconds);

return 0;
}

You might also like