problem in Signals

Submitted by beparas
on June 27, 2009 - 3:40am

Hi all,
I wrote two program given below just for testing .
I want to measure the time that signal takes to travels between two process.
In these program, Two signals are send,
1) SIGUSR1(from sigTime2.c) to sigTime1 process &
2) SIGUSR2 (from sigTime1.c) to sigTime2 process.

---------------------------- sigTime1.c -----------------------------------------
#include "stdio.h"
#include "unistd.h"
#include "signal.h"
#include "sys/time.h"
#include "string.h"
#include "fcntl.h"

struct timeval tv;

void handler(){

int read_ret1, cont, fd, ret, strret, flag = 1, kill_ret, gotPid;
char name[32], block[32];

read_ret1 = gettimeofday(&tv, NULL);
if(read_ret1)
perror("gettimeofday in handler");
else
printf("T1 usec=%ld \n", (long)tv.tv_usec);

if(flag == 0){
kill_ret = kill(gotPid, SIGUSR2);
if(kill_ret < 0)
perror("kill");

}
else{
for(cont = 0; (cont < 1500) && flag ; cont++){
snprintf(name, sizeof name, "/proc/%d/cmdline", cont);

fd = open(name, O_RDONLY);

ret = read(fd, block, sizeof block);
if(ret > 0){
strret = strcmp("./sigTime2", block);
if(strret == 0){
kill_ret = kill(cont, SIGUSR2);
if(kill_ret < 0)
perror("Kill");

gotPid = cont;
flag = 0;
}
}
close(fd);
}
}
}

int main(void){

signal(SIGUSR1, handler);
while(1);

return 0;
}

------------------ sigTime1.c -END -------------------------------------

---------------------- sigTime2.c --------------------------------------
#include "unistd.h"
#include "sys/wait.h"
#include "signal.h"
#include "fcntl.h"
#include "sys/time.h"
#include "string.h"

int fd;
struct timeval tv;
int gotpid;

void time_chk(void){
int ret;

ret = gettimeofday(&tv, NULL);
if(ret)
perror("gettimeofday");
else
printf("T2 usec=%ld\n", (long)tv.tv_usec);
}

void handler(){
time_chk();

kill(gotpid, SIGUSR1);
}

int main(void){
int ret, cont, strret, flag = 1;
char name[32], block[32];

signal(SIGUSR2, handler);

for(cont = 0; cont < 1500 && flag ; cont++){
snprintf(name, sizeof name, "/proc/%d/cmdline", cont);

fd = open(name, O_RDONLY);
ret = read(fd, block, sizeof block);

if(ret > 0){
strret = strcmp("./sigTime1", block);
if(strret == 0){
ret = gettimeofday(&tv, NULL);
if(ret)
perror("gettimeofday");
else
printf("sec=%ld, usec=%ld \n", (long)tv.tv_sec, (long)tv.tv_usec);

kill(cont, SIGUSR1);
flag = 0;
gotpid = cont;
}
}
close(fd);
}

while(1);

return 0;
}

--------------- sigTime2.c - END -------------------------------------

------------------------------------ OutPut --------------------------
# ./sigTime1 &
# ./sigTime2
sec=2129097828, usec=870277
T1 usec=873027
T2 usec=4990
T1 usec=6010
T2 usec=135988
T1 usec=136982
T2 usec=267988
T1 usec=268982
T2 usec=399987
T1 usec=400982
T2 usec=531084
T1 usec=531987
T2 usec=660997
T1 usec=663958
T2 usec=813001
T1 usec=815001
T2 usec=966988
T1 usec=967981
T2 usec=91992
T1 usec=93009
T2 usec=217012
T1 usec=219040
T2 usec=341988
T1 usec=342982
T2 usec=467986
T1 usec=469977
T2 usec=593988
T1 usec=594982
T2 usec=717988
T1 usec=718983
T2 usec=843971
T1 usec=845972
T2 usec=969990
T1 usec=970982
T2 usec=93989
T1 usec=94981
T2 usec=217988
T1 usec=218982
T2 usec=342989
T1 usec=343981
T2 usec=467986
T1 usec=469012
T2 usec=593013
T1 usec=593983
T2 usec=717988
T1 usec=718983
T2 usec=843982
T1 usec=845081

-------------------------------- OutPut - END ------------------------------------------

So my Question is

The difference between T1 & T2 (845081 - 843982 = 1099 us) is Correct. The value of HZ = 1000
But the difference between T2 & T1 is (843982 - 718983 = 124999 us ).

So the question is, why the time difference between T2 & T1 is so high ?

Thanks in Advance

<pre/> and automatic variables

strcmp
on
June 27, 2009 - 10:16am

for HTML novices: you can use <pre/> to post formatted code.

isn't there a better way to find the pids? it is slow, you can only start one copy of your programs and what do you do, if you get pids over 1500? reboot? what do you do under bsd? you could (manually or with a helper script) pass the pid via standard input or a named pipe (mknod p pidpipe1 and open and read from pidpipe in sigTime1.c, which will block, until you echo pid2 > pidpipe1; just a suggestion).

flag and gotPid are automatic variables in sigTime1.c's handler(). they lose their value when the function ends. the if(flag == 0) always executes the else branch and searches for the pid.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.