-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.c
More file actions
129 lines (122 loc) · 3.68 KB
/
process.c
File metadata and controls
129 lines (122 loc) · 3.68 KB
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
#include "kmeans.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/shm.h>
int main(void){
int testCase;
int iterNum;
int clusterNum;
int pointNum;
int key = KEY;
int status;
int pipenum = 2*PROCNUM;
int fd[2*PROCNUM];
char* fifoName[2*PROCNUM];
scanf("%d", &testCase);
for (int i=0;i<pipenum;i++){
char tmpbuf[5];
sprintf(tmpbuf, "%d", i);
fifoName[i] = (char*)malloc(sizeof(char)*20);
strcpy(fifoName[i], "./");
strcat(fifoName[i], tmpbuf);
}
for(int i=0;i<testCase;i++){
scanf("%d", &iterNum);
scanf("%d", &clusterNum);
scanf("%d", &pointNum);
if(pointNum<NOPARAL_NUM){/*calculate in noparallel way when the entered
pointNum is less then NOPARAL_NUL*/
noparallel_process(i, iterNum, clusterNum, pointNum);
continue;
}
for (int j=0;j<pipenum;j++){
remove(fifoName[j]); /*if there's already named fifo files exist,
remove them*/
mkfifo(fifoName[j], 0666); /*making named fifo file*/
}
int shm_point = shmget(key, sizeof(Point)*pointNum,
IPC_CREAT|0666|IPC_EXCL);
if (shm_point == -1){/*shared memory that can be accessed by the given key
is already exists! so failed to make new shared
memory*/
perror("fatal error!: ");
}
int shm_cluster = shmget(key+1, sizeof(Point)*clusterNum,
IPC_CREAT|0666|IPC_EXCL);
if (shm_cluster == -1){
perror("fatal error!: ");
}
Point* point_addr = shmat(shm_point, NULL, 0);
Point* cluster_addr = shmat(shm_cluster, NULL, 0);
for (int j=0;j<pointNum;j++){
scanf("%lf %lf", &(point_addr[j].xpos), &(point_addr[j].ypos));
point_addr[j].cluster = -1;
}
long before = getTime();
for (int j=0;j<clusterNum;j++){
cluster_addr[j].xpos = point_addr[j].xpos;
cluster_addr[j].ypos = point_addr[j].ypos;
cluster_addr[j].cluster = -1;
}
for (int j=0;j<PROCNUM;j++){/*forking child processes that will execute
determine cluster function*/
int fn = fork();
if (fn == 0){
char arg_key[100];
char arg_psize[100];
char arg_csize[100];
char arg_index[100];
char arg_iter[100];
sprintf(arg_key,"%d",key);
sprintf(arg_psize, "%d", pointNum);
sprintf(arg_csize, "%d", clusterNum);
sprintf(arg_index,"%d",j);
sprintf(arg_iter, "%d", iterNum);
execl("./process_child", "./process_child", arg_key, arg_psize, arg_csize, arg_index, arg_iter, NULL);
}
}
for (int j=0;j<PROCNUM;j++){/*opening named fifo in parents file*/
fd[j] = open(fifoName[j], O_RDONLY);
fd[j+PROCNUM] = open(fifoName[j+PROCNUM], O_WRONLY);
}
for (int j=0;j<iterNum;j++){
char myBuf[5] = "p";
for (int k=0;k<PROCNUM;k++){/*sending signal to child process to
start their work by writing on the
named fifo*/
write(fd[PROCNUM+k], myBuf, sizeof(myBuf));
}
for (int k=0;k<PROCNUM;k++){
read(fd[k],myBuf,sizeof(myBuf));/*recieving signal from
children*/
}
p_clusterRenewal(point_addr, cluster_addr, pointNum, clusterNum);
}
while(wait(&status)>0);
long after = getTime();
long consumedTime = after - before;
printf("Test Case #%d\n", i);
printf("%ld microseconds\n", consumedTime);
for (int j=0;j<pointNum;j++){
printf("%d\n", point_addr[j].cluster);
}
printf("%dth test\n",i) ;
printf("time: %ld\n", consumedTime);
shmctl(shm_point, IPC_RMID, 0);/*make shared memory free*/
shmctl(shm_cluster, IPC_RMID, 0);
shmdt(point_addr);
shmdt(cluster_addr);
for (int j=0;j<2*PROCNUM;j++){/*closing named fifos*/
int cs = close(fd[j]);
}
}
for (int j=0;j<pipenum;j++){
remove(fifoName[j]);/*removing remaining fifos*/
}
}