-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2db.cpp
More file actions
130 lines (118 loc) · 2.73 KB
/
Copy pathtest2db.cpp
File metadata and controls
130 lines (118 loc) · 2.73 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
130
#ifndef TEST2DB
#define TEST2DB
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
//#include "askdbfunc.cpp"
using namespace std;
//namespace askdbfunc {char* askdb(char*,int);};
char* askdb(char* word, int tablenum);
int main(int argc, char** argv)
{
int pid, status, len, sem_id, sem_id2;
//char* dbout = new char[512];
string dbout;
struct sembuf SB;
if (argc < 2)
{
cout << "Enter word as argument for searching\n";
return 0;
}
sem_id = semget(IPC_PRIVATE, 1, IPC_CREAT);
sem_id2 = semget(IPC_PRIVATE, 1, IPC_CREAT);
if (sem_id == -1)
{
cout << "sem1 error\n";
return 1;
}
else if (sem_id2 == -1)
{
cout << "sem2 error\n";
return 1;
}
SB.sem_num = 0;
SB.sem_op = 1;
SB.sem_flg = 0;
semop(sem_id, &SB, 1);
//int chan[2];
//char buf[300];
if (!(pid = fork()))
{
//strcpy(files.sa_data, "socket.soc");
//int sockidW2 = socket(AF_UNIX, SOCK_STREAM, 0);
//connect(sockidW2, &files, (strlen(files.sa_data) + sizeof(files.sa_family)));
//recv(sockidW2, (char*) buf, BUFSIZE,0);
SB.sem_num = 0;
SB.sem_op = -1;
SB.sem_flg = 0;
dbout = askdb(argv[1], 2);
semop(sem_id, &SB, 1);
printf("Worker2\n");
std::cout << "table2:\n";
std::cout << dbout;
SB.sem_num = 0;
SB.sem_op = 1;
SB.sem_flg = 0;
semop(sem_id, &SB, 1);
SB.sem_num = 0;
SB.sem_op = 1;
SB.sem_flg = 0;
semop(sem_id2, &SB, 1);
exit(0);
//send(sockidW2, askdb(buf,2), BUFSIZE,0);
}
if (!(pid = fork()))
{
//strcpy(files.sa_data, "socket.soc");
//int sockidW3 = socket(AF_UNIX, SOCK_STREAM, 0);
//connect(sockidW3, &files,(strlen(files.sa_data) + sizeof(files.sa_family)));
//recv(sockidW3, (char*) buf, BUFSIZE,0);
SB.sem_num = 0;
SB.sem_op = -1;
SB.sem_flg = 0;
dbout = askdb(argv[1], 3);
semop(sem_id, &SB, 1);
printf("Worker3\n");
std::cout << "table3:\n";
std::cout << dbout;
SB.sem_num = 0;
SB.sem_op = 1;
SB.sem_flg = 0;
semop(sem_id, &SB, 1);
SB.sem_num = 0;
SB.sem_op = 1;
SB.sem_flg = 0;
semop(sem_id2, &SB, 1);
exit(0);
//send(sockidW3, askdb(buf,3), BUFSIZE,0);
}
//strcpy(files.sa_data, "socket.soc");
//int sockidW1 = socket(AF_UNIX, SOCK_STREAM, 0);
//connect(sockidW1, &files, (strlen(files.sa_data) + sizeof(files.sa_family)));
//recv(sockidW1, (char*) buf, BUFSIZE,0);
SB.sem_num = 0;
SB.sem_op = -1;
SB.sem_flg = 0;
dbout = askdb(argv[1], 1);
semop(sem_id, &SB, 1);
printf("Worker1\n");
std::cout << "table1:\n";
std::cout << dbout;
SB.sem_num = 0;
SB.sem_op = 1;
SB.sem_flg = 0;
semop(sem_id, &SB, 1);
SB.sem_num = 0;
SB.sem_op = -2;
SB.sem_flg = 0;
semop(sem_id2, &SB, 1);
//send(sockidW1, askdb(buf,1), BUFSIZE,0);
//delete[] dbout;
return 0;
}
#endif