diff options
-rw-r--r-- | .version | 1 | ||||
-rw-r--r-- | Makefile | 17 | ||||
-rw-r--r-- | child.c | 2 | ||||
-rw-r--r-- | child.h | 2 | ||||
-rw-r--r-- | dist.c | 23 | ||||
-rw-r--r-- | dist.h | 26 | ||||
-rw-r--r-- | ipc.c | 20 | ||||
-rw-r--r-- | main.h | 18 | ||||
-rw-r--r-- | pa1.c (renamed from main.c) | 30 | ||||
-rw-r--r-- | parent.c | 2 | ||||
-rw-r--r-- | parent.h | 2 |
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 @@ -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 @@ -1,7 +1,7 @@ #include <stdint.h> #include <string.h> -#include "main.h" +#include "dist.h" #include "ipc.h" #include "pa1.h" @@ -3,7 +3,7 @@ #include <stdint.h> -#include "main.h" +#include "dist.h" void child_workflow( dist_info_t *info, uint8_t id ); @@ -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 ) + } + } + } + } +} @@ -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 @@ -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; } @@ -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 @@ -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; @@ -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 ) { @@ -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 ); |