Im trying to make a Tic Tac Toe game with server-client using FIFO(named pipe) and shared memory.
The first step is to write the pid of the client process to the FIFO. And in the server process i need to wait until 2 pids was passed in the FIFO (from 2 different client process).
Right now im just doing 2 read from the pipe but it's not works proper. Do i need to read in a while loop or something?
The client code:
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
void main()
{
int fd;
int pid=getpid();
pid_t pid1=getpid();
fd=open("fifo_clientTOserver",O_WRONLY);
write(fd,&pid1,sizeof(pid_t));
printf("%d\n",pid1);
while(1);
}
The server code:
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
void main()
{
int fd;
pid_t pid;
ftok("shmdemo.c","j");
mkfifo("fifo_clientTOserver",400);
fd=open("fifo_clientTOserver",O_RDONLY);
read(fd,&pid,sizeof(pid_t));
printf("%d",pid);
read(fd,&pid,sizeof(pid_t));
printf("%d",pid);
//sleep(2);
}
I want that the server will wait until 2 clients are running, how should i do that?
Thank you very much, Asaf.