Linux-C: reading from pipe returns first buffer written to it -



Linux-C: reading from pipe returns first buffer written to it -

this programme simulates variant of dijkstra's producer/consumer problem. pipeline first created followed kid process using fork(). kid write pipe crudely done randomly generated piece of "stock market ticker information". after waiting child/producer process write information, parent/consumer process read out.

the first output correct:

produced: fpoo 57.83 +0.43 consumed: fpoo 57.83 +0.43

any output next "consumed: info first read":

produced: rjii 71.30 -2.71 consumed: fpoo 57.83 +0.43

i unsure why occurring because tickerinfo changing. why suspect i'm reading pipe incorrectly or perhaps improperly structured forked processes.

i have compiled code in g++. takes argument number of seconds want programme run for.

#include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/types.h> #include <unistd.h> #include <time.h> #include <string.h> // generate info each stock entry void generatetickerinfo(char * info) { int i; int randnum = 0; srand(time(null)); // generate seed random // generate 4 characters stock sym for(i = 0; < 4; i++) { randnum = rand() % 26 + 65; info[i] = (char)(randnum); } info[4] = ' '; // generate cost traded for(i = 5; < 7; i++) { randnum = rand() % 8 + 1; info[i] = '0' + randnum; } info[7] = '.'; for(i = 8; < 10; i++) { randnum = rand() % 9; info[i] = '0' + randnum; } info[10] = ' '; // determine if + or - alter amount randnum = rand(); if(randnum % 2 == 1) { info[11] = '+'; } else { info[11] = '-'; } // generate alter amount randnum = rand() % 9; info[12] = '0' + randnum; info[13] = '.'; for(i = 14; < 16; i++) { randnum = rand() % 9; info[i] = '0' + randnum; } } // ** constant , global variables ** const int buffer_size = 25; // ** main code ** int main(int argc, char *argv[]) { pid_t cpid; // kid process id int mypipe[2]; // [0] read, [1] write char * tickerinfo; // hold current tickerinfo tickerinfo = new char[buffer_size]; // info passed through pipe char buf[buffer_size]; time_t currenttime, stoptime; // initialize time variables if(argc < 2) { printf("invalid arg. type 'foo.out (# seconds)'"); exit(0); } else { currenttime = time(null); stoptime = time(null) + (time_t)atoi(argv[1]); } int pipereturn = pipe(mypipe); if(pipereturn == -1) { // handle pipe creation error perror("pipe error..."); exit(0); } // main loop; go on until desired time has elapsed while(currenttime < stoptime) { cpid = fork(); if(cpid < 0) { // handle process creation error perror("forking error...\n"); exit(0); } else if(cpid == 0) { // kid process close(mypipe[0]); // kid not need read generatetickerinfo(tickerinfo); write(mypipe[1], tickerinfo, buffer_size); printf("produced: %s\n", tickerinfo); exit(0); } else if(cpid > 0) { // parent process wait(0); close(mypipe[1]); // parent not need write read(mypipe[0], buf, buffer_size); printf("consumed: %s\n", buf); } sleep(1); currenttime = time(null); } homecoming 0; }

after first kid done , close write end of pipe in parent process, go top of loop , create child. kid doesn't inherit write fd. write fd gone. write in sec kid fails, didn't check homecoming value error don't notice.

the parent reads eof pipe since there no remaining writers. since didn't check read homecoming value either (bad habit you've got!) don't notice that, , print buffer still contains previous contents.

c linux pipe fork inter-process-communicat

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -