-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjoey.c
More file actions
206 lines (159 loc) · 5.61 KB
/
Copy pathjoey.c
File metadata and controls
206 lines (159 loc) · 5.61 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
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
197
198
199
200
201
202
203
204
205
/*--------------------------------------------------------------------------*
*----
----*
*---- joey.c
----*
*----
----*
*---- This program implements the joey processes, which try to
----*
*---- leave the mall.
----*
*----
----*
*---- ---- ---- ---- ---- ---- ---- ---- ----
----*
*----
----*
*---- Version 1.0 2016 April 11 Joseph Phillips
----*
*----
----*
*--------------------------------------------------------------------------*/
//
// Header inclusion:
//
#include "kangarooHeaders.h"
//
// Definition of global vars:
//
// PURPOSE: To tell if the mama is still looking for joeys.
int shouldStillRun = 1;
// PURPOSE: To tell which joey this joey is.
int joeyIndex;
// PURPOSE: To tell the process id of the mall process.
pid_t mallPid;
// PURPOSE: To hold what the next turn will be.
int nextTurn;
// PURPOSE: To initialize most globals given the 'argc' command line
// arguments pointed to by 'argv[]'. No return value.
void initializeMostGlobals (int argc,
char* argv[]
)
{
// I. Application validity check:
if (argc < MIN_JOEY_CMD_LINE_PARAMS)
{
fprintf(stderr,"Usage: %s <index> <mallPid> <randSeed>\n",JOEY_PROG_NAME);
exit(EXIT_FAILURE);
}
// II. Initialize globals:
// II.A. Initialize 'joeyIndex':
joeyIndex = strtol(argv[JOEYS_INDEX_CMD_LINE_INDEX],NULL,10); // <-- REPLACE with code that gets integer from 'argv[JOEYS_INDEX_CMD_LINE_INDEX]'
// II.B. Initialize 'mallPid':
mallPid = strtol(argv[JOEYS_MALL_PID_CMD_LINE_INDEX],NULL,10);
// <-- REPLACE with code that gets integer from 'argv[JOEYS_MALL_PID_CMD_LINE_INDEX]'
// II.C. Initialize random number generator:
int seed = strtol(argv[JOEYS_RAND_SEED_CMD_LINE_INDEX],NULL,10);
// <-- REPLACE with code that gets integer from 'argv[JOEYS_RAND_SEED_CMD_LINE_INDEX]'
srand(seed);
// III. Finished:
}
// PURPOSE: To handle 'CORRECT_TURN_SIGNAL'. Ignores 'sig'. No return value.
void correctTurnSignalHandler (int sig
)
{
// I. Application validity check:
// II.
nextTurn = ((rand() % 2) == 1) ? LEFT_TURN_SIGNAL : RIGHT_TURN_SIGNAL;
printf("Joey # %d: Yay! On to the next turn; I'll guess %s.\n",
joeyIndex,
(nextTurn == LEFT_TURN_SIGNAL) ? "left" : "right"
);
fflush(stdout);
// YOUR CODE HERE to send 'nextTurn' to 'mallPid'
kill(mallPid, nextTurn);
// III. Finished:
}
// PURPOSE: To handle 'WRONG_TURN_SIGNAL'. Ignores 'sig'. No return value.
void wrongTurnSignalHandler (int sig
)
{
// I. Application validity check:
// II.
nextTurn = (nextTurn == LEFT_TURN_SIGNAL)
? RIGHT_TURN_SIGNAL: LEFT_TURN_SIGNAL;
printf("Joey # %d: Oops! Let us try the other direction: %s.\n",
joeyIndex,
(nextTurn == LEFT_TURN_SIGNAL) ? "left" : "right");
fflush(stdout);
// YOUR CODE HERE to send 'nextTurn' to 'mallPid'
kill(mallPid,nextTurn);
// III. Finished:
}
// PURPOSE: To note that the program may now finish (after receiving
// 'SIGINT'). Ignores 'sig'. No return value.
void sigIntHandler (int sig
)
{
printf("Joey # %d: Yay! I'm safely in mommy's pouch!\n",joeyIndex);
fflush(stdout);
shouldStillRun = 0;
}
// PURPOSE: To install 'correctTurnSignalHandler()' as the signal handler for
// 'CORRECT_TURN_SIGNAL' and 'wrongTurnSignalHandler()' as the handler for
// 'WRONG_TURN_SIGNAL'. No parameters. No return value.
void installTurnHandlers ()
{
// I. Application validity check:
// II. Install the handler:
// YOUR CODE HERE
struct sigaction act;
memset(&act,'\0',sizeof(struct sigaction));
act.sa_handler = correctTurnSignalHandler;
act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
sigaction(CORRECT_TURN_SIGNAL,&act,NULL);
struct sigaction actt;
memset(&act,'\0',sizeof(struct sigaction));
actt.sa_handler = wrongTurnSignalHandler;
actt.sa_flags = SA_NOCLDSTOP | SA_RESTART;
sigaction(WRONG_TURN_SIGNAL,&actt,NULL);
// III. Finished:
}
// PURPOSE: To install 'sigIntHandler()' as the handler for 'SIGINT'. No
// parameters. No return value.
void installSigIntHandler ()
{
// I. Application validity check:
// II. Install handler:
// II.A. Set up struct to specify the new action.
// YOUR CODE HERE
struct sigaction act;
memset(&act,'\0',sizeof(struct sigaction));
act.sa_handler = sigIntHandler;
act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
sigaction(SIGINT,&act,NULL);
// III. Finished:
}
int main (int argc,
char* argv[])
{
// I. Application validity check (done by 'initializeMostGlobals()'):
// II. Try to leave mall:
// II.A. Initialize globals:
initializeMostGlobals(argc,argv);
installTurnHandlers();
installSigIntHandler();
// II.B. Try to leave mall:
nextTurn = ((rand() % 2) == 1) ? LEFT_TURN_SIGNAL : RIGHT_TURN_SIGNAL;
printf("Joey # %d: Which direction? Decisions, Decisions! I'll guess %s.\n",
joeyIndex,
(nextTurn == LEFT_TURN_SIGNAL) ? "left" : "right");
fflush(stdout);
// YOUR CODE HERE to send 'nextTurn' to 'mallPid'
kill(mallPid,nextTurn);
while (shouldStillRun)
sleep(1);
// III. Finished:
return(EXIT_SUCCESS);
}