-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsorter.c
104 lines (93 loc) · 2.71 KB
/
sorter.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
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include "record.h"
// int main(int argc, char* argv[])
// {
// /* store the arguments in variables so that the code becomes more readable */
// char* inputfile = argv[1];
// char* number_of_records = argv[2];
// char* left = argv[3];
// char* right = argv[4];
// char* sort_function = argv[5];
// char* attribute = argv[6];
// char* pipe_fd = argv[7];
// char* children_pipes = argv[9];
//
// /* process id of the coach, used to send a signal to it */
// pid_t coach_pid = atoi(argv[8]);
//
// /* decide which sorting algorithm will be used */
// int flag_quicksort = 1;
// if (strcmp(sort_function, "q") != 0) {
// flag_quicksort = 0;
// }
//
// /* create new process */
// pid_t child_pid = fork();
//
// if (child_pid < 0) {
//
// printf("Something went wrong with fork() in sorter.c: pipe_fd = %d\n", atoi(pipe_fd));
// exit(-1);
//
// } else if (child_pid != 0) {
//
// /* wait for the child process to end */
// int returnStatus;
// wait(&returnStatus);
//
// if (returnStatus == -1) {
// printf("The child process terminated with an error in sorter.c!\n");
// exit(-1);
// }
//
// } else {
//
// /* execute child process -> sort algorithm */
// if (flag_quicksort == 1) {
// char *args[] = {"./quicksort", inputfile, attribute, left, right, pipe_fd, children_pipes, NULL};
// execvp(args[0], args);
// } else {
// char *args[] = {"./heapsort", inputfile, attribute, left, right, pipe_fd, children_pipes, NULL};
// execvp(args[0], args);
// }
//
// }
//
//
// /* send signal SIGUSR2 to the root */
// kill(coach_pid, SIGUSR2);
//
// return 0;
// }
int main(int argc, char* argv[])
{
/* store the arguments in variables so that the code becomes more readable */
char* inputfile = argv[1];
char* number_of_records = argv[2];
char* left = argv[3];
char* right = argv[4];
char* sort_function = argv[5];
char* attribute = argv[6];
char* pipe_fd = argv[7];
char* children_pipes = argv[9];
/* process id of the coach, used to send a signal to it */
char* coach_pid = argv[8];
/* decide which sorting algorithm will be used */
int flag_quicksort = 1;
if (strcmp(sort_function, "q") != 0) {
flag_quicksort = 0;
}
/* execute child process -> sort algorithm */
if (flag_quicksort == 1) {
char *args[] = {"./quicksort", inputfile, attribute, left, right, pipe_fd, children_pipes, coach_pid, NULL};
execvp(args[0], args);
} else {
char *args[] = {"./heapsort", inputfile, attribute, left, right, pipe_fd, children_pipes, coach_pid, NULL};
execvp(args[0], args);
}
return 0;
}