-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfork_pingpong.c
More file actions
83 lines (73 loc) · 1.51 KB
/
fork_pingpong.c
File metadata and controls
83 lines (73 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* This program demonstraits a child process waiting on a parent process to
* perform some task, and then the parent process waits for the child process
* to perform some task, and back and forth.
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#define REPEAT 5
static pid_t pid;
static int count;
static void handler(int sig) {
}
static void run_parent() {
int i, status;
sigset_t empty;
sigemptyset(&empty);
kill(pid, SIGUSR1);
for (i = 0; i < REPEAT; i++) {
// Wait for the go ahead
sigsuspend(&empty);
// Do some work
sleep(1);
puts("(parent) Okay, go ahead!");
// Signal go ahead!
kill(pid, SIGUSR1);
}
kill(pid, SIGINT);
wait(&status);
}
static void run_child() {
sigset_t empty;
sigemptyset(&empty);
while (1) {
// Wait for the go ahead
sigsuspend(&empty);
// Do some work..
sleep(1);
puts("(child) All done!");
kill(pid, SIGUSR1);
}
}
int main(int args, char **argv) {
sigset_t block;
struct sigaction sigact;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigact.sa_handler = handler;
if (sigaction(SIGUSR1, &sigact, NULL) == -1) {
puts("sigaction");
exit(EXIT_FAILURE);
}
sigemptyset(&block);
sigaddset(&block, SIGUSR1);
if (sigprocmask(SIG_BLOCK, &block, NULL) == -1) {
puts("sigprocmask");
exit(EXIT_FAILURE);
}
switch ((pid = fork())) {
case -1:
puts("fork");
exit(EXIT_FAILURE);
case 0:
pid = getppid();
run_child();
break;
default:
run_parent();
}
exit(EXIT_SUCCESS);
}