-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.c
166 lines (143 loc) · 5.29 KB
/
process.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "process.h"
#include "main.h"
Process* newProcess(int processType){
Process* createdProcess = (Process*) malloc(sizeof(Process));
if(createdProcess == NULL){
perror("newProcess: Failed to create process struct");
return NULL;
}
char newName[16];
char testProcess[35];
//init integer values
createdProcess->state = 0;
createdProcess->pid = 0;
//set process information
if(processType == 1) {
strcpy(newName, "CPUBound");
strcpy(testProcess, "../testing_processes/cpubound.py");
} else if(processType == 2) {
strcpy(newName, "IOBound");
strcpy(testProcess, "../testing_processes/iobound.py");
} else if(processType == 3) {
strcpy(newName, "MixedBound");
strcpy(testProcess, "../testing_processes/mixbound.py");
}
//allocate memory for name
createdProcess->name = (char*) malloc(sizeof(char) * (strlen(newName)+1));
if (createdProcess->name == NULL) {
perror("newProcess: Failed to allocate memory for process struct name");
free(createdProcess);
return NULL;
}
strcpy(createdProcess->name, newName);
//allocate memory for the arguments array
createdProcess->args = (char**) malloc(3 * sizeof(char*)); //allocate python3, path, NULL terminator, and amount of addl arguments
if (createdProcess->args == NULL) {
perror("newProcess: Failed to allocate memory for process struct arguments");
free(createdProcess->name);
free(createdProcess);
return NULL;
}
//allocate and set the python argument
createdProcess->args[0] = (char*) malloc(strlen("python3")+1);
if (createdProcess->args[0] == NULL) {
perror("newProcess: Failed to allocate memory for python3 argument");
free(createdProcess->args);
free(createdProcess->name);
free(createdProcess);
return NULL;
}
strcpy(createdProcess->args[0], "python3");
//allocate and set the name of the script
createdProcess->args[1] = (char*) malloc(strlen(testProcess)+1);
if (createdProcess->args[1] == NULL) {
perror("newProcess: Failed to allocate memory for script argument");
for (int i = 0; createdProcess->args[i] != NULL; i++) {
free(createdProcess->args[i]);
}
free(createdProcess->args);
free(createdProcess->name);
free(createdProcess);
return NULL;
}
strcpy(createdProcess->args[1], testProcess);
//null-terminate arguments array
createdProcess->args[2] = NULL;
return createdProcess;
}
void startProcess(Process *process, initData *data) {
pid_t pid;
pid = fork(); //fork process
if (pid == 0) { //child process
execvp(process->args[0], process->args);
//will only run if execvp fails
perror("startProcess: execvp failed");
exit(EXIT_FAILURE);
} else if (pid > 0) { //parent process
process->pid = pid;
process->state = 1;
//append message for gtktextview
char message[256];
snprintf(message, sizeof(message), "Started process %d (%s)\n", pid, process->name);
//append message
if(data->textCallback){
data->textCallback(message, data->textCallbackData);
}
} else { //fork failed
perror("startProcess: fork failed");
}
}
void stopProcess(Process *process, initData *data) {
int status;
pid_t result = waitpid(process->pid, &status, WNOHANG);
if (result == 0) {
//process is still running, safe to send SIGSTOP
if (kill(process->pid, SIGSTOP) == -1) {
perror("Failed to stop process");
exit(EXIT_FAILURE);
} else {
process->state = 2;
char message[256];
snprintf(message, sizeof(message), "Stopped process %d (%s)\n", process->pid, process->name);
if (data->textCallback) {
data->textCallback(message, data->textCallbackData);
}
}
} else if (result > 0) {
//process has already finished
process->state = 3;
fprintf(stderr, "Program %d has already completed, cannot stop\n", process->pid);
} else {
//an error occurred in waitpid
perror("stopProcess: waitpid failed");
exit(EXIT_FAILURE);
}
}
void continueProcess(Process *process, initData *data) {
if (kill(process->pid, SIGCONT) == -1) { //resume process by sending SIGSTOP signal
fprintf(stderr, "Program %d failed to resume\n", process->pid); //print error to stderr
exit(EXIT_FAILURE);
} else {
char message[256];
snprintf(message, sizeof(message), "Continued process %d (%s)\n", process->pid, process->name);
//append message
if (data->textCallback) {
data->textCallback(message, data->textCallbackData);
}
process->state = 1;
}
}
void setLogCallback(struct initData *data, logCallback callback, void *userData) {
data->textCallback = callback;
data->textCallbackData = userData;
}
void setTimerCallback(struct initData *data, timerCallback callback, void *userData) {
data->timerInfoCallback = callback;
data->timerCallbackData = userData;
}