pipe() and poll() problem in C


 
Thread Tools Search this Thread
Top Forums Programming pipe() and poll() problem in C
# 1  
Old 12-08-2010
pipe() and poll() problem in C

Hi all,
Im trying to do a simple program which ask the user for a unix command with the arguments. The program fork and the two process communicate with pipes. The child process call execvp with the command and the father process read the result of the execvp via the pipe.

This program works well with basic commands like ls or echo but when there's interaction between the user and the process for example tr a-z A-Z I think i get a broken pipe on the writing end of my pipe from the parent to the child.

In the parent process, I also have to poll between what the user type and what the child process send but I don't think this is the problem.

here's my code, the name variable and error messages are in french but you get the idea. thank u very much and sorry for bad english.

If you have any questions feel free to ask

Code:
void executerCommande( char *commande[], int descripteur, char* option )
{
	int tubeEntree[2];
	int tubeSortie[2];
	int erreur;
	
	erreur = pipe( tubeEntree );
	if ( erreur == -1 )
	{
		printf( "Erreur lors de la création du tube %s\n", strerror( errno ) );
		exit( 1 );
	}
	erreur = pipe( tubeSortie );
	if ( erreur == -1 )
	{
		printf( "Erreur lors de la création du tube %s\n", strerror( errno ) );
		exit( 1 );
	}

	pid_t pid = fork();
	if ( pid == -1 )	// erreur
	{
		perror( "fork a échoué" );
		exit( 1 );
	}
	else if ( pid == 0 )	// child
	{
		close( tubeEntree[1] );		// closing writing end of tube from parent to child
		close( tubeSortie[0] );		// closing reading end of tube from child to parent

		dup2( tubeSortie[1], STDOUT_FILENO );	// redirecting stdout to writing end of tube from child to parent
		close( tubeSortie[1] );		// fermeture du doublon

		dup2( tubeEntree[0], STDIN_FILENO );	// redirecting stdin to reading end of tube from parent to child
		close( tubeEntree[0] );		// fermeture du doublon
		execvp( commande[0], commande );
	}
	else		// parent
	{
		int evenement;
		int commandeTerminee = 0;
		char tampon [256];
		struct pollfd entrees[2];
		entrees[0].fd = 0;		// check stdin
		entrees[0].events = POLLIN | POLLPRI;		// surveiller pour des données normales ou prioritaires
		entrees[0].revents = 0;
		entrees[1].fd = tubeSortie[0];		// check reading end of tube from child to parent
		entrees[1].events = POLLIN | POLLPRI;		// surveiller pour des données normales ou prioritaires
		entrees[1].revents = 0;

		close( tubeEntree[0] );		// closing reading end of tube from parent to child
		close( tubeSortie[1] );		// closing writing end of tube from child to parent
		
		while ( commandeTerminee == 0 )
		{
			evenement = poll( entrees, 2, 0 );
			if ( evenement == -1 )
			{
				printf( "Erreur lors du poll %s\n", strerror( errno ) );
				exit( 1 );
			}
			if ( ( entrees[0].revents & POLLIN ) || ( entrees[0].revents & POLLPRI ) )
			{
				memset(tampon, '\0', 256);
				read( entrees[0].fd, tampon, 256 );
				if ( ( strcmp( option, "-i" ) == 0 ) || ( strcmp( option, "-io" ) == 0 ) )
				{
					erreur = write ( descripteur, tampon, strlen( tampon ) );
					if ( erreur == -1 )
					{
						printf( "Erreur lors de l'écriture sur le fichier %s\n", strerror( errno ) );
						exit( 1 );
					}
				}
				erreur = write ( tubeEntree[1], tampon, strlen( tampon ) );
				printf( "Ecriture enfant\n" );
				if ( erreur == -1 )
				{
					printf( "Erreur lors de l'écriture dans le tube d'entrée %s\n", strerror( errno ) );
					exit( 1 );
				}
			entrees[0].revents = 0;
			}
			if ( ( entrees[1].revents & POLLIN ) || ( entrees[1].revents & POLLPRI ) )
			{
				do {
					memset(tampon, '\0', 256);
					erreur = read( entrees[1].fd, tampon, 256 );
					if ( erreur == -1 )	
					{
						printf( "Erreur lors de la lecture du tube de sortie %s\n", strerror ( errno ) );
						exit( 1 );
					}
					if ( ( strcmp( option, "-o" ) == 0 ) || ( strcmp( option, "-io" ) == 0 ) )
					{
						erreur = write( descripteur, tampon, strlen( tampon ) );
						if ( erreur == -1 )	
						{
							printf( "Erreur lors de l'écriture sur le fichier %s\n", strerror ( errno ) );
							exit( 1 );
						}
					}
					printf( "%s", tampon );
				} while (erreur != 0);
			entrees[1].revents = 0;
			}
			if ( entrees[1].revents & POLLHUP )
			{
				commandeTerminee = 1;
			}
		}	
		pid = wait( NULL );
	}
}

---------- Post updated at 12:49 PM ---------- Previous update was at 11:26 AM ----------

I have force a read in the pipe connecting the child process ( the command ) to my parent process and there's nothing so my problem is either 1) the information coming from the user is not arriving at the child or the child is not writing properly in the exit pipe.

Any help would be appreciated... Its my first time with pipes
# 2  
Old 12-08-2010
Quote:
here's my code, the name variable and error messages are in french but you get the idea.
Not really, I don't. I have no idea what "descripteur" is and why you're writing to it, for example.

It would also help to know what you're trying to do with this command, and what it's actually doing. And maybe minor details like what your operating system is. Because if you're running linux, you could try strace -f ./command parameters to see a trace of system calls as it runs, which will tell you exactly who's waiting for what when.

---------- Post updated at 09:31 PM ---------- Previous update was at 09:03 PM ----------

This isn't right:
Code:
erreur = write ( tubeEntree[1], tampon, strlen( tampon ) );

Not all text is strings. read() doesn't add a null to the end.

Code:
length=read( entrees[0].fd, tampon, 256 );
if(length == 0)
{
        printf("End of file\n");
}
...
erreur = write ( tubeEntree[1], tampon, length );

# 3  
Old 12-08-2010
descripteur is descriptor in french its because the parent process is suppose to collect the data from the child and write it in a file specified by descriptor.
I spent all day on this and decide to check the return value fo my dup2 and got an error when redirecting the output of my child process from stdout to the pipe tubeSortie[1].

I try replacing fileno(stdout) with 1 but i keep getting the same error:
Erreur lors du dup stdout Unknown error: 0

Im using mac os x with darwin

check this
Code:
void executerCommande( char *commande[], int descripteur, char* option )
{
	int tubeEntree[2];
	int tubeSortie[2];
	int erreur;
	
	erreur = pipe( tubeEntree );
	if ( erreur == -1 )
	{
		printf( "Erreur lors de la création du tube %s\n", strerror( errno ) );
		exit( 1 );
	}
	erreur = pipe( tubeSortie );
	if ( erreur == -1 )
	{
		printf( "Erreur lors de la création du tube %s\n", strerror( errno ) );
		exit( 1 );
	}

	pid_t pid = fork();
	if ( pid == -1 )	// erreur
	{
		perror( "fork a échoué" );
		exit( 1 );
	}
	else if ( pid == 0 )	// child
	{
		erreur = close( tubeEntree[1] );		// closing writing end of tube from parent to child
		if ( erreur == -1 )
		{
			printf( "Erreur lors du close %s\n", strerror( errno ) );
			exit( 1 );
		}
		erreur = close( tubeSortie[0] );		// closing reading end of tube from child to parent
		if ( erreur == -1 )
		{
			printf( "Erreur lors du close %s\n", strerror( errno ) );
			exit( 1 );
		}
		if ( dup2 ( tubeSortie[1], fileno(stdout) ) == -1 );	// redirecting stdout to writing end of tube from child to parent
		{
			printf( "Erreur lors du dup stdout %s\n", strerror( errno ) );
			exit( 1 );
		}

# 4  
Old 12-08-2010
Once that's fixed: Your code does actually write to the pipe.

Code:
# the code does tr 'a-z' 'A-Z'
while true ; do echo -n a ; done | ./a.out
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

But the read end doesn't see data until the write end is full (or closes). That's probably at least 65536 bytes, if not much more, so you don't see enough by just typing. On EOF you should close the writing pipe and break the loop, which will cause the pipe to send everything it had.

---------- Post updated at 09:52 PM ---------- Previous update was at 09:40 PM ----------

Code:
fileno(stdout)

You don't need to do this. STDOUT_FILENO was right in the first place.
# 5  
Old 12-08-2010
thanks alot, I can't try your solution cuz im stuck with the dup2 error. Its weird cuz it was working when i didnt check the return value
# 6  
Old 12-08-2010
You need to check for POLLHUP on entrees[0], to see when stdin closes, so it can close its writing pipe.
# 7  
Old 12-08-2010
I know but I tried with STD_FILENO, 1, fileno(stdout) and its not working either, maybe i should just not check the return value but its not clean
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Problem with Pipes => Only works first pipe

Hi! I'm having problems with pipes... I need comunnications with childs processes and parents, but only one child can comunnicate with parent (first child), others childs can't. A brief of code: if(pipe(client1r)<0){ perror("pipe"); } ... (1 Reply)
Discussion started by: serpens11
1 Replies

2. Shell Programming and Scripting

The problem of pipe

Hi,guys: I want to use c to implement a pipe. For example: ps auxwww | grep fred | more I forked three child processes. Each is responsible for each command, and pipe to next one. for(i=0;i<2;i++) pipe(fd) if(child==1) // child 1 { close(1) dup2(fd,1) close(fd) }... (3 Replies)
Discussion started by: tomlee
3 Replies

3. UNIX for Dummies Questions & Answers

problem with pipe operator

hi i am having issues with extra pipe. i have a data file and i need to remove the extra pipe in the(example 4th and 7thline) in datafile. there are many other line and filed like this which i need to remove from files. The sample data is below: 270 31|455004|24/03/2010|0001235|72 271... (3 Replies)
Discussion started by: abhi_n123
3 Replies

4. Shell Programming and Scripting

problem using a pipe to grep

Hello ! I want to process a text file in order to extract desired data using sed and grep... Now I am facing a problem piping to grep... nothing happens.. The text consists of blocks of 3 lines that may (or not) contain the Desired data. the desired data is on each 2... (4 Replies)
Discussion started by: ShellBeginner
4 Replies

5. Programming

Pipe problem

Could anyone tell me whats wrong whit this piping? the commands that they execute are correct. the command I am trying is ls|wc. Both processes go to the right if statement. for(i=0;i<argc;i++){ if(i==0&&argc>1){//first command if(pipe(pipa1)==-1) ... (2 Replies)
Discussion started by: isato
2 Replies

6. Programming

Problem in read() from a pipe

Hi, Can any one please help me with this. Am struggling hard to get a solution. I am doing telnet through a C program and getting the stdout file descriptor of the remote machine to pipe. read() function is getting data, But whenl it receives SOH character ie. ^A ( Start of heading = Console... (2 Replies)
Discussion started by: JDS
2 Replies

7. Programming

Pipe Problem

Is there a way to know whether is pipe is opened in read or write mode.I mean is there any signal that is generated when a pipe is opened in read or write mode. If you have some solution .please let me know ........ (2 Replies)
Discussion started by: vivivo2000
2 Replies

8. Shell Programming and Scripting

Problem with pipe into sed

Basically I am trying to write a short script to report total space used on /u0? file systems. This is what I was trying to do:df -k /u0? | grep -v kbytes | awk '{ printf $2 "+" }' | sed s/.$// | bcBut it returns no output. This works however: > A=`df -k /u0? |grep -v kbytes | awk '{ printf $2... (2 Replies)
Discussion started by: 98_1LE
2 Replies

9. Shell Programming and Scripting

read after pipe problem OSX10.4

I use read often in scripts to filter the right part into a variable like: $ print "abc cde efg" | read k l ; print "k=$k, l=$l" k=, l= This works on linux and unix versions I work with. On OSX 10.4 this doesn't work. I found a workaround but would like to know why the original line... (5 Replies)
Discussion started by: relyveld
5 Replies

10. Programming

Wierd pipe problem

I have encountered a strange problem dealing with pipes and forking. The program basicaly does this: cat file | tbl | eqn | groff Now, I have a parent process that forks children that that exec the stuff that they should. The pipes defined in the parent are the ones used. The chain goes... (1 Reply)
Discussion started by: denoir
1 Replies
Login or Register to Ask a Question