Do You Need to Clear Pipes Before Writing Again C
-
09-25-2006 #i
Registered User
Clearing a piping used in IPC
Hi. I am using the fork() and pipe() functions to constitute communication betwixt 2 processes. If 1 procedure writes to the pipage, and the second process reads the pipe, how does the 2d process clear the input that information technology read on the last read functioning? (Or alternatively, how can I make process 1 articulate the piping earlier it writes to it?)
For instance, process 1 writes data as follows:
string1
string2
string3Process 2 is waiting for the data that process 1 is writing. In a loop, every bit procedure 2 reads the pipe, this is what gets read as a string on each iteration:
1. string1
2. string1string2
three. string1string2string3How do I clear the pipe in process 2 after each read so that I only become the latest cord written by process 1? Just to clarify, I want procedure 2 to read the the data from process i in this manner:
1. string1
2. string2
3. string3Thank you!
-
09-25-2006 #2
and the lid of int overfl
How about posting your code to read the pipe?
pipes are like files, you get each grapheme in turn exactly one time, so I effigy in that location's something else going on equally well.
-
09-25-2006 #3
Registered User
Certain. Here it is...
Thanks for taking a look at it. I am simply posting the relevant sections.
Code:
... /****************************/ /* Global variables and constants: */ /****************************/ #ascertain RSIZE lxxx #define BUFSIZE x #define NEWBUFSIZE 200 #define FD_READ 0 #ascertain FD_WRITE 1 #ascertain STDIN 0 #ascertain STDOUT ane #ascertain NO_OF_OPERANDS v #define NO_OF_OPERATORS iv ... establishProcessPipe(pipeFDs); fdin = fopen("./data.in","r"); fdout = fopen("./data.out","w"); /* Create child process to load the piping w/input. Process the outcome */ /* as the parent: */ pid = fork(); switch(pid) { case -one: /* fork() Error */ printf("ERROR: Could not spawn child!"); go out(i); case 0: /* I am the kid */ close(STDOUT); dup2(pipeFDs[FD_WRITE]); shut(FD_WRITE); processInputFile(pipeFDs,fdin,pid); fclose(fdin); exit(0); default: /* I am the parent */ close(STDIN); dup2(pipeFDs[FD_READ]); close(FD_READ); processLoadedPipe(pipeFDs,fdout,pid); fclose(fdout); exit(0); } ... void establishProcessPipe(int pipeFDs[]) { if(pipe(pipeFDs) == -1) { printf("ERROR: Could non create pipe!"); exit(1); } } void processLoadedPipe(int pipeFDs[],FILE *fdout,int pid) { /******************************/ /* Create output file from result pipe: */ /******************************/ int nread; char buf[NEWBUFSIZE]; char newbuf[NEWBUFSIZE] = {""}; nread = 0; close(pipeFDs[FD_WRITE]); /* Close write descriptor */ while((nread = read(pipeFDs[FD_READ],buf,NEWBUFSIZE)) != 0) { if(strcmp(buf, "EOF") == 0) /* Observe cease of file message */ break; performCalculationsUsingReadBuffer(buf,newbuf); fprintf(fdout,"%south\due north",newbuf); } close(pipeFDs[FD_READ]); /* Close read descriptor */ } void processInputFile(int pipeFDs[],FILE *fdin,int pid) { char buf[BUFSIZE]; char newbuf[NEWBUFSIZE] = {""}; int newbufsize = NEWBUFSIZE; int i; while(fscanf(fdin,"%s\n",buf) != EOF) { performTranslationOfReadBuffer(buf,newbuf,newbufsize,pid); writeToPipe(pipeFDs,newbuf,newbufsize,pid); } writeToPipe(pipeFDs,"EOF",newbufsize,pid); /* EOF message */ } void writeToPipe(int pipeFDs[],char buf[],int bufsize,int pid) { close(pipeFDs[FD_READ]); /* Close read descriptor */ write(pipeFDs[FD_WRITE],buf,bufsize); }
-
09-25-2006 #4
and the lid of int overfl
> close(STDOUT);
> dup2(pipeFDs[FD_WRITE]);
> close(FD_WRITE);
This makes no sense - why would you close(1) a second fourth dimension?> processInputFile(pipeFDs,fdin,pid);
> processLoadedPipe(pipeFDs,fdout,pid);
Neither of these should be passed the pipes. You lot've already dup'ed them to stdin and stdout.
I retrieve yous should probably close both ends of the pipe once yous've dup'ed them.> writeToPipe
> close(pipeFDs[FD_READ]);
OK already, it'south closed - stop endmost it again and again!> writeToPipe(pipeFDs,newbuf,newbufsize,pid);
Maybe something like strlen(newbuf) and then you don't write a whole bunch of garbage downwardly the pipe each time?
-
09-26-2006 #5
Registered User
Thank you for replying
Thanks for your response.
If I practise not pass the pipe array to the functions, should I use scanf to read stdin (which has at present been overridden past the pipage)? When I tried to practice this the screen hangs, so it seems like stdin is not beingness overridden to the pipe?
Also - Is the pipe supposed to be cleared? I wind upwards reading the entire contents of the pipage on each read. I merely want to read one row at a fourth dimension.
Thanks once again.
-
09-26-2006 #6
and the chapeau of int overfl
This seems to work on my Linux box
Code:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/look.h> #define PIPE_READ_END 0 #define PIPE_WRITE_END one void producer ( void ) { int i; for ( i = 0 ; i < ten ; i++ ) { printf( "Loop %d\north", i ); } fflush(stdout); } void consumer ( void ) { char vitrify[BUFSIZ]; while ( fgets( vitrify, sizeof buff, stdin ) != Goose egg ) { printf( "read: %south", buff ); } } int principal ( void ) { pid_t pid; int p[2]; int status; status = pipe(p); if ( status == -1 ) { perror("Can't pipe"); exit( EXIT_FAILURE ); } pid = fork(); if ( pid == 0 ) { close( p[PIPE_READ_END] ); dup2( p[PIPE_WRITE_END], STDOUT_FILENO ); producer(); _exit(ii); } else if ( pid > 0 ) { close( p[PIPE_WRITE_END] ); dup2( p[PIPE_READ_END], STDIN_FILENO ); consumer(); { int s; wait(&s); printf( "Kid exited with status=%d\northward", WEXITSTATUS(south) ); } } else { perror("Can't fork"); } render 0; }
sheldonstaend1940.blogspot.com
Source: https://cboard.cprogramming.com/c-programming/83405-clearing-pipe-used-ipc.html
0 Response to "Do You Need to Clear Pipes Before Writing Again C"
Post a Comment