-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
217 lines (182 loc) · 5.17 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include<stdio.h>
#include<stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include<time.h>
#include <errno.h>
#include <string.h>
#define FIFO_NAME "myFiFo"
void forkChildren (int); //a simple function to handle multi children
void sendData(int [],int); //a simple fuction to send data through FIFO, pass an array and it's size
void readData(int); //a simple fuction to read data through FIFO, onle pass the
int acquireLock (char *);
void releaseLock (int );
void findDub(int, int); //each child will use this function to process it's 2M integers to find dublicates
void initDublicatesCounter(); //the global hashed array, all children will call this function to init their counters
void initArray(); //fill "array" with 10000000 random integer
static int array[10000000]; //parent will initialize this array with 10M random integers between -5M and 5M
static int parts[2000000]; //each child will process it's 2000000 integers from this array
static int pos[5000000]; //between 0 and 5000000
static int neg[5000000]; //between -5000000 and -1
int dublicates=0;
int me=0; //each child will save it's number here, 0 is parent
int fd; //Global file handler
int main()
{
long int i;
initArray();
initDublicatesCounter();
forkChildren(5);
if(me==0){
printf("Parent starting\n");
sendData(array,9999999);
}
int pid;
while (pid = waitpid(-1, NULL, 0)) {
if (errno == ECHILD) {
break;
}
}
}
void forkChildren (int nChildren) {
int i;
pid_t pid;
for (i = 1; i <= nChildren; i++) {
pid = fork();
if (pid == -1) {
printf("Can't fork child number %d\n",i);
return;
}
if (pid == 0 && i==1) {
me = 1;
printf("I am child: %d PID: %d\n",i, getpid());
initDublicatesCounter();
readData(1999999);
findDub(0,1999999);
return;
}
if (pid == 0 && i==2) {
me = 2;
printf("I am child: %d PID: %d\n",i, getpid());
initDublicatesCounter();
readData(1999999);
findDub(0,1999999);
return;
}
if (pid == 0 && i==3) {
me = 3;
printf("I am child: %d PID: %d\n",i, getpid());
initDublicatesCounter();
readData(1999999);
findDub(0,1999999);
return;
}
if (pid == 0 && i==4) {
me = 4;
printf("I am child: %d PID: %d\n",i, getpid());
initDublicatesCounter();
readData(1999999);
findDub(0,1999999);
return;
}
if (pid == 0 && i==5) {
me = 5;
//printf("I am child: %d PID: %d\n",i, getpid());
readData(1999999);
findDub(0,1999999);
return;
}
}
}
void sendData(int data[],int lim){
int num,dataCounter,lock;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
fd = open(FIFO_NAME, O_WRONLY); //here the program will stop excution
//and keep waiting for a process to open the
//file and start reading
printf("Sending all 10 million integers\n");
for(dataCounter=0;dataCounter<=lim;dataCounter++){
if ((num = write(fd, &data[dataCounter], sizeof(int))) == -1)
perror("Can't communicate");
else{
//data was sent successfuly
}
}
printf("sent all data\n");
}
void readData(int lim){
int data;
int i,num,lock;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
if((lock = acquireLock (FIFO_NAME)) < 0){
printf("Child %d waiting lock\n",me);
while((lock = acquireLock (FIFO_NAME)) < 0) {
}
}
printf("Child %d got lock\n",me);
printf("Child %d started reading data\n",me);
fd = open(FIFO_NAME, O_RDONLY,O_NONBLOCK);
for(i=0;i<=lim;i++){
if ((num = read(fd, &data, sizeof(int))) == -1){
perror("Can't read data");
}
else {
parts[i]=data;
//printf("child %d reading: \"%d\"\n",me, data);
}
}
releaseLock(lock);
printf("Child %d released lock and read it's data\n",me);
}
int acquireLock (char *fileSpec) {
int lockFd;
if ((lockFd = open (fileSpec, O_RDWR, 0666)) < 0)
return -1;
if (flock (lockFd, LOCK_EX | LOCK_NB) < 0) {
close (lockFd);
return -1;
}
return lockFd;
}
void releaseLock (int lockFd) {
flock (lockFd, LOCK_UN);
close(fd);
}
void findDub(int from, int to){
int i,counter;
for(i=from;i<=to;i++){
if(parts[i]>=0){
pos[parts[i]]++;
}
if(parts[i]<0){
long int conv = parts[i];
conv=abs(conv);
neg[conv]++;
}
}
for(counter=0;counter<=5000000;counter++){
if(pos[counter]>0 || neg[counter]>0){
dublicates++;
}
}
printf("\nDublicates = %d\n",dublicates);
}
void initDublicatesCounter(){
int counter=0;
for(counter=0;counter<=5000000;counter++){
pos[counter]=0;
neg[counter]=0;
}
}
void initArray(){
int i;
srand (time(NULL));
for(i=0;i<=9999999;i++){
if(i==5000000 || i==2500000 || i==7500000){
srand (time(NULL));
}
array[i] = (rand() % (5000000-(-5000000)+1))+(-5000000);
}
}