summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Nazaryev <sergey@nazaryev.ru>2016-04-24 13:09:54 +0000
committerSergey Nazaryev <sergey@nazaryev.ru>2016-04-24 13:09:54 +0000
commitd48b2b23938399e00dc796e05f7ea2a8b51b7c98 (patch)
tree4e249349f5957a01656b27ee8b7c4fbf56209a0f
parent87534aac9928ca4f6f59540b23f5e906424b13f6 (diff)
downloadpa1-d48b2b23938399e00dc796e05f7ea2a8b51b7c98.zip
pa1-d48b2b23938399e00dc796e05f7ea2a8b51b7c98.tar.gz
pa1-d48b2b23938399e00dc796e05f7ea2a8b51b7c98.tar.bz2
Synced with pa2HEADmaster
-rw-r--r--.version1
-rw-r--r--Makefile17
-rw-r--r--child.c2
-rw-r--r--child.h2
-rw-r--r--dist.c23
-rw-r--r--dist.h26
-rw-r--r--ipc.c20
-rw-r--r--main.h18
-rw-r--r--pa1.c (renamed from main.c)30
-rw-r--r--parent.c2
-rw-r--r--parent.h2
11 files changed, 69 insertions, 74 deletions
diff --git a/.version b/.version
new file mode 100644
index 0000000..e673a57
--- /dev/null
+++ b/.version
@@ -0,0 +1 @@
+38f0c5ce6aa00c87b4fb0155fc3889e64d1ee93d
diff --git a/Makefile b/Makefile
index 152615f..3fd7eb4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,22 +1,13 @@
CFLAGS = -std=c99 -Wall -Wpedantic
CC = gcc
-all: dist
+all: pa1
-dist: main.o parent.o child.o ipc.o
+pa1: dist.o pa1.o parent.o child.o ipc.o
$(CC) -o $@ $^
-main.o: main.c
- $(CC) -c -o $@ $< $(CFLAGS)
-
-parent.o: parent.c
- $(CC) -c -o $@ $< $(CFLAGS)
-
-child.o: child.c
- $(CC) -c -o $@ $< $(CFLAGS)
-
-ipc.o: ipc.c
+%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
clean:
- rm -f *.o dist
+ rm -f *.o pa1
diff --git a/child.c b/child.c
index 6e5037b..e3885ed 100644
--- a/child.c
+++ b/child.c
@@ -1,7 +1,7 @@
#include <stdint.h>
#include <string.h>
-#include "main.h"
+#include "dist.h"
#include "ipc.h"
#include "pa1.h"
diff --git a/child.h b/child.h
index 0af7ab4..1aecf78 100644
--- a/child.h
+++ b/child.h
@@ -3,7 +3,7 @@
#include <stdint.h>
-#include "main.h"
+#include "dist.h"
void child_workflow( dist_info_t *info, uint8_t id );
diff --git a/dist.c b/dist.c
new file mode 100644
index 0000000..3a76bf3
--- /dev/null
+++ b/dist.c
@@ -0,0 +1,23 @@
+#include "dist.h"
+
+void close_redundant_pipes( dist_info_t *info, uint8_t id ) {
+ uint8_t i, j;
+ for(i = 0; i < info->proccnt; i++) {
+ uint8_t base = i * (info->proccnt-1); // base of block of pipes for this process
+
+ if( i == id ) {
+ for(j = 0; j < info->proccnt - 1; j++)
+ close( info->pipes[base + j][0] ); // close only read descriptor of ( ID -> Y )
+ } else {
+ uint8_t idx = ( id > i ? id - 1: id ); // index number in block of pipes for ( X -> ID )
+ for(j = 0; j < info->proccnt - 1; j++) {
+ if( j == idx ) {
+ close( info->pipes[base + j][1] ); // close only write descriptor of ( X -> ID )
+ } else {
+ close( info->pipes[base + j][0] ); // close read descriptor of ( X -> Y, where Y != ID )
+ close( info->pipes[base + j][1] ); // close write descriptor of ( X -> Y, where Y != ID )
+ }
+ }
+ }
+ }
+}
diff --git a/dist.h b/dist.h
new file mode 100644
index 0000000..c9b72c3
--- /dev/null
+++ b/dist.h
@@ -0,0 +1,26 @@
+#ifndef __IFMO_DISTRIBUTED_CLASS_DIST__H
+#define __IFMO_DISTRIBUTED_CLASS_DIST__H
+
+#include "main.h"
+#include "pa1.h"
+
+#define MAX_PIPES ( MAX_PROCCNT ) * ( MAX_PROCCNT - 1 )
+
+typedef struct {
+ int pipes[MAX_PIPES][2];
+ uint8_t proccnt;
+ uint8_t x;
+
+ FILE* events_log;
+ FILE* pipes_log;
+ pid_t parent_pid;
+} dist_info_t;
+
+typedef struct {
+ int id;
+ dist_info_t *dist_info;
+} my_info_t;
+
+void close_redundant_pipes( dist_info_t *info, uint8_t id );
+
+#endif
diff --git a/ipc.c b/ipc.c
index 0b7a9d7..b208067 100644
--- a/ipc.c
+++ b/ipc.c
@@ -1,10 +1,11 @@
#define _DEFAULT_SOURCE
#include <stdint.h>
+#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "ipc.h"
-#include "main.h"
+#include "dist.h"
int send(void * self, local_id dst, const Message * msg) {
if( self == NULL )
@@ -27,7 +28,8 @@ int send(void * self, local_id dst, const Message * msg) {
int wr_dscr = me->dist_info->pipes[idx][1];
size_t wr_size = sizeof(msg->s_header) + msg->s_header.s_payload_len;
- if( write( wr_dscr, &msg, wr_size ) != wr_size )
+
+ if( write( wr_dscr, msg, wr_size ) != wr_size )
return 1;
return 0;
@@ -94,7 +96,7 @@ int receive_any(void * self, Message * msg) {
return 1;
uint8_t i, j = 0;
- int descriptors[MAX_PROCCNT - 1];
+ int *descriptors = (int *)malloc( sizeof(int) * (me->dist_info->proccnt - 1) );
for(i = 0; i < me->dist_info->proccnt; i++) {
// don't see at pipes as ( ID -> Y )
if( me->id == i )
@@ -116,19 +118,13 @@ int receive_any(void * self, Message * msg) {
for( i = 0; i < j; i++ ) {
if( ( size = read( descriptors[i], buf, sizeof( buf ) ) ) > 0 ) {
memcpy( msg, buf, sizeof( *msg ) );
- /*switch( msg->s_header.s_type ) {
- case STARTED:
- fprintf( stderr, "[ID: %d] Message STARTED received\n", me->id );
- break;
-
- case DONE:
- fprintf( stderr, "[ID: %d] Message DONE received\n", me->id );
- break;
- }*/
+ free( descriptors );
return 0;
}
}
usleep(100000); // sleep 100 ms
}
+
+ free( descriptors );
return 1;
}
diff --git a/main.h b/main.h
index c746d75..42e288e 100644
--- a/main.h
+++ b/main.h
@@ -8,23 +8,5 @@
#define MAX_X ( 10 )
#define MAX_PROCCNT ( MAX_X + 1 )
-#define MAX_PIPES ( MAX_PROCCNT ) * ( MAX_PROCCNT - 1 )
-
-typedef struct {
- int pipes[MAX_PIPES][2];
- uint8_t proccnt;
- uint8_t x;
-
- FILE* events_log;
- FILE* pipes_log;
- pid_t parent_pid;
-} dist_info_t;
-
-typedef struct {
- int id;
- dist_info_t *dist_info;
-} my_info_t;
-
-void close_redundant_pipes( dist_info_t *info, uint8_t id );
#endif // __IFMO_DISTRIBUTED_CLASS_MAIN__H
diff --git a/main.c b/pa1.c
index 8c8ec08..6bd3d59 100644
--- a/main.c
+++ b/pa1.c
@@ -7,9 +7,11 @@
#include <string.h>
#include "main.h"
+#include "common.h"
+
#include "child.h"
#include "parent.h"
-#include "common.h"
+#include "dist.h"
static char* app_name;
@@ -18,32 +20,6 @@ void usage( FILE* output ) {
fprintf(output, "`x` must be int between 1 and %d\n", MAX_X);
}
-void close_redundant_pipes( dist_info_t *info, uint8_t id ) {
- uint8_t i, j;
- for(i = 0; i < info->proccnt; i++) {
- uint8_t base = i * (info->proccnt-1); // base of block of pipes for this process
-
- if( i == id ) {
- for(j = 0; j < info->proccnt - 1; j++) {
- // fprintf( stderr, "Read descriptor %d closed for id %d\n", info->pipes[base + j][0], id );
- close( info->pipes[base + j][0] ); // close only read descriptor of ( ID -> Y )
- }
- } else {
- uint8_t idx = ( id > i ? id - 1: id ); // index number in block of pipes for ( X -> ID )
- for(j = 0; j < info->proccnt - 1; j++) {
- if( j == idx ) {
- close( info->pipes[base + j][1] ); // close only write descriptor of ( X -> ID )
- // fprintf( stderr, "Write descriptor %d of pipe (%d -> %d) closed for id %d\n", info->pipes[base + j][1], i, id, id );
- } else {
- // fprintf( stderr, "LOL\n\n\n");
- close( info->pipes[base + j][0] ); // close read descriptor of ( X -> Y, where Y != ID )
- close( info->pipes[base + j][1] ); // close write descriptor of ( X -> Y, where Y != ID )
- }
- }
- }
- }
-}
-
int main(int argc, char* argv[]) {
uint8_t i;
dist_info_t info;
diff --git a/parent.c b/parent.c
index bf72edb..dc11c65 100644
--- a/parent.c
+++ b/parent.c
@@ -2,7 +2,7 @@
#include <sys/wait.h>
#include <stdlib.h>
-#include "main.h"
+#include "dist.h"
#include "ipc.h"
void parent_workflow( dist_info_t *info ) {
diff --git a/parent.h b/parent.h
index eec9c49..f30ce2e 100644
--- a/parent.h
+++ b/parent.h
@@ -1,7 +1,7 @@
#ifndef __IFMO_DISTRIBUTED_CLASS_PARENT__H
#define __IFMO_DISTRIBUTED_CLASS_PARENT__H
-#include "main.h"
+#include "dist.h"
void parent_workflow( dist_info_t *info );