I have a problem with this little piece of code. I have a "server" and a "client". The server waits SIGUSR1
from the client. But when I send SIGUSR1
in a loop, the server doesn't handle every signal !
I do i++
each time I receive a signal, and I get 981 while I send 1000 signals.
usleep()
and sleep()
doesn't help.
here is the client code:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
int main(int ac, char **av)
{
int i = 0;
int status = 0;
if (ac < 2)
return (0);
printf("kill %s (%d)", av[1], atoi(av[1]));
for (i=0;i<=1000;i++)
{
printf("%d\n", i);
kill(atoi(av[1]), SIGUSR1);
}
kill(atoi(av[1]), SIGUSR2);
}
and the server code :
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int i = 0;
void sig2(int signum)
{
/* usleep(30); */
printf("%d\n", i);
i = 0;
}
void my_handler(int signum)
{
i++;
}
int main()
{
printf("pid: %d\n", getpid());
if (signal(SIGUSR1, my_handler) == SIG_ERR)
{
printf("rt\n");
exit(0);
}
signal(SIGUSR2, sig2);
while (1);
}