-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_MultiProc.c
133 lines (108 loc) · 2.75 KB
/
server_MultiProc.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
/*Marwan Mostafa mam024 11305332*/
#include <network.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/select.h>
struct sockaddr_storage their_addr;
socklen_t addr_size;
int count;
/*array of usable pipes for IPC*/
int fd[1024][2];
void connection(int c)
{
int r;
int s;
uint16_t buf[64];
uint16_t response[64];
/*recv msg request from client*/
r = recv(c,buf,sizeof(uint16_t)*64,0);
if (r > 0){
network_to_host(buf,64);
/*send request to main proc*/
write(fd[count][1],buf,sizeof(uint16_t)*64);
sleep(1);
/*get request from main proc*/
read(fd[count][0],response,sizeof(uint16_t)*64);
host_to_network(response,64);
/*send response to client*/
s = send(c,response,sizeof(uint16_t)*64,0);
if (s != -1){
/*close connection*/
close(c);
}
}
close(fd[count][0]);
close(fd[count][1]);
exit(0);
}
void handle_connections(int socket)
{
pid_t p;
int c;
int i;
fd_set readfds;
struct timeval tv;
int rv;
int n;
uint16_t request[64];
uint16_t *response;
int DBL;
int ndb;
tv.tv_sec = 1;
tv.tv_usec = 500000;
fprintf(stdout,"server running using MultiProc\n");
count = 0;
ndb = 0;
while (1){
FD_ZERO(&readfds);
FD_SET(socket, &readfds);
n = socket + 1;
rv = select(n, &readfds, NULL, NULL, &tv);
if (rv == -1) fprintf(stderr,"selcet error");
else if (FD_ISSET(socket, &readfds)){
/*accept connection*/
c = accept(socket, (struct sockaddr *)&their_addr, &addr_size);
fprintf(stdout,"server recieved connection\n");
/*checking return value of accept*/
if (c == -1){
fprintf(stderr,"accept error");
exit(1);
}
/*create pipe for comunication*/
pipe(fd[count]);
/*forking child proc to handle conections requests*/
p = fork();
if (p<0){
fprintf(stderr,"fork error");
exit(1);
}else if (p == 0){
connection(c);
}
count++;
ndb++;
}
/*checking if child procs have a request waiting to be handled*/
for (i = 0;i<count;i++){
FD_ZERO(&readfds);
FD_SET(fd[i][0], &readfds);
n = fd[i][0] + 1;
rv = select(n, &readfds, NULL, NULL, &tv);
if (rv == -1) fprintf(stderr,"selcet error");
else if (FD_ISSET(fd[i][0], &readfds)){
memset(request,0,sizeof(uint16_t)*64);
read(fd[i][0],request,sizeof(uint16_t)*64);
fprintf(stdout,"server recieved request\n");
DBL = get_DBL(request);
if (DBL == -1) DBL = ndb;
response = operate(DBL,request);
write(fd[i][1],response,sizeof(uint16_t)*64);
sleep(1);
fprintf(stdout,"server sent response\n");
free(response);
count--;
close(fd[i][0]);
close(fd[i][1]);
}
}
}
}