-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgs_tets.c
71 lines (66 loc) · 2.2 KB
/
msgs_tets.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
/*
Assignment 3 CSE 531
Leena Panchmatia ASUID : 1208107975
Jaideep Gour ASUID : 1208721926
*/
#include<stdlib.h>
#include<stdio.h>
#include "msgs.h"
/* Server function
Blocks if no message for it
Removes first received message
Sends reply to corresponding client
*/
void serverport0(){
struct message *rec_message;
while(1) {
printf("\nServer Port [%d] : Listening\n", SERVER_PORT0);
rec_message = receive(SERVER_PORT0);
printf("\nServer Port [%d] : Sending Message to %d\n", SERVER_PORT0, rec_message->message_arr[1]);
send(rec_message->message_arr[1], SERVER_PORT0);
}
}
void serverport1(){
struct message *rec_message;
while(1) {
printf("\nServer Port [%d] : Listening\n", SERVER_PORT1);
rec_message = receive(SERVER_PORT1);
printf("\nServer Port [%d] : Sending Message to %d\n", SERVER_PORT1, rec_message->message_arr[1]);
send(rec_message->message_arr[1], SERVER_PORT1);
}
}
/* Client function
Randomly chooses a port
Sends message from that port
Waits for reply on that port
Recieves the messae and prints it
Then chooses another port
*/
void client(){
int my_port;
struct message *rec_message;
rec_message = (struct message*)malloc(sizeof(struct message));
while(1) {
my_port = rand() % 98;
printf("\nClient Port [%d] : Sending Message to %d\n", my_port, SERVER_PORT0);
send(SERVER_PORT0, my_port);
rec_message = receive(my_port);
printf("\nClient Port [%d] : Recieving Message from %d\n", my_port, SERVER_PORT0);
print_message(rec_message);
free(rec_message);
sleep(1);
}
}
int main()
{
Q = InitQ(Q); //Modification to initialize the run queue
int i;
for(i = 0; i < 100; i++)
create_port(10, i); // Initializes all the ports
start_thread(serverport0); // Only argument passed to thead_start is function name
start_thread(client);
start_thread(client);
start_thread(client);
start_thread(client);
run(); //Since run function is non-terminating we do not require an infinite loop in main
}