Skip to content

Instantly share code, notes, and snippets.

Created January 3, 2014 22:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/768cd33f89397bc3c978 to your computer and use it in GitHub Desktop.
Save anonymous/768cd33f89397bc3c978 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];
void sigquit()
{
printf("%d finished work\n", getpid());
exit(0);
}
void father(int SIG_NUM)
{
int i;
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],SIGQUIT);
}
void son(int SIG_NUM) {
printf("son %d received signal\n", getpid());
sleep(1);
if (kill(pidMain ,SIGUSR1) == -1)
perror("error sending signal to father\n");
signal(SIGQUIT, sigquit);
pause();
}
int main(int argc, char *argv[]) {
int i;
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());
signal(SIGUSR1, father);
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