Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Trouble with pipe function
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;
}

Report this thread to moderator Post Follow-up to this message
Old Post
Craig Bumpstead
10-31-04 08:56 AM


Re: Trouble with pipe function
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Pascal Bourguignon
10-31-04 08:56 AM


Re: Trouble with pipe function
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 ***

Report this thread to moderator Post Follow-up to this message
Old Post
Barry Margolin
10-31-04 08:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:36 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.