This repository has been archived by the owner on Nov 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Prgm-1.c
196 lines (150 loc) · 4.88 KB
/
Prgm-1.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Lab 3: 21-Aug
Question:
Processing of commands with pipes and fork
Write a program that will read a command from a user and execute them. The program should exit
if the user enters a special command called "quit" or "exit". The program should use pipes
to communicate between parent and child processes to implement the desired functionality.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
int execute_process(char *readBuffer){
/* Util function to execute a process */
char readBuffer_Copy[strlen(readBuffer)];
strcpy(readBuffer_Copy, readBuffer);
/* 1. Count number of words required as well as max word length */
int wordCount = 0;
char *tempWord = strtok(readBuffer, " \n");
while (tempWord != NULL){
++wordCount;
tempWord = strtok(NULL, " \n");
}
// Make Arguments
char *args[wordCount + 1];
char *word = strtok(readBuffer_Copy, " \n");
int i = 0;
// Copy tokens to args
while (word != NULL){
args[i++] = word;
word = strtok(NULL, " \n");
}
args[i] = NULL;
/* 2. Execute le command via un fork */
pid_t execPid;
execPid = fork();
if (execPid == -1){
perror("Fork creation in execute_process failed!\nExiting\n");
return -1;
}
if (execPid == 0){
/* This is the child process */
execvp(args[0], args);
exit(errno);
} else {
/* This is the parent process */
int returnStatus;
waitpid(execPid, &returnStatus, 0);
}
return 0;
}
int main(int argc, char *argv[]){
printf("Starting Lab3...\n");
printf("Question:\n\n\tWrite a program that will read a command from a user and execute them. The program should exit ");
printf("if the user enters a special command called \"quit\" or \"exit\". The program should use pipes to communicate ");
printf("between parent and child processes to implement the desired functionality.\n");
int signal_sendPipe[2]; // This is a pipe for sending the user input from the parent
int signal_ackPipe[2]; // This is a pipe to acknowledge from the child
char quit_word[][10] = {
"QUIT",
"EXIT",
"quit",
"exit"
};
int loopProcess = 1; // The entire program loop is controlled by this variable
/* Pipe_return_status */
int signal_sendPipe_rs = pipe(signal_sendPipe);
int signal_ackPipe_rs = pipe(signal_ackPipe);
if (signal_sendPipe_rs == -1){
perror("signal_sendPipe creation failed!\nExiting\n");
return -1;
}
if (signal_ackPipe_rs == -1){
perror("signal_ackPipe creation failed!\nExiting\n");
return -1;
}
pid_t pid = fork();
if (pid == -1){
perror("Fork creation failed!\nExiting\n");
return -1;
}
// Begin Loop
while (loopProcess){
if (pid == 0){
/* This is the Child Process */
raise(SIGSTOP); // Wait for Parent to give signal to continue
char readBuffer[1024];
close(signal_sendPipe[1]); // Close writing
close(signal_ackPipe[0]); // Close reading
if (read(signal_sendPipe[0], readBuffer, sizeof(readBuffer)) == -1){
perror("Child Process:\tReading (readBuffer) from Pipeline in Child failed!\nReturning -1\n");
return -1;
}
// printf("Child Process:\tRead:\t%s\n", readBuffer);
char send_acknowledge[10];
strcpy(send_acknowledge, "continue");
for (int i = 0; i < sizeof(quit_word)/10; ++i){
if (strncmp(quit_word[i], readBuffer, strlen(quit_word[i])) == 0){
strcpy(send_acknowledge, "quit");
loopProcess = 0;
}
}
// Send Acknowledge
if (write(signal_ackPipe[1], send_acknowledge, sizeof(send_acknowledge)) == -1){
perror("Child Process:\tWriting (send_acknowledge: success) to Pipeline in Child failed!\nBreaking\n");
return -1;
}
// printf("\nSending:\t%s", send_acknowledge);
// strncmp evaluates to 0 if it's true.
if (strncmp(send_acknowledge, "quit", 4) != 0){
if (execute_process(readBuffer) == -1){
perror("Child Process:\texecute_process returned -1.\nReturning -1\n");
return -1;
}
}
} else {
char writeBuffer[1024];
/* This is the Parent Process */
waitpid(pid, NULL, WUNTRACED);
close(signal_sendPipe[0]); // Close reading
close(signal_ackPipe[1]); // Close writing
// printf("\nParent Process:\tType Command:\t");
printf("\nType Command:\t");
fgets(writeBuffer, 1024, stdin);
if (write(signal_sendPipe[1], writeBuffer, sizeof(writeBuffer)) == -1){
perror("Parent Process:\tWriting (writeBuffer) to Pipeline in Parent failed!\nBreaking\n");
kill(pid, SIGKILL);
return -1;
}
kill(pid, SIGCONT);
// Read Acknowledge
char acknowledge[10];
if (read(signal_ackPipe[0], acknowledge, sizeof(acknowledge)) == -1){
perror("Parent Process:\tReading (acknowledge) from Pipeline in Parent failed!\nBreaking\n");
kill(pid, SIGKILL);
return -1;
}
if (strncmp(acknowledge, "quit", 4) == 0){
printf("Parent Process:\tAcknowledge QUIT received!\n");
kill(pid, SIGKILL);
loopProcess = 0;
}
}
}
return 0;
}