-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbadApple_owenVer
167 lines (132 loc) · 4.44 KB
/
badApple_owenVer
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define CHAR_BUFFER_SIZE 50
#define INT_BUFFER_SIZE 3
#define RECEIVING_END 0
#define SENDING_END 1
void sigHandler0(int sigNum){
printf(" received. That's it, I'm shutting you down...\n");
exit(0);
}
typedef struct messageType{
int sendTo;
char message[CHAR_BUFFER_SIZE];
} messageType;
int getNodeNum(void){
char *line = calloc(INT_BUFFER_SIZE, sizeof(int));
int k;
printf("Enter desired number of communcation nodes: ");
fflush(stdout);
fgets(line, sizeof(line), stdin);
k = atoi(line);
free(line);
return k;
}
messageType getMessage(int kNodes){
char *line = calloc(INT_BUFFER_SIZE, sizeof(int));
char *junk;
messageType newMessage;
do{
printf("Send to which node? Must be 0-(k-1): ");
fgets(line, sizeof(line), stdin);
newMessage.sendTo = atoi(line);
}while(newMessage.sendTo >= kNodes);
printf("Message (must be under %d chars): ", CHAR_BUFFER_SIZE - 1);
fgets(newMessage.message, sizeof(newMessage.message), stdin);
newMessage.message[strcspn(newMessage.message, "\n")] = 0; //attempts to get rid of newline
free(line);
return newMessage;
}
int main(){
srand(time(NULL));
int kNodes = getNodeNum();
pid_t pid[kNodes];
int pipes[kNodes+1][2];
int badNode, status;
badNode = (rand() % kNodes);
messageType apple = getMessage(kNodes);
//generate k number of pipes
for(int i = 0; i < kNodes; i++){
if(pipe(pipes[i]) < 0){
perror("Pipe failed!");
exit(1);
}
}
//generate k number of nodes
for(int i = 0; i < kNodes; i++){
pid[i] = fork();
if(pid[i] < 0){
perror("Fork Failed!");
exit(1);
}
if(pid[i] == 0){ // child
signal(SIGINT, sigHandler0);
//close every pipe not used by *this* child
for(int j = 0; j < kNodes; j++){
if(j != i){
close(pipes[j][RECEIVING_END]);
}
if(j != i + 1){
close(pipes[j][SENDING_END]);
}
}
//receive message from k - 1
messageType receivedApple;
read(pipes[i][RECEIVING_END], &receivedApple, sizeof(messageType));
//check if *this* child is bad apple
if(badNode == i){
printf("[node %d] This node is the Bad Apple! Modifying message...\n", i);
const char *badMessage = "Your message got Bad Apple'd!";
strcat(receivedApple.message, badMessage);
}
//check if this message is for *this* child
if(receivedApple.sendTo == i){
printf("[node %d] Message reached destination!\n", i);
printf("[node %d] Message reads: \"%s\"\n", i, receivedApple.message);
}
else{
printf("[node %d] Message not addressed to this node.\n", i);
sleep(1);
}
//keep packet going so it hits the end
printf("[node %d] Transmitting to Next Node...\n", i);
write(pipes[i+1][SENDING_END], &receivedApple, sizeof(messageType));
//task complete, shutdown comms
close(pipes[i][RECEIVING_END]);
close(pipes[i + 1][SENDING_END]);
return 0;
}
}
// close all pipes not interacting with top node
for(int j = 0; j < kNodes; j++){
if(j != kNodes){
close(pipes[j][RECEIVING_END]);
}
if(j != 0){
close(pipes[j][SENDING_END]);
}
}
// send the user-defined message to the top node
write(pipes[0][SENDING_END], &apple, sizeof(messageType));
// read from k'th node
read(pipes[kNodes][RECEIVING_END], &apple, sizeof(messageType));
if(apple.sendTo == 0){
printf("[node 0] Message reached destination!\n");
printf("[node 0] Message reads: \"%s\"\n", apple.message);
}
// transmissions complete, close remaining pipes
printf("[node 0] Transmission complete. Closing pipes...\n");
close(pipes[0][RECEIVING_END]);
close(pipes[kNodes][SENDING_END]);
// wait for child processes to complete
for (int i = 0; i < kNodes; i++){
wait(&status);
}
return 0; //End of Main
}