-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.h
69 lines (61 loc) · 1.4 KB
/
misc.h
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
#ifndef MISC_H
#define MISC_H
#include <string>
#include <fstream>
#include <sys/socket.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <pthread.h>
#include "family.h"
using namespace std;
extern pthread_mutex_t *mx;
extern vector<string> ip, mac;
int setMutex(){
int fd = shm_open(SHM_FILENAME, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if(fd == -1)
return -1;
if(ftruncate(fd, sizeof(pthread_mutex_t)) == -1)
return -1;
mx = (pthread_mutex_t *) mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if((long)mx == MAP_SHARED)
return -1;
close(fd);
pthread_mutexattr_t mutexattr;
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(mx, &mutexattr);
return 0;
}
void createChildren(){
int pid;
string c;
fstream f("IPs.txt", fstream::in);
f >> c;
while(!f.eof()){
int s[2];
socketpair(AF_UNIX, SOCK_STREAM, (int)NULL, s);
sd.push_back(s[0]);
ip.push_back(getIp(c));
mac.push_back(getMac(c));
if((pid = fork()) == -1){
string response;
response += "\nError at fork()";
send(response);
}
if(pid == 0){
f.close();
sd.clear();
ip.clear();
close(s[0]);
childPlay(getIp(c), getMac(c), s[1]);
exit(0);
}
close(s[1]);
f >> c;
}
f.close();
}
#endif