-
Notifications
You must be signed in to change notification settings - Fork 1
/
conflicting_writes.c
77 lines (72 loc) · 2.08 KB
/
conflicting_writes.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
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <netdb.h>
#include <errno.h>
#include <pthread.h>
#include <time.h>
#include "network.h"
#include "constants.h"
#define LISTEN 1
#define DONT 0
void conflicting_writes(host_port *host) {
int connection, err;
err = make_connection_with(host->ip, host->port, &connection);
if(err < 0) {
problem("Connection failure.\n");
exit(-1);
}
printf("Made connection in second thread, file descriptor is %d\n", connection);
err = DONT;
safe_send(connection, &err, sizeof(int));
err = 3000;
safe_send(connection, &err, sizeof(int));
}
void receive_int(int *connection) {
int incoming = 0;
safe_recv(*connection, &incoming, sizeof(int));
printf("Received %d\n", incoming);
}
void listen_for_connection(host_port *info) {
pthread_t thread;
int connection, connect_result, listener;
connect_result = set_up_listener(info->port, &listener);
connect_result = -1;
while(1) {
do {
connect_result = wait_for_connection(listener, &connection);
} while(connect_result < 0);
connect_result = 0;
safe_recv(connection, &connect_result, sizeof(int));
if(connect_result) {
pthread_create(&thread, NULL, (void *(*)(void *))receive_int, (void *)&connection);
}
}
}
int main(int argc, char **argv) {
int connection, err;
pthread_t thread;
host_port host;
struct timespec req;
if(argc < 3) {
host.port = atoi(argv[1]);
pthread_create(&thread, NULL, (void *(*)(void *))listen_for_connection, &host);
pthread_join(thread, NULL);
} else {
strcpy(host.ip, argv[1]);
host.port = atoi(argv[2]);
make_connection_with(host.ip, host.port, &connection);
printfl("Made connection in main thread, file descriptor is %d", connection);
err = LISTEN;
safe_send(connection, &err, sizeof(int));
pthread_create(&thread, NULL, (void *(*)(void *))conflicting_writes, &host);
req.tv_sec = 2;
pthread_join(thread, NULL);
}
}