473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

kernel timer

Hey all,

I've been working on the code for a kernel timer which will report real
time, CPU time, user time and kernel time on the operation of fibonacci
method.

The way im doing it is keeping track of the parent and two child times
(using fork()).

I can get parent times to come up, however i do not think they are
correct, and i can not get child times to come up at all. I think in
the signal handler methods i have to somehow restart the timer or
signal, but i am not entirely sure how to.

All the code is below, can someone please assist me. Don't worry about
the delta_time method.

Thanks in advance for any help.
Michael M

#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

long unsigned int fibonacci(unsigned int n);
long unsigned int elapsed_usecs(long, long);
long unsigned int delta_time(struct itimerval, struct itimerval);
void p_handler(int);
void c1_handler(int);
void c2_handler(int);

/* These variables are used to record the accumulated times. They
are set by the signal handlers and read by the processes when
they report the results */
static long p_realt_secs = 0, c1_realt_secs = 0, c2_realt_secs = 0;
static long p_virtt_secs = 0, c1_virtt_secs = 0, c2_virtt_secs = 0;
static long p_proft_secs = 0, c1_proft_secs = 0, c2_proft_secs = 0;
static struct itimerval p_realt, c1_realt, c2_realt;
static struct itimerval p_virtt, c1_virtt, c2_virtt;
static struct itimerval p_proft, c1_proft, c2_proft;

main(int argc, char **argv) {
long unsigned fib = 0;
int pid1, pid2;
unsigned int fibarg;
int status;

// Get command line argument, fibarg (N)
if(argc != 2) {
printf("usage: $./a.out <number>\n");
exit(EXIT_FAILURE);
}
fibarg = (int)strtoul(argv[1], NULL, 10);
printf("Fib is: %u\n", fibarg);
// Initialize parent, child1 and child2 timer values
p_realt.it_interval.tv_sec = 1; //change
p_realt.it_interval.tv_usec = 0; // change
p_realt.it_value.tv_sec = 1; // change
p_realt.it_value.tv_usec = 0; // change

c1_realt.it_interval.tv_sec = 0; //change
c1_realt.it_interval.tv_usec = 0; // change
c1_realt.it_value.tv_sec = 0; // change
c1_realt.it_value.tv_usec = 0; // change

c2_realt.it_interval.tv_sec = 0; //change
c2_realt.it_interval.tv_usec = 0; // change
c2_realt.it_value.tv_sec = 0; // change
c2_realt.it_value.tv_usec = 0; // change

p_virtt.it_interval.tv_sec = 1; //change
p_virtt.it_interval.tv_usec = 0; // change
p_virtt.it_value.tv_sec = 1; // change
p_virtt.it_value.tv_usec = 0; // change

c1_virtt.it_interval.tv_sec = 0; //change
c1_virtt.it_interval.tv_usec = 0; // change
c1_virtt.it_value.tv_sec = 0; // change
c1_virtt.it_value.tv_usec = 0; // change

c2_virtt.it_interval.tv_sec = 0; //change
c2_virtt.it_interval.tv_usec = 0; // change
c2_virtt.it_value.tv_sec = 0; // change
c2_virtt.it_value.tv_usec = 0; // change

p_proft.it_interval.tv_sec = 1; //change
p_proft.it_interval.tv_usec = 0; // change
p_proft.it_value.tv_sec = 1; // change
p_proft.it_value.tv_usec = 0; // change

c1_proft.it_interval.tv_sec = 0; //change
c1_proft.it_interval.tv_usec = 0; // change
c1_proft.it_value.tv_sec = 0; // change
c1_proft.it_value.tv_usec = 0; // change

c2_proft.it_interval.tv_sec = 0; //change
c2_proft.it_interval.tv_usec = 0; // change
c2_proft.it_value.tv_sec = 0; // change
c2_proft.it_value.tv_usec = 0; // change

// Enable parents signal handlers
signal(SIGALRM, p_handler); // change to own signal handlers
signal(SIGVTALRM, p_handler); // change to own signal handlers
signal(SIGPROF, p_handler); // change to own signal handlers

// Set parents itimers
if(setitimer(ITIMER_REAL, &p_realt, NULL) == -1)
perror("parent real timer set error");
if(setitimer(ITIMER_VIRTUAL, &p_virtt, NULL) == -1)
perror("parent virtual timer set error");
if(setitimer(ITIMER_PROF, &p_proft, NULL) == -1)
perror("parent profile timer set error");

pid1 = fork();
if(pid1 == 0) {
// Enable child1 signal handlers (disable parent handlers)
signal(SIGALRM, c1_handler); // change to own signal handlers
signal(SIGVTALRM, c1_handler); // change to own signal
handlers
signal(SIGPROF, c1_handler);

// enable child1 signal handlers

// Set child1 itimers
if(setitimer(ITIMER_REAL, &c1_realt, NULL) == -1)
perror("child1 real timer set error");
if(setitimer(ITIMER_VIRTUAL, &c1_virtt, NULL) == -1)
perror("child1 virtual timer set error");
if(setitimer(ITIMER_PROF, &c1_proft, NULL) == -1)
perror("child1 profile timer set error");

// Start child1 itimer on the fibonacci program
fib = fibonacci(fibarg);

// Read child1 itimer values and report them
getitimer(ITIMER_PROF, &c1_proft);
getitimer(ITIMER_REAL, &c1_realt);
getitimer(ITIMER_VIRTUAL, &c1_virtt);
printf("\n");
printf("Child 1 fib = %ld, real time = %ld sec, %ld msec\n", fib,
c1_realt_secs, elapsed_usecs(c1_realt.it_value.tv_sec,
c1_realt.it_value.tv_usec)/1000);

printf("Child 1 fib = %ld, cpu time = %ld sec, %ld msec\n", fib,
c1_proft_secs, elapsed_usecs(c1_proft.it_value.tv_sec,
c1_proft.it_value.tv_usec)/1000);

printf("Child 1 fib = %ld, user time = %ld sec, %ld msec\n", fib,
c1_virtt_secs, elapsed_usecs(c1_virtt.it_value.tv_sec,
c1_virtt.it_value.tv_usec)/1000);

printf("Child 1 fib = %ld, kernel time = %ld sec, %ld msec\n", fib,
delta_time(c1_proft, c1_virtt),
(elapsed_usecs(c1_proft.it_value.tv_sec,
c1_proft.it_value.tv_usec)/1000) -
(elapsed_usecs(c1_virtt.it_value.tv_sec, c1_virtt.it_value.tv_usec)
/1000));

fflush(stdout);
exit(0);
} else {
pid2 = fork();
if(pid2 == 0) {
// Enable child2 signal handlers
signal(SIGALRM, c2_handler); // change to own signal handlers
signal(SIGVTALRM, c2_handler); // change to own signal
handlers
signal(SIGPROF, c2_handler);

// Set child2 itimers
if(setitimer(ITIMER_REAL, &c2_realt, NULL) == -1)
perror("child 2 real timer set error");
if(setitimer(ITIMER_VIRTUAL, &c2_virtt, NULL) == -1)
perror("child 2 virtual timer set error");
if(setitimer(ITIMER_PROF, &c2_proft, NULL) == -1)
perror("child 2 profile timer set error");

// Start child2 on the fibonacci program
fib = fibonacci(fibarg);

// Read child2 itimer vlues and report them
getitimer(ITIMER_PROF, &c2_proft);
getitimer(ITIMER_REAL, &c2_realt);
getitimer(ITIMER_VIRTUAL, &c2_virtt);
printf("\n");
printf("Child 2 fib = %ld, real time = %ld sec, %ld msec\n", fib,
c2_realt_secs, elapsed_usecs(c2_realt.it_value.tv_sec,
c2_realt.it_value.tv_usec)/1000);

printf("Child 2 fib = %ld, cpu time = %ld sec, %ld msec\n", fib,
c2_proft_secs, elapsed_usecs(c2_proft.it_value.tv_sec,
c2_proft.it_value.tv_usec)/1000);

printf("Child 2 fib = %ld, user time = %ld sec, %ld msec\n", fib,
c2_virtt_secs, elapsed_usecs(c2_virtt.it_value.tv_sec,
c2_virtt.it_value.tv_usec)/1000);

printf("Child 2 fib = %ld, kernel time = %ld sec, %ld msec\n",
fib,
delta_time(c2_proft, c2_virtt),
(elapsed_usecs(c2_proft.it_value.tv_sec,
c2_proft.it_value.tv_usec)/1000) -
(elapsed_usecs(c2_virtt.it_value.tv_sec,
c2_virtt.it_value.tv_usec)
/1000));

fflush(stdout);
exit(0);
} else { /* this is the parent */

// Start parent on the fibonacci program
fib = fibonacci(fibarg);

// Wait for children to terminate
waitpid(0, &status, 0);
waitpid(0, &status, 0);

// Read parent itimer values and report them
getitimer(ITIMER_PROF, &p_proft);
getitimer(ITIMER_REAL, &p_realt);
getitimer(ITIMER_VIRTUAL, &p_virtt);
printf("\n");
printf("Parent fib = %ld, real time = %ld sec, %ld msec\n", fib,
p_realt_secs, elapsed_usecs(p_realt.it_value.tv_sec,
p_realt.it_value.tv_usec)/1000);

printf("Parent fib = %ld, cpu time = %ld sec, %ld msec\n", fib,
p_proft_secs, elapsed_usecs(p_proft.it_value.tv_sec,
p_proft.it_value.tv_usec)/1000);

printf("Parent fib = %ld, user time = %ld sec, %ld msec\n", fib,
p_virtt_secs, elapsed_usecs(p_virtt.it_value.tv_sec,
p_virtt.it_value.tv_usec)/1000);

printf("Parent fib = %ld, kernel time = %ld sec, %ld msec\n", fib,
delta_time(p_proft, p_virtt),
(elapsed_usecs(p_proft.it_value.tv_sec,
p_proft.it_value.tv_usec)/1000) -
(elapsed_usecs(p_virtt.it_value.tv_sec, p_virtt.it_value.tv_usec)
/1000));

fflush(stdout);
exit(0);

}
printf("this line should never be printed\n");
}
}

long unsigned int fibonacci(unsigned int n) {
if(n == 0)
return 0;
else if ( n == 1 || n == 2)
return 1;
else
return (fibonacci(n-1) + fibonacci(n-2));
}

long unsigned int elapsed_usecs(long sec, long usec)
{
return ((sec*1000000) + usec);
}

long unsigned int delta_time(struct itimerval n, struct itimerval m)
{
return m.it_value.tv_sec + n.it_value.tv_sec;
}

void p_handler(int signo)
{
switch(signo) {
case SIGALRM:
p_realt_secs++;
signal(SIGALRM, p_handler); // change to own signal handlers
break;
case SIGVTALRM:
p_virtt_secs++;
signal(SIGVTALRM, p_handler); // change to own signal handlers
break;
case SIGPROF:
p_proft_secs++;
signal(SIGPROF, p_handler);
break;
}
return;
}

void c1_handler(int signo)
{
switch(signo) {
case SIGALRM:
c1_realt_secs++;
signal(SIGALRM, c1_handler); // change to own signal handlers
break;
case SIGVTALRM:
c1_virtt_secs++;
signal(SIGVTALRM, c1_handler); // change to own signal handlers
break;
case SIGPROF:
c1_proft_secs++;
signal(SIGPROF, c1_handler);
break;
}
return;
}

void c2_handler(int signo)
{
switch(signo) {
case SIGALRM:
c2_realt_secs++;
break;
case SIGVTALRM:
c2_virtt_secs++;
break;
case SIGPROF:
c2_proft_secs++;
break;
}
return;
}

Nov 15 '05 #1
1 5646
ma********@hotmail.com wrote:
I've been working on the code for a kernel timer which will report real
time, CPU time, user time and kernel time on the operation of fibonacci
method.

The way im doing it is keeping track of the parent and two child times
(using fork()).


[...]

I think you might be looking for comp.unix.programmer.

By the way, may I suggest splitting up your main() into more functions?

--
Sean
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steven T. Hatton | last post by:
http://netlab.ru.is/exception/LinuxCXX.shtml We have implemented a complete kernel level run-time support for C++ in the Linux kernel. In particular our run-time support enables the full use of...
2
by: Rick | last post by:
Does anyone have any recommendations for setting kernel.shmmax and/or some of the other kernel parms that affect DB2, on a system that has 24-32GB of RAM available? I am also looking for a good...
12
by: Jean-Marc Blaise | last post by:
Hi, Is it worth to use 64-bit DB2 instances on a 32-bit kernel, in terms of: - performance - configuration (go beyond the 256 Mb segment for private mem, 1.75 Gb for Bufferpools) - other ? ...
1
by: alice | last post by:
hi all, First of all I would apologize in case this the wrong group for posting this article. I'm trying to write small Linux kernel modules.I compile them with the following command gcc -c...
50
by: Romeo Colacitti | last post by:
Is the C library of most OSes (i.e, unix type OSes) implemented at the very low kernel or just outside kernel level? Looking through the source tree of Linux/BSDs it seems like the C library is...
1
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for...
1
by: Marcel Groner | last post by:
I have a problem with postgresql runnung on smp kernel. setup: master: ------- - Pentium 4 (hyperthreading) - 2 GB Memory - os: fedora core 1 - kernel: 2.4.22-1.2188.nptlsmp
8
by: db2_d_b_a | last post by:
Hello All, Brief info on the system: Db2 version : "DB2 v8.1.0.121" and FixPak "13". OS : RHEL 3 2.4.21-37.0.1 Memory : 8 GB Swap : 2 GB The issue here is the instance not being able to...
20
by: Jimmy | last post by:
Hi to all python now has grown to a versatile language that can accomplish tasks for many different purposes. However, AFAIK, little is known about its ability of kernel coding. So I am...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.