-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject2.cpp
More file actions
136 lines (115 loc) · 3.43 KB
/
project2.cpp
File metadata and controls
136 lines (115 loc) · 3.43 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
*Programmer: Adam Page
*
*Class: CSCI480
*
*Date Due 9/26/2020
*Assignment: 2
*practice system calls such as fork(), pipe()
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 25
#define READ_END 0
#define WRITE_END 1
int main(void)
{
setbuf(stdout, NULL);
char write_msg[BUFFER_SIZE] = "Go do some chores...";
char read_msg[BUFFER_SIZE];
pid_t pidChild1, pidChild2;
//pipe_a where pipe_a[0] for read end and pipe_a[1] for write end.
int pipe_a[2];
//pipe_b where pipe_b[0] for read end and pipe_b[1] for write end.
int pipe_b[2];
//create the pipe
if (pipe(pipe_a) == -1)
{
fprintf(stderr, "Pipe_a failed");
return 1;
}
if (pipe(pipe_b) == -1)
{
fprintf(stderr, "Pipe_b failed");
return 1;
}
// now fork a child process
//Creates child of the initial process.
pidChild1 = fork();
//The process above executes this line along with the main process,
//thus creating it's own child and becomes intermediate process and initial process creates
//second child
pidChild2 = fork();
if (pidChild1 < 0)
{
fprintf(stderr, "Fork failed");
return 1;
}
if (pidChild1 > 0 && pidChild2 > 0)
{
// parent process
// close the unused read end of the pipe
close(pipe_a[READ_END]);
//display message
printf("\nParent: My PID is %d, my parent's PID is %d. My children are %d, %d. I write to pipe A: %s. \n", getpid(), getppid(), pidChild1, pidChild2, write_msg);
char command[100];
// Create the ps command
sprintf(command, "ps -f --ppid %d,%d,%d,%d", getppid(), getpid(), pidChild1, pidChild2);
//Invoke the command
system(command);
// write to the pipe
write(pipe_a[WRITE_END], write_msg, strlen(write_msg));
// close the write end of the pipe
close(pipe_a[WRITE_END]);
// Wait untill all children finish execution
while (waitpid(pidChild1, NULL, 0) > 0 || waitpid(pidChild2, NULL, 0) > 0);
printf("\nParent: Child processes finished their work. ");
}
else if (pidChild1 == 0 && pidChild2 > 0)
{
// Intermediate Parent
// close the unused read end of the pipe
close(pipe_b[READ_END]);
printf("\nIntermediate Parent: My PID is %d, my parent's PID is %d. My child is %d. I write to pipe B: %s.\n", getpid(), getppid(), pidChild2, write_msg);
// write into the pipe
write(pipe_b[WRITE_END], write_msg, strlen(write_msg));
// close the write end of the pipe
close(pipe_b[WRITE_END]);
//Sleep for 3 seconds.
sleep(3);
printf("\nIntermediate Process: %d is awake", getpid());
}
else if (pidChild1 > 0 && pidChild2 == 0)
{
// Child Process
// close the unused end of the pipe
close(pipe_a[WRITE_END]);
// read from the pipe
read(pipe_a[READ_END], read_msg, BUFFER_SIZE);
printf("\nChild: My PID is %d, my parent's PID is %d. I read from pipe A: %s.\n", getpid(), getppid(), read_msg);
// close the read end of the pipe
close(pipe_a[READ_END]);
//Sleep for 3 seconds.
sleep(3);
printf("\nChild: %d is awake", getpid());
}
else
{
// Child Process
// close the unused end of the pipe
close(pipe_b[WRITE_END]);
// read from the pipe
read(pipe_b[READ_END], read_msg, BUFFER_SIZE);
printf("\nChild: My PID is %d, my parent's PID is %d. I read from pipe A: %s.\n", getpid(), getppid(), read_msg);
// close the read end of the pipe
close(pipe_b[READ_END]);
//Sleep for 3 seconds.
sleep(3);
printf("\nChild: %d is awake", getpid());
}
return 0;
}