Skip to content

Instantly share code, notes, and snippets.

Created January 4, 2014 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7c7ab7dc3b8c0ae73247 to your computer and use it in GitHub Desktop.
Save anonymous/7c7ab7dc3b8c0ae73247 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
int count = 0;
int pidMain;
int numThreads = 3;
int pids[3];
struct sigaction action;
void sigterm()
{
printf("%d finished work\n", getpid());
exit(0);
}
void father(int signo, siginfo_t *info, void *extra)
{
int i;
void *ptr_val = info->si_value.sival_ptr;
int int_val = info->si_value.sival_int;
printf("Signal %d, value %d \n", signo, int_val);
count++;
printf("father received signal, count = %d\n", count);
if (count < 3)
pause();
if (count == 3)
for (i = 0; i < numThreads; i++)
kill(pids[i],SIGTERM);
}
void son(int SIG_NUM)
{
union sigval value;
printf("son %d received signal\n", getpid() - pidMain);
sleep(1);
value.sival_int = getpid() - pidMain;
sigqueue(pidMain,SIGUSR1, value);
signal(SIGTERM, sigterm);
pause();
}
int main(int argc, char *argv[]) {
int i;
action.sa_flags = SA_SIGINFO;
action.sa_sigaction = &father;
pidMain = getpid();
for (i = 0; i < numThreads; i++)
{
pids[i] = fork();
switch (pids[i]) {
case 0:
i = numThreads;
printf("Proc %d created\n", getpid());
signal(SIGUSR2, son);
pause();
break;
case -1:
perror("Creating process failed\n");
exit(EXIT_FAILURE);
break;
default: ;
}
}
if (getpid() == pidMain) {
sleep(1);
printf("Starting first cycle %d\n", getpid());
if (sigaction(SIGUSR1, &action, NULL) == -1)
{
perror("sigusr: sigaction");
_exit(1);
}
for (i = 0; i < numThreads; i++)
{
printf("pids[i] = %d started\n", pids[i]);
kill(pids[i], SIGUSR2);
}
pause();
printf("Finishing first cycle %d\n", getpid());
}
printf("pid %d finished work\n", getpid());
return (EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment