c - 2 FIFOs 1 Task - Synchronize them -


i have following problem:

i have launch process various terminals can start running program after number of processes has started. in other words, have block somehow processes until right number of processes.

i'm trying using fifos using first process called "server". problem resumed ticket entrance problem: "server" process writing in fifo number of next process. next process take "ticket" , send "server" number++. coded this, basically:

server:

count = 0; fd_w = open ("client", o_wronly); fd_r = open ("server", o_rdonly); {     write (fd_w, &count, sizeof(int)); //write on client's reading fifo     read (fd_r, &count, sizeof(int));  //read clients wrote     printf ("msg read: %d\n", count); } while (count != 4);  printf ("starting...\n"); 

client:

fd_r = open ("client", o_rdonly); fd_w = open ("server", o_wronly);  read (fd_r, &count, sizeof(int)); // read ticket server  count++;  write (fd_w, &count, sizeof(int)); // write should next ticket  //wait until have 4 processes 

what happens server waits first call when first process arrives reads right server message, put right message server server somehow interact 1 or 2 in loop (it seems kinda random) , ends. without print "starting...". ideas why happening? also, ideas how should notify other process can go on?

if matters, i'm using same code server , client, check which making test if "server" fifo created. guess doesn't matter.

there multiple problems in program. first note fifo semantics different of regular files.

if open fifo in read (o_rdonly) or write (o_wronly) modes, open calls block until fifo opened in write only/read write (o_wronly/o_rdwr)or read (o_rdonly) modes respectively. if want open calls return irrespective of above condition have open fifo in non-blocking , read (o_rdonly | o_nonblock) mode or in read write mode (o_rdwr).

if open (o_rdonly | o_nonblock) flags receive eagain error every read() unless fifo not opened writing. if open both read , write (o_rdwr) read block until data available in fifo. makes sense because ensures there @ least 1 writer , 1 reader on fifo.

also haven't done error checking in code, obfuscates coding errors , makes debugging harder.

i have fixed server code, works expected.

#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <fcntl.h>  int main(int argc, char *argv[]) {         int count = 0;     int fd_w = open ("client-fifo", o_rdwr);     if (fd_w < 0) {             perror("open");             exit(-1);         }     int fd_r = open ("server-fifo", o_rdwr);     if (fd_r < 0) {             perror("open");             exit(-1);         }        {         if (write (fd_w, &count, sizeof(int)) < 0) { //write                 perror("write");                 exit(-1);         }         if (read (fd_r, &count, sizeof(int)) < 0) {  //read                 perror("read");                 exit(-1);         }      } while (count != 4);      printf ("starting...\n");     return 0; } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -