c - Weird behaviour of fifos on linux -


i'm studying linux fifos , made 2 small c programs communicate through fifo. first 1 acts server, receive pattern , executes command using pattern. second 1 acts client, sends pattern , receive result. want server capable of serving multiple requests, not simultaneously, weird thing after first client served stops although put there infinite loop.

server.c

#include <sys/types.h> #include <sys/stat.h> #include <string.h> #include <signal.h> #include <fcntl.h>   void siginthandler(int i){     remove("./fifo1");     remove("./fifo2");     printf("got sigint signal\n");     exit(exit_success); }   int main(int argc, char *argv[]){     signal(sigint, siginthandler);     int f = mkfifo("./fifo1", 0600);     if (f == -1){         perror("unable create fifo1\n");         exit(exit_failure);     }     f = mkfifo("./fifo2", 0600);     if (f == -1){         perror("unable create fifo2\n");         exit(exit_failure);     }     int fd1 = open("./fifo1", o_rdonly);     int fd2 = open("./fifo2", o_wronly);     if (fd1 == -1 || fd2 == -1){         perror("unable open fifos\n");         exit(exit_failure);     }     while (1){         char buf[50];         char pattern[50];         read(fd1, pattern, 50);         char command[80] = "ps -e | grep ";         strcat(command, pattern);         file *result = popen(command, "r");         while (fgets(buf, 50, result)){             write(fd2, buf, 50);             //printf("%s", buf);         }         memset((void *) buf, 0, 50);         write(fd2, buf, 50);         pclose(result);     }     remove("./fifo1");     remove("./fifo2");     return 0; } 

client.c

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h>  int main(int argc, char *argv[]){     int fd1 = open("./fifo1", o_wronly);     int fd2 = open("./fifo2", o_rdonly);     if ((fd1 == -1) || (fd2 == -1)){         perror("unable find fifos");         exit(exit_failure);     }     char input[50];     printf("give pattern: ");     scanf("%s", input);     write(fd1, input, 50);     char buf[50];     while (read(fd2, buf, 50) == 50){         if (buf[0] == 0){             break;         }         printf("%s", buf);     }     return 0; } 

when first client closes fifo, server gets eof on fifo, , continues no new data in perpetuity. server has reopen fifo next client. if there multiple clients fifo open concurrently, server not eof until last of clients disconnected (as long there 1 writer, reader — server — ok).

this expected behaviour — or, since weren't expecting it, behaviour should expected.

of course, since code ignores return value read(), have no idea what, if anything, being read.

the code:

memset((void *) buf, 0, 50); write(fd2, buf, 50); 

is curious; why send buffer of 50 0 bytes client? close fifo without sending that.

also note writing on fifo there isn't reader generate sigpipe signal — , aren't handling those. default action sigpipe exit.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -