IPC: named FIFO not working properly.

Hello, guys.

I'm trying to make a simple program which communicates with other programs using named fifo. The first thing i want to do is make a simple server program which creates a random number and writes in a fifo file, and a client program read it. The server is working fine, but my client program is not working properly. He stops in read() function!

What is going on?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Server.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main( void ) {

	int number, pipe;

	if( mkfifo("arqfifo", 0666 ) < 0 ) {
		perror("Error: mkfifo( ): ");
		return 1;
	}

	printf("Trying to connect to client.\n");
	if( pipe = open("arqfifo", O_WRONLY ) < 1 ) {
		perror("Error: open( ): ");
		return 1;
	}

	//number = rand() % 100; 
	number = 256;
	printf("Connection succeeded.\n");
	printf("The number is: %d\n", number );

	if( write( pipe, &number, sizeof( int ) ) < 0 ) {
		perror("Error: write( ): ");
		return 1;
	}

	close( pipe );	
	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Client.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main( void ) {
	
	int number, pipe;

	printf("Trying to connect to server.\n");
	if( pipe = open("arqfifo", O_RDONLY, 0 ) == -1 ) {
		perror("Error: open( ): ");
		return 1;
	}

	if( read( pipe, &number, sizeof( int ) ) < 0 ) {
		perror("Erro: read( ): ");
		return 1;
	}
	printf("Connection succeeded.\n");
	printf("The number is: %d\n", number );
	close( pipe );	
	return 0;
}
closed account (Dy7SLyTq)
http://linux.die.net/man/3/read
it looks like your arguments are wrong
You are using two separate terminals?

Here some tests I just did

test1:

terminal 1:

mkfifo myfifo
echo "stuff" > myfifo

this waits because there is no reader from the fifo

terminal 2:

cat myfifo

"stuff" appears, the cat command has pulled it out of the fifo. the echo command in terminal 1 now completes

test 2:

terminal 1:

cat myfifo

cat now waits because there is nothing in the fifo to pull out

terminal 2:

echo "stuff" > myfifo

this puts "stuff" into the fifo - it is immediately pulled out by cat on terminal 1 which now ends waiting

So you need to be careful r.e. synchronisation, if the reader is waiting it may mean that there is nothing in the fifo at that time,



if( pipe = open("arqfifo", O_RDONLY, 0 ) == -1 )
You should get a warning doing this.

What is happening, is that the return value of open is being compared with -1, and the result of the comparison is true or false, which is then casted to an int, becoming, 1 or 0, for the assignment to pipe. 0, and 1, are standard input and output.

You need parentheses around the assigment.

if( (pipe = open("arqfifo", O_RDONLY, 0 )) == -1 )

And also in other file,

if( (pipe = open("arqfifo", O_WRONLY )) < 1 )

Last edited on
Thank you, guys. Now everything is working fine. Thank you htirwin, you are right.

Problem fixed.
Topic archived. No new replies allowed.