Sponsored Content
Top Forums Programming pipe() and poll() problem in C Post 302478755 by blackmamba21 on Wednesday 8th of December 2010 10:34:34 PM
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 );
		}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
FORK(2) 							System Calls Manual							   FORK(2)

NAME
fork - create a new process SYNOPSIS
#include <sys/types.h> #include <unistd.h> pid_t fork(void) DESCRIPTION
Fork causes creation of a new process. The new process (child process) is an exact copy of the calling process except for the following: The child process has a unique process ID. The child process has a different parent process ID (i.e., the process ID of the parent process). The child process has its own copy of the parent's descriptors. These descriptors reference the same underlying objects, so that, for instance, file pointers in file objects are shared between the child and the parent, so that an lseek(2) on a descriptor in the child process can affect a subsequent read or write by the parent. This descriptor copying is also used by the shell to establish standard input and output for newly created processes as well as to set up pipes. The child starts with no pending signals and an inactive alarm timer. RETURN VALUE
Upon successful completion, fork returns a value of 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, a value of -1 is returned to the parent process, no child process is created, and the global variable errno is set to indicate the error. ERRORS
Fork will fail and no child process will be created if one or more of the following are true: [EAGAIN] The system-imposed limit on the total number of processes under execution would be exceeded. This limit is configuration- dependent. (The kernel variable NR_PROCS in <minix/config.h> (Minix), or <minix/const.h> (Minix-vmd).) [ENOMEM] There is insufficient (virtual) memory for the new process. SEE ALSO
execve(2), wait(2). 3rd Berkeley Distribution May 22, 1986 FORK(2)
All times are GMT -4. The time now is 09:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy