Rts Lab 3
Rts Lab 3
(EKT 424)
LAB 3
CODE
Lab3.c
#include <unistd.h>
#include <stdio.h>
#include <sched.h>
#include <sys/time.h>
void main()
{
int p1,i=1;
p1=fork();
if (p1==0)
{
execl("Task1","Task1",NULL);
}
p1=fork();
if (p1==0)
{
execl("Task2","Task2",NULL);
}
#include <unistd.h>
#include <stdio.h>
#include <sched.h>
#include <time.h>
#include <signal.h>
timer_t gTimerid;
int k = 0;
void timespec_sub(struct timespec *d, struct timespec *a, struct timespec *b)
{
d->tv_nsec = a->tv_nsec - b->tv_nsec;
d->tv_sec = a->tv_sec - b->tv_sec;
void start_timer(void)
{
struct itimerspec value;
value.it_value.tv_sec = 1;
value.it_value.tv_nsec = 0;
value.it_interval.tv_sec = 5;
value.it_interval.tv_nsec = 0;
if (k > 0)
{
clock_gettime(CLOCK_MONOTONIC,&end);
timespec_sub(&diff,&end,&begin);
printf("Delay Task 1 elapsed:%lld.%.9ld \n\n", (long long)diff.tv_sec, diff.tv_nsec);
diff.tv_sec=0;
diff.tv_nsec=0;
}
clock_gettime(CLOCK_MONOTONIC,&begin);
do
{
for(j=0; j<100; j++){}
clock_gettime(CLOCK_MONOTONIC,&end);
timespec_sub(&diff,&end,&begin);
} while(diff.tv_sec < 1);
clock_gettime(CLOCK_MONOTONIC,&begin);
printf("Catched timer signal: %d\nTimer for pid:%d\n", sig, getpid());
k++;
(void) signal(SIGALRM, timer_callback);
}
int main()
{
struct timespec end,begin,diff;
(void) signal(SIGALRM, timer_callback);
start_timer();
while(1);
}
task2.c
#include <unistd.h>
#include <stdio.h>
#include <sched.h>
#include <time.h>
#include <signal.h>
timer_t gTimerid;
int k = 0;
void timespec_sub(struct timespec *d, struct timespec *a, struct timespec *b)
{
d->tv_nsec = a->tv_nsec - b->tv_nsec;
d->tv_sec = a->tv_sec - b->tv_sec;
void start_timer(void)
{
struct itimerspec value;
value.it_value.tv_sec = 1;
value.it_value.tv_nsec = 0;
value.it_interval.tv_sec = 5;
value.it_interval.tv_nsec = 0;
if (k > 0)
{
clock_gettime(CLOCK_MONOTONIC,&end);
timespec_sub(&diff,&end,&begin);
printf("Delay Task 2 elapsed:%lld.%.9ld \n\n", (long long)diff.tv_sec, diff.tv_nsec);
diff.tv_sec=0;
diff.tv_nsec=0;
}
clock_gettime(CLOCK_MONOTONIC,&begin);
do
{
for(j=0; j<100; j++){}
clock_gettime(CLOCK_MONOTONIC,&end);
timespec_sub(&diff,&end,&begin);
} while(diff.tv_sec < 2);
clock_gettime(CLOCK_MONOTONIC,&begin);
printf("Catched timer signal: %d\nTimer for pid:%d\n", sig, getpid());
k++;
(void) signal(SIGALRM, timer_callback);
}
int main()
{
struct timespec end,begin,diff;
(void) signal(SIGALRM, timer_callback);
start_timer();
while(1);
}
task3.c
#include <unistd.h>
#include <stdio.h>
#include <sched.h>
#include <time.h>
#include <signal.h>
timer_t gTimerid;
int k = 0;
void timespec_sub(struct timespec *d, struct timespec *a, struct timespec *b)
{
d->tv_nsec = a->tv_nsec - b->tv_nsec;
d->tv_sec = a->tv_sec - b->tv_sec;
void start_timer(void)
{
struct itimerspec value;
value.it_value.tv_sec = 1;
value.it_value.tv_nsec = 0;
value.it_interval.tv_sec = 5;
value.it_interval.tv_nsec = 0;
if (k > 0) {
clock_gettime(CLOCK_MONOTONIC,&end);
timespec_sub(&diff,&end,&begin);
printf("Delay Task 3 elapsed:%lld.%.9ld \n\n", (long long)diff.tv_sec, diff.tv_nsec);
diff.tv_sec=0;
diff.tv_nsec=0;
}
clock_gettime(CLOCK_MONOTONIC,&begin);
do
{
for(j=0; j<100; j++){}
clock_gettime(CLOCK_MONOTONIC,&end);
timespec_sub(&diff,&end,&begin);
} while(diff.tv_sec < 3);
clock_gettime(CLOCK_MONOTONIC,&begin);
printf("Catched timer signal: %d\nTimer for pid:%d\n", sig, getpid());
k++;
(void) signal(SIGALRM, timer_callback);
}
int main()
{
struct timespec end,begin,diff;
(void) signal(SIGALRM, timer_callback);
start_timer();
while(1);
\
OUTPUT
DISCUSSION
In Lab 3 we will be using two different function which is timer_create( ) and timer_settime( ).
This two functions will help us to get precise timing of the task period.
1) timer_create ( )
- Creates a timer
- The timer_create ( ) function creates a per-process timer using the specified clock
source, clock_id, as the timing base.
- Example of clock source that we preiously learned in Lab 2:-
CLOCK_REALTIME the standard POSIX-defined clock.
CLOCK_MONOTONIC this clock always increases at a constant rate
and can't be adjusted
2) timer_settime ( )
- Sets the expiration time for a timer.
- specified by timerid from the it_value member of the value argument.
- If the it_value structure member of value is zero, then the timer is disarmed.
- The timer_settime ( ) function rounds up time values that are between two
consecutive nonnegative integer multiples of the resolution of the specified timer to
the larger multiple of the resolution.