-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmultisort.c
executable file
·104 lines (89 loc) · 2.16 KB
/
multisort.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <assert.h>
#define MAXLINE 1000000
#define MAXWORDSZ 100
int
readwords(char **words, char *inputfile)
{
FILE *fp = fopen(inputfile, "r");
int n_words = 0;
while (1) {
char *word = (char *)malloc(MAXWORDSZ);
if (!fgets(word, MAXWORDSZ, fp)) {
break;
}
if (word[strlen(word)-1] != '\n') {
printf("Warning: word is longer than %d characters\n", MAXWORDSZ);
}
words[n_words] = word;
n_words++;
assert(n_words < MAXLINE);
}
fclose(fp);
return n_words;
}
int
mergewords(char **l1, int l1_len, char **l2, int l2_len, char *outfile)
{
int l1_ind = 0;
int l2_ind = 0;
while (l1_ind < l1_len && l2_ind < l2_len) {
if (strncmp(l1[l1_ind], l2[l2_ind], MAXWORDSZ) < 0) {
printf("%s", l1[l1_ind]);
l1_ind++;
}else {
printf("%s", l2[l2_ind]);
l2_ind++;
}
}
while (l1_ind < l1_len) {
printf("%s", l1[l1_ind]);
l1_ind++;
}
while (l2_ind < l2_len) {
printf("%s", l2[l2_ind]);
l2_ind++;
}
}
int
qsort_cmp(const void *a, const void *b)
{
return strcmp((const char *)a, (const char *)b);
}
int
main(int argc, char **argv)
{
if (argc < 2) {
printf("usage: ./multisort inputfile > outfile \n");
exit(1);
}
char **words = (char **)malloc(sizeof(char *)*MAXLINE);
int n_words = readwords(words, argv[1]);
int start = 0;
int len = n_words;
pid_t pid;
if ((pid = fork()) != 0) {
//parent sort the first half of the [start, start + len] section of words
len = len/2;
qsort(words + start, len, sizeof(char *), qsort_cmp);
fprintf(stderr, "pid-%d has sorted [%d, %d)\n", getpid(), start, start + len);
} else {
start = start + len/2;
len = len - len/2;
qsort(words + start, len, sizeof(char *), qsort_cmp);
fprintf(stderr, "pid-%d has sorted [%d, %d)\n", getpid(), start, start + len);
exit(0);
}
//parent waits for child
int status;
waitpid(pid, &status, 0);
fprintf(stderr, "both sorting has finished\n");
fprintf(stderr, "merging both lists\n\n\n");
//merge the two sorted list into one
mergewords(words, n_words/2, words+(n_words/2), n_words-(n_words/2), argv[1]);
}