Tuesday, December 2, 2008

Different signal handling under FreeBSD and Linux

Signals Delivery and SA_RESTART:

Under 2.6 i386 Linux at home I have got all of this signals.

Under amd64 FreeBSD I have got only some of them.

That way under FreeBSD there are no warranty on signals delivery.


#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <semaphore.h>

const int MAX_CHI=1000;

volatile int chld_count;

sem_t asem;

void handler(int sig)
{
pid_t pid;
sem_wait(&asem);
chld_count ++;
pid = wait(NULL);

printf("Pid %d exited. count: %d\n", pid, chld_count);
sem_post(&asem);
}

int main(void)
{
int i;
chld_count=0;
struct sigaction sa;
sem_init(&asem,0,1);

signal(SIGCHLD, handler);

sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = handler;

sigaction(SIGCHLD, &sa, NULL);

for(i=0;i<MAX_CHI;i++)
{
int ret_val;
ret_val=fork();

if(ret_val==-1)
{
perror("fork()");
}
else if(!ret_val)
{
printf("in child;Child pid is %d\n", getpid());
exit(0);
}
else
{
printf("Parent pid is %d; child is %d;\n",
getpid(),ret_val);
}
}

for(i=0;i<MAX_CHI;i++)
{
int wparam;
wait(&wparam);
}

return 0;
}

No comments: