|
Home > Archive > Unix Programming > October 2004 > Trouble with pipe function
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Trouble with pipe function
|
|
| Craig Bumpstead 2004-10-31, 2:46 am |
| 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;
}
| |
| Pascal Bourguignon 2004-10-31, 2:46 am |
| cbumpste@yahoo.com.au (Craig Bumpstead) writes:
> 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.
strace -f could help.
> char *cmdVarTwo = "sort";
> char *cmdVarThree;
> char *cmdSwitch[2];
> exit(1);
> exit(1);
Why do you exit with an error status?
> cmdSwitch[0] = cmdVarThree;
> execvp( cmdVarTwo, cmdSwitch );
You're giving sort an uninitialized argv.
May be that prevent it to work?
--
__Pascal Bourguignon__ http://www.informatimago.com/
Voting Democrat or Republican is like choosing a cabin in the Titanic.
| |
| Barry Margolin 2004-10-31, 2:46 am |
| In article <1ffd63e4.0410302235.3edc595e@posting.google.com>,
cbumpste@yahoo.com.au (Craig Bumpstead) wrote:
> 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.
You didn't close pipeDes[1] in the original parent process, so the
reading process never see EOF.
I have some other comments about the code interspersed.
>
> 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();
Why are you forking twice? You could just fork once, and have the
original parent process write to the pipe, while the child exec's a
command. Then you wouldn't have had the problem I described above.
>
> 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;
You've never initialized cmdVarThree, so this assignment is not very
useful. And what about cmdSwitch[1]? If you're not passing any
arguments, you should set that to NULL.
>
> execvp( cmdVarTwo, cmdSwitch );
> wait(( int *)0 );
If the exec succeeds, this wait() will never be executed, and the child
will be a zombie until the command finishes. So it would be better to
do the exec in the child so that the parent can call wait() after it
finishes the while loop (or it could just exit, and the child will be
inherited by init).
> /*perror("Parent exiting:");*/
> exit(1);
> /*return 0;*/
>
> }
> }
> }
>
> return 0;
> }
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
|
|
|
|
|