-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforkpipe.c
109 lines (89 loc) · 2.33 KB
/
forkpipe.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#define _POSIX_SOURCE
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SYSCALL_ERROR 2
void gen_signal_handler(int signum)
{
write(STDOUT_FILENO, "GEN TERMINATED\n",strlen("GEN TERMINATED\n"));
_exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[]){
int pipefd[2];
pid_t gen_pid, nsd_pid;
// Create pipe
if (pipe(pipefd) == -1) {
return SYSCALL_ERROR;
}
// Create GEN process
gen_pid = fork();
if (gen_pid == -1) {
return SYSCALL_ERROR;
}
// GEN main work
if (gen_pid == 0) {
// Cathching signal
signal(SIGTERM, gen_signal_handler);
// Redirect stdout to pipe
if(dup2(pipefd[1],STDOUT_FILENO) == -1){
return SYSCALL_ERROR;
}
//Close pipe for GEN
if(close(pipefd[0]) == -1 || close(pipefd[1]) == -1){
return SYSCALL_ERROR;
}
// GEN work
while (1)
{
// Printed text redirected to pipe
printf("%d %d\n", rand() % 4096, rand() % 4096);
fflush(stdout);
sleep(1);
}
}
nsd_pid = fork();
if (nsd_pid == -1) {
return SYSCALL_ERROR;
}
if(nsd_pid == 0){
// Get input from pipe
if(dup2(pipefd[0],STDIN_FILENO) == -1){
return SYSCALL_ERROR;
}
//Close pipe for NSD
if(close(pipefd[0]) == -1 || close(pipefd[1]) == -1){
return SYSCALL_ERROR;
}
char *nsd_name = "nsd";
// Execute nsd
if(execl(nsd_name,nsd_name,NULL,NULL) == -1){
return SYSCALL_ERROR;
}
}
// Close pipe for MAIN
if(close(pipefd[0]) == -1 || close(pipefd[1]) == -1){
return SYSCALL_ERROR;
}
sleep(5);
if(kill(gen_pid, SIGTERM) == -1){
return SYSCALL_ERROR;
}
// Checking return value of children processes
int gen_stat, nsd_stat;
if(wait(&gen_stat) == -1 || wait(&nsd_stat) == -1){
return SYSCALL_ERROR;
}
// Exit MAIN process
if(gen_stat == EXIT_SUCCESS && nsd_stat == EXIT_SUCCESS){
printf("OK\n");
return EXIT_SUCCESS;
}
else{
printf("ERROR\n");
return EXIT_FAILURE;
}
}