I've been trying to wrap my head around FIFO, and came up with a simple program of server and client.
I'm not trying to do anything fancy, just to have one process that will play a role of 'server', this process will 'listen' to any messages delivered by another process; the client.
Here's what I wrote:
server.c
#include<stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#define INGOING "clientToServer.fifo"
#define BUFFER 200
int main(int argc, char *argv[]) {
char in[BUFFER];
mkfifo(INGOING, 0666);
printf("Welcome to server.\n");
printf("channel for sending messages to server is %s\n", INGOING);
int in_fd=open(INGOING, O_RDONLY);
if (in_fd==-1) {
perror("open error");
exit(-1);
}
while (read(in_fd, in, BUFFER)>0) {
printf("You sent %s to server.\n", in);
}
return 2;
}
As you can see, this is pretty straight forward, when I ran this at background with ./server.out&
it's blocked at the read
call and waiting for anyone to write to clientToServer.fifo
. so far so good.
Now, consider the client end:
client.c
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#define BUFFER 200
int main(int argc, char *argv[]) {
char input[BUFFER]={0};
int out_fd=open("clientToServer.fifo", O_WRONLY);
if (out_fd==-1) {
perror("open error");
}
while (1) {
printf("What would you like to send to server? (send Quit to quit)\n");
fgets(input, BUFFER, stdin);
if (input[strlen(input)-1]=='\n') {
input[strlen(input)-1]='\0';
}
if (strcmp(input, "Quit")==0) {
printf("Bye!");
break;
}
if (write(out_fd, input, strlen(input))==-1) {
perror("write error");
}
}
return 1;
}
This is the client. also pretty simple code. when I run it with ./a.out
from shell, it works - it sends the message, and the server.out
process prints You sent %s to server.
Problem is, when I send Quit
through the client to the server, although the a.out
process terminates as desired, the while
loop in the server.out
breaks as well. meaning, the read
no longer blocks the server.out
process and awaits other clients, instead, the server program ends, along with the client.
Why is this happening? shouldn't the read
suspend the server.out
again, even after the a.out
process ends?