named fifo C issue -
i have c code:
#define bufsize 256 int main ( int argc, char *argv[]) { int fdin; int fdout; if( argc != 3) { perror("argument error"); exit(1); } if( (fdin = open(argv[1], o_rdonly ) )<0) { perror("pipe input error open"); exit(1); } if( (fdout = open(argv[2], o_wronly ) )<0) { perror("pipe input error open"); exit(1); } int c = 2; while(c--) { char var1[bufsize]; char var2[bufsize]; char string[100]; memset(var1, 0, sizeof(var1)); memset(var2, 0, sizeof(var2)); memset(string, 0, sizeof(string)); if( readline(fdin, var1, sizeof(var1)) == 0) { printf("exit1\n"); exit(0); } if( readline(fdin, var2, sizeof(var2)) == 0) { printf("exit2\n"); exit(0); } removenewline(var1); removenewline(var2); printf("%s\n",var1); printf("%s\n",var2); int n = atoi(var1); int m = atoi(var2); if (n!=0 && m % n == 0 ) { sprintf(string,"multiple\n"); } else{ sprintf(string,"negative\n"); } printf("%s", string); writeline(fdout, string, strlen(string)); } close(fdout); close(fdin); exit(0); } this program accepts 2 inputs: first input fifo's name , second output fifo's name. program this: reads in input fifo 2 numbers , establishes if second number multiple of first number , writes on output fifo "multiple" or "negative". 2 times; problem when executed second loop first readline returns 0 , prints exit1. program should not stop on readline pending content in fifo?
functions used in program:
int readline( int fd, char* str, int buffersize) { return readtodel(fd, '\n', str, buffersize); } int readtodel( int fd, char delimiter, char* str, int buffersize) { int n; int byteletti =0; int index=0; /* read characters until null or end-of-input */ { if( (n = read (fd, str+index, 1)) < 0) { perror("errore: lettura dal file descriptor fallita"); exit(1); } byteletti+=n; } while (n > 0 && *(str+index++) != delimiter && index < buffersize); return byteletti; /* return false if end-of-input */ } for testing program this: open first terminal , write in input fifo in way:
echo "4" > input echo "8" > input then open other terminal , execute program in way:
./program input output and when program blocked on writeline(fdout, string, strlen(string)); ( because there isn't reader on output fifo) open other terminal , : cat output
and get(based on input):
multiple exit1 and dont understand why. should not wait until more input on input fifo?
Comments
Post a Comment