From cb1f33c6ae840e8dc0f43518daf76e6ed01034f0 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Mon, 08 Dec 2014 19:48:57 +0000
Subject: [PATCH] Fixed race condition in server
---
src/server.c | 26 +++++++++++++++++---------
1 files changed, 17 insertions(+), 9 deletions(-)
diff --git a/src/server.c b/src/server.c
index c802f84..e927011 100644
--- a/src/server.c
+++ b/src/server.c
@@ -9,6 +9,7 @@
#include "mini_blas.h"
#include "utils.h"
+#include "parser.h"
#include "server.h"
#include "connected_layer.h"
#include "convolutional_layer.h"
@@ -50,20 +51,22 @@
void read_all(int fd, char *buffer, size_t bytes)
{
+ //printf("Want %d\n", bytes);
size_t n = 0;
while(n < bytes){
int next = read(fd, buffer + n, bytes-n);
- if(next < 0) error("read failed");
+ if(next <= 0) error("read failed");
n += next;
}
}
void write_all(int fd, char *buffer, size_t bytes)
{
+ //printf("Writ %d\n", bytes);
size_t n = 0;
while(n < bytes){
int next = write(fd, buffer + n, bytes-n);
- if(next < 0) error("write failed");
+ if(next <= 0) error("write failed");
n += next;
}
}
@@ -78,11 +81,11 @@
void handle_connection(void *pointer)
{
- printf("New Connection\n");
connection_info info = *(connection_info *) pointer;
+ free(pointer);
+ printf("New Connection\n");
int fd = info.fd;
network net = info.net;
- ++*(info.counter);
int i;
for(i = 0; i < net.n; ++i){
if(net.types[i] == CONVOLUTIONAL){
@@ -126,14 +129,16 @@
listen(fd, 10);
struct sockaddr_in client; /* remote address */
socklen_t client_size = sizeof(client); /* length of addresses */
- connection_info info;
- info.net = net;
- info.counter = &counter;
while(1){
+ connection_info *info = calloc(1, sizeof(connection_info));
+ info->net = net;
+ info->counter = &counter;
pthread_t worker;
int connection = accept(fd, (struct sockaddr *) &client, &client_size);
- info.fd = connection;
- pthread_create(&worker, NULL, (void *) &handle_connection, &info);
+ info->fd = connection;
+ pthread_create(&worker, NULL, (void *) &handle_connection, info);
+ ++counter;
+ if(counter%1000==0) save_network(net, "cfg/nist.part");
}
}
@@ -164,6 +169,7 @@
/* send a message to the server */
int i;
+ //printf("Sending\n");
for(i = 0; i < net.n; ++i){
if(net.types[i] == CONVOLUTIONAL){
convolutional_layer layer = *(convolutional_layer *) net.layers[i];
@@ -181,6 +187,7 @@
memset(layer.weight_updates, 0, layer.inputs*layer.outputs*sizeof(float));
}
}
+ //printf("Sent\n");
for(i = 0; i < net.n; ++i){
if(net.types[i] == CONVOLUTIONAL){
@@ -201,5 +208,6 @@
push_connected_layer(layer);
}
}
+ //printf("Updated\n");
close(fd);
}
--
Gitblit v1.10.0