Trouble with pipe function
From: Craig Bumpstead (cbumpste_at_yahoo.com.au)
Date: 10/31/04
- Next message: Tabrez Iqbal: "Re: help"
- Previous message: Barry Schwarz: "Re: C program question"
- Next in thread: B. v Ingen Schenau: "Re: Trouble with pipe function"
- Reply: B. v Ingen Schenau: "Re: Trouble with pipe function"
- Reply: Alwyn: "Re: Trouble with pipe function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 31 Oct 2004 12:32:26 +1100
Hi,
I am having trouble with the pipe function.
Currently I can list the contents of a directory via readdir(), but
trying to get the pipe to work is difficult.
It just doesn't return anything?
I am not sure what I am doing wrong.
What is also difficult is debugging a multiprocess program. Any hints
would be great.
The target OS is unix, so that cuts out MS debuggers.
Cheers,
Craig
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <wait.h>
#include <fcntl.h>
#include <signal.h>
int main()
{
int cmdPipe = 1;
int pipePos = 1;
char *cmdVarTwo = "sort";
char *cmdVarThree;
struct dirent *dirLIST;
DIR *userDIR;
char *currentDIR = ".";
int pipeDes[2];
int status = 0;
char *cmdSwitch[2];
pid_t pid1, pid2;
if( cmdPipe == 1 )
{
if( pipePos == 1 )
{
if( pipe( pipeDes ) == -1 )
{
perror("Error creating pipe.\n");
return 0;
}
switch( pid1 = fork() )
{
case -1:
perror("Error creatign the fork");
case 0:
break;
default:
wait( &status);
return 0;
}
pid2 = fork();
switch( pid2 )
{
case -1:
perror("Unable to fork the process.\n");
exit(1);
case 0:
dup2( pipeDes[1], 1 );
close( pipeDes[0] );
close( pipeDes[1] );
if((userDIR = opendir( currentDIR )) == NULL )
return (-1);
while( (dirLIST = readdir(userDIR)))
{
if( dirLIST->d_ino != 0)
{
printf("%s\n", dirLIST->d_name);
}
}
exit(1);
default:
dup2( pipeDes[0], 0 );
close( pipeDes[0] );
close( pipeDes[1] );
cmdSwitch[0] = cmdVarThree;
execvp( cmdVarTwo, cmdSwitch );
wait(( int *)0 );
/*perror("Parent exiting:");*/
exit(1);
/*return 0;*/
}
}
}
return 0;
}
- Next message: Tabrez Iqbal: "Re: help"
- Previous message: Barry Schwarz: "Re: C program question"
- Next in thread: B. v Ingen Schenau: "Re: Trouble with pipe function"
- Reply: B. v Ingen Schenau: "Re: Trouble with pipe function"
- Reply: Alwyn: "Re: Trouble with pipe function"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|