-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysortapp.c
179 lines (161 loc) · 4.03 KB
/
mysortapp.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
/*
* Yuriy Koziy
* CS1550
* Multiprocess sorter: Tax records sorter
*/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include "myRecordDef.h"
typedef struct{
char *depth;
char *file_name;
char *attrib;
} args;
/* processes commnd line arguments */
int process_c_args(int argc, char *argv[], args *list);
/* reads the fifo and prints it*/
void read_fifo(int fd, int size);
/* read and print statistics file*/
void read_stat();
int main(int argc, char *argv[])
{
args list;
int fd;
pid_t pid;
int size = 0;
int i = 1;
int nodes_num = 0;
char sz[25];
char pipe_path[30];
if(process_c_args(argc, argv, &list))
{
return -1;
}
size = strtol(list.file_name, NULL, 10);
snprintf(sz, 25, "%d", size);
if(mkfifo ("/tmp/1_p",0666) == -1)
{
perror("Error creating named pipe");
}
pid = fork();
if(pid == 0) //child
{
//SM node arguments: lower bound, upper bound, sorting attribute, file name, node number, current depth, max depth
execl("smnode", "smnode", "0", sz, list.attrib, list.file_name, "1", "0", list.depth, (char *)0);
fflush(stdout);
perror("Can't execute smnode\n");
} else if (pid < 0) //fork failed
{
perror("Fork error!\n");
} else //parent
{
fd = open("/tmp/1_p", O_RDONLY);
read_fifo(fd,size);
close(fd);
}
printf("\n\n ======================= Sorting Statistics ======================= \n\n");
read_stat();
printf("\n\n ================================================================== \n\n");
//remove the pipes when finished
nodes_num = (int)(pow(2,atoi(list.depth)+1)) - 1;
for(i = 1; i <= nodes_num; i++)
{
snprintf(pipe_path, 30, "/tmp/%d_p", i);
unlink(pipe_path);
}
return 0;
}
int process_c_args(int argc, char *argv[], args *list)
{
int c;
if(argc == 1)
{
printf("No arguments provided\n");
printf("./mysortapp -d DepthOfBinTree(1-6) -f FileName -a AttributeNumber(0-3)\n");
return -1;
} else if(argc != 7)
{
printf("Not enough arguments provided\n");
printf("./mysortapp -d DepthOfBinTree(1-6) -f FileName -a AttributeNumber(0-3)\n");
return -1;
}
while((c = getopt (argc, argv, "d:f:a:")) != -1)
{
switch (c)
{
case 'd':
list->depth = optarg;
break;
case 'f':
list->file_name = optarg;
break;
case 'a':
list->attrib = optarg;
break;
case '?':
if (isprint (optopt))
{
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
}
else
{
fprintf (stderr,"Unknown option character `\\x%x'.\n", optopt);
return -1;
}
default:
return -1;
}
}
int depth = atoi(list->depth);
if(depth < 1 || depth > 6)
{
printf("Depth of tree must be between 1 and 6\n");
return -1;
}
int attrib = atoi(list->attrib);
if(attrib < 0 || attrib > 3)
{
printf("Attribute number must be between 0 and 3\n");
return -1;
}
return 0;
}
void read_fifo(int fd, int size)
{
MyRecord record;
int i = 0;
int rd = 0;
for(i = 0; i < size; i++)
{
rd = read(fd, &record, sizeof(record));
if(rd < 0 || rd != sizeof(record))
{
perror("Reading failed!");
break;
}
printf("%d %s %s %d \n",record.ssn, record.FirstName, record.LastName, record.income);
}
return;
}
void read_stat()
{
int c;
FILE *file;
file = fopen("stat.txt", "r");
if (file) {
while ((c = getc(file)) != EOF)
putchar(c);
fclose(file);
}
remove("stat.txt");
return;
}