-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeFile.c
115 lines (84 loc) · 2.44 KB
/
codeFile.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
110
111
112
113
114
115
/*
Leland Conn
October 6, 2020
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
// Reading and writing end of the pipe
#define READ_END 0
#define WRITE_END 1
int main(int argc, char **argv)
{
// If the user does not pass X, Y and Z, the program will terminate
if (argc != 4)
{
printf("Invalid arguments\n");
exit(0);
}
int port[2];
int status;
pid_t parentPid, child;
parentPid = getpid();
status = pipe(port);
if (pipe(port) < 0) {
perror("pipe error");
exit(0);
}
printf("A pipe is created for communication between parent (PID %d) and child\n", parentPid);
child = fork();
if (child < 0) {
perror("fork error");
exit(0);
}
if (child > 0) { // parent
printf("parent (PID %d) created a child (PID %d)\n", parentPid, child);
char* x = argv[1];
printf("parent (PID %d) receives X = \"%s\" from the user\n", parentPid, x);
//parent now waits
wait(NULL);
int len;
char yz[len];
// read the length of yz from the pipe
read(port[0], &len, sizeof(len));
//read yz from the pipe
read(port[0], yz, len);
printf("parent (PID %d) reads Y' from the pipe (Y' = \"%s\")\n", parentPid, yz);
// create a char array to store xyz
char xyz[len + sizeof(x)];
/* Copy the string x into xyz */
strcpy(xyz, x);
/* Concatenate yz to the end of xyz */
strcat(xyz, yz);
printf("parent (PID %d) concatenates X and Y' to generate the string: \"%s\"\n", parentPid, xyz);
}
if (child == 0) { // child
pid_t childPid = getpid();
// create char arrays to store y and z
char* y = argv[2];
char* z = argv[3];
printf("child (PID %d) receives Y = \"%s\" and Z = \"%s\" from the user\n", childPid, y, z);
// store the length of yz in an integer variable
int yz_length = sizeof(y) + sizeof(z);
// create a character array to store concatenated yz
char yz[yz_length];
// Copy y into the variable
strcpy(yz, y);
// Concatenate z to the end of the first one
strcat(yz, z);
printf("child (PID %d) concatenates Y and Z to generate Y'= \"%s\"\n", childPid, yz);
// store the length string yz in variable len + 1 to account for newline
int len;
len = strlen(yz) + 1;
// write the length of string yz to the pipe
write(port[1],&len,sizeof(len));
// write yz to the pipe
write(port[1], yz, len);
printf("child (PID %d) writes Y' into the pipe\n", childPid);
}
return 0;
}