I want to create a server process and a client process -for an optional class assignment- and make them communicate with each other. The professor told us that:
1)we must use O_NONBLOCK
2)we must create 2 FIFOs, one for reading only and one for writing only
3)we can't use sockets
So, I created 2 FIFOs in server process and I tried to open them but the open for WRONLY returned "No such device or address".
server process:
......
if( mkfifo("fifo1", PERMS) < 0 && errno != EEXIST)
{
perror("can't create FIFO (read)");
exit(EXIT_FAILURE);
}
if( mkfifo("fifo2", PERMS) < 0 && errno != EEXIST )
{
perror("can't create FIFO (write)");
exit(EXIT_FAILURE);
}
if( (readfd = open("fifo1", O_RDONLY | O_NONBLOCK)) < 0)
{
perror("console: can't open read FIFO");
exit(EXIT_FAILURE);
}
if( (writefd = open("fifo2", O_WRONLY | O_NONBLOCK)) < 0)
{
perror("coord: can't open write FIFO");
exit(EXIT_FAILURE);
}
client process:
.....
if( (readfd = open("fifo2", O_RDONLY | O_NONBLOCK)) < 0)
{
perror("console: can't open read FIFO");
exit(EXIT_FAILURE);
}
if( (writefd = open("fifo1", O_WRONLY | O_NONBLOCK)) < 0)
{
perror("console: can't open write FIFO");
exit(EXIT_FAILURE);
}
while( fgets(buffer, 100, stdin) ) //char buffer[100];
{
n = strlen(buffer);
w = write(writefd, buffer, n);
memset(buffer, 0, 100);
}
I looked for a solution online and I found out this answer, which explains fine what goes wrong, but doesn't propose a way to fix the issue. I looked online again and found select() and it looked like it can provide a solution, but I had some trouble understanding how exactly it works.
Is it possible to use select() in order to fix this issue?