-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcp.c
191 lines (152 loc) · 3.87 KB
/
mcp.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
/*****
Brandon Cao
*****/
#include <stdio.h>
#include <wordexp.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <proc/readproc.h>
#define MAXLEN 256
/***************************************
*
* Signal handler function for SIGALRM
*
*
*****************************************/
typedef void (*sighandler_t)(int sig); //Stackoverflow
volatile int alarmHandler;
void signalHandler(int t ) {
alarmHandler = 1;
}
int main(int argc, char *argv[]){
FILE *file = fopen(argv[1],"r");
int i, len, sig,status;
int numPrograms = 0;
int runningProcess = 1;
wordexp_t wordStruct;
wordexp_t wordArray[MAXLEN];
char buffer[MAXLEN][MAXLEN];
char line[MAXLEN];
sigset_t sigSet;
pid_t pid[numPrograms];
pid_t tempChild;
pid_t procPID[numPrograms];
signal(SIGALRM,signalHandler);
sigemptyset(&sigSet);
sigaddset(&sigSet, SIGUSR1);
sigprocmask(SIG_BLOCK,&sigSet,NULL);
PROCTAB* proc;
proc_t* procInfo;
proc_t info;
if( file != NULL){
/***PART 1***/
char* get;
while((get = fgets(line, sizeof(line),file)) != NULL) {
len = strlen(line);
if(line[len-1] == '\n'){
line[len-1] = '\0';
}
memset(&wordStruct, 0, sizeof(wordStruct));
strcpy(buffer[numPrograms], line);
wordexp(buffer[numPrograms],&wordStruct,0);
wordArray[numPrograms] = wordStruct;
numPrograms++;
}
for(i = 0; i < numPrograms; ++i){
/***fork child process***/
tempChild = fork();
if(tempChild < 0){
perror("failed to fork\n");
exit(1);
}
if(tempChild ==0){
/***part 2***/
sigwait(&sigSet,&sig);
if(execvp(wordArray[i].we_wordv[0], wordArray[i].we_wordv) == -1){
perror("Execution failed\n");
}
}
else if ( tempChild > 0) {
pid[i] = tempChild;
wordfree(&(wordArray[i]));
}
}
/***Wake up child process***/
for(i = 0; i < numPrograms; ++i){
kill(pid[i],SIGUSR1);
printf("Process %d signaled SIGUSR1\n",pid[i]);
}
/**Stop child process**/
for(i = 0; i < numPrograms; ++i){
kill(pid[i],SIGSTOP);
printf("Process %d signaled SIGSTOP\n",pid[i]);
}
/***************************************
*
* This while loop was used in part 2 of the mcp project
* but removed.
removed from part 2
* for(i = 0; i < numPrograms; ++i){
* kill(pid[i],SIGCONT);
* printf("Child %d is continuing\n",i);
* waitpid(-1,&pid[i],0);
*}
*
*****************************************/
/**************
* Got idea of this while loop from office hours
*
*
* while flag{
*
* flag = false;
* for id in pids:
* if running ID
* flag == true
* //ran process
*
* }
*
***************/
/**flag **/
while(runningProcess == 1 ){
runningProcess = 0;
for( i = 0; i < numPrograms; ++i){
if(waitpid(pid[i], &status, WNOHANG) == 0){
runningProcess = 1;
printf("Process %d resumed\n",pid[i]);
printf("----------------------\n");
kill(pid[i],SIGCONT);
/**PART 4**/
proc = openproc(PROC_FILLMEM | PROC_FILLSTAT | PROC_FILLSTATUS| PROC_PID,pid);
memset(&info, 0, sizeof(info));
printf("%20s:\t%s\t%5s\n", "COMMAND","PRIORITY","PPID(PID of parent)");
printf("%20s\t%s\t%5s\n", "--------","--------","----");
while((procInfo = readproc(proc,&info)) != NULL){
printf("%20s:\t%lu\t\t%5i\n",
info.cmd, info.priority,info.ppid);
}
closeproc(proc);
/**PART 3 alarm handler**/
alarm(1);
/**Interupts the MCP**/
while(alarmHandler == 0){
;
}
alarmHandler = 0;
kill(pid[i],SIGSTOP);
printf("----------------------\n");
printf("Stopping process %d \n",pid[i]);
printf("----------------------\n");
}
}
}
}
printf("\nChild processes complete\nProgram Complete\n");
fclose(file);
}