Need urgent help with this (fork, dup, pipes) 
Author Message
 Need urgent help with this (fork, dup, pipes)

Hi,

I'm trying :( to do a program for linux that envolves one main process
and 2 childs.
The program must do the following using 3 pipes.

--> Main Process: Read words typed by user and send them to CHILD 1.
--> CHILD1: Execute a "sort -r" command with the words sent by Main
Process
and send the result to CHILD2.
--> CHILD2: Execute a "uniq" command with the result given by CHILD1
for removing the duplicated words.

-->And finally: Main process has to get them and show them to screen.

Example: if I type:
house
car
dog
tree
tree

The output must be:
tree  ---> one tree removed by uniq command!
house
dog
car

I've typed this code but it does not work properly (I dont understand
very well
the pipe redirection) and note that I don't know how to recover the
stdout
at the "default:" of the second switch (so I write to stderror).
The result of my program is: tree, tree, house, dog, car
(so the uniq command does not act) and the program
gets stuck.

Anyone can help me with this? I'm sure that if you are a very expert
programmer with this you will see the solution rapidly.
I've typed in uppercase the weak points of the code for helping
in correct this...
Please help me because I need a solution before 27th May.

Note: the code should be envolved by a while (1==1) but I don't
have success because I get reading pipe errors.

Code follows:

#include <stdio.h>
#include <strings.h>
#include <unistd.h>

#define MAX_LONG 40

void error(char *message)
{
  write(2,"\n",1);
  write(2,message,strlen(message));    
  write(2,"\n",1);
  exit(1);

Quote:
}

int main(void)
{

int child1, child2, st1,st2;
int p1[2],p2[2],p3[2]; /* For the pipes */
char cadena[MAX_LONG]; /* To be readen from keyboard */
char resultat[MAX_LONG]; /* To be shown to screen */
int n, cont;

n=0;
cont=0; // Counter for the number of words.

if (pipe(p1)<0) error("Error pipe 1");
if (pipe(p2)<0) error("Error pipe 2");
if (pipe(p3)<0) error("Error  pipe 3");

/* I SUPPOSE THE WHILE (1==1) WOULD BE HERE */
/* CORRECTING THE CODE FOR RUNNING WITH THAT WHILE WILL BE
/* APPRECIATED TOO. THANK YOU */

switch (child1=fork())
{

        case -1: error("Error creating CHILD1");
        case 0:
                /* Redirect pipe to stdin */
                close(0); dup(p1[0]);
                /* Redirect pipe to stdout */
                close(1); dup(p3[1]);

                /* Close unnecesary pipes */
                close(p1[0]); close(p1[1]);

                close(p2[0]); close(p2[1]);
                close(p3[0]); close(p3[1]);
                execl("/usr/bin/sort", "sort", "-r", (char *)0);
                error("Error in CHILD1");
        default:

                /* --- MAIN PROCESS --- */

                 /* Redirect pipe to stdout */
                   close(1); dup(p1[1]);
                  close(p1[0]); close(p1[1]);

                   /*  Read words from stdin */
                   while ((n=read(0,cadena,MAX_LONG))>0 && cont<5)

                   {
                            cont++;
                            write(1,cadena,n);  
                    }

                  close(1); /* Need to send an EOF to CHILD1. */

                  switch (child2=fork())
                  {
                        case -1: error("error creating CHILD2");
                        case  0:

                               /* ARE CORRECT THE FOLLOWING
REDIRECTING */
                               /* AND CLOSING SENTENCES? */
                               close(0); dup(p2[0]);
                               close(1); dup(p3[1]);

                                /* Close unnecessary pipes */
                                close(p3[0]); close(p3[1]);
                                close(p2[0]); close(p2[1]);
                                close(p1[0]); close(p1[1]);
                                execl("/usr/bin/uniq", "uniq", (char *)0);
                                error("Error en fill2");

                        default:

                                /* Recover words from pipe.... */

                                while ((n=read(p3[0],resultat,MAX_LONG))>0)          
                                {

                                   /* WRITE THE WORDS TO STDERROR */
                                   /* I DON'T KNOW HOW TO USE STDOUT
BECAUSE*/
                                   /* IT IS PROBABLY CLOSED...*/
                                    write(2,resultat,n);

                                 }

                                  /* I'M NOT SURE ABOUT THIS... */
                                  close(p2[0]); close(p2[1]);
                                  close(p3[0]); close(p3[1]);

                                 /* COMMENTED WAITs BECAUSE DON'T WORK
*/
                                 /* THE PROGRAM GETS STUCK IF I
UNCOMMENT THEM*/
                                 /*
                                 wait(&st1);
                                 wait(&st2);

                                 */
                   }    

    }

return 0;

Quote:
}



Fri, 11 Nov 2005 00:46:14 GMT  
 Need urgent help with this (fork, dup, pipes)


Quote:
> Hi,

> I'm trying :( to do a program for linux that envolves one main process
> and 2 childs.

<snip>

Quick! Post this to a Linux newsgroup and don't look back!

    Ed.



Fri, 11 Nov 2005 00:52:23 GMT  
 Need urgent help with this (fork, dup, pipes)


Quote:



> > I'm trying :( to do a program for linux that envolves one main process
> > and 2 childs.

> <snip>

> Quick! Post this to a Linux newsgroup and don't look back!

I notice that Endymion has already posted his message to
comp.unix.programmer and gotten a response.

Endymion, please read this group's welcome message:

http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html

Regards,

Russell Hanneken



Fri, 11 Nov 2005 01:23:44 GMT  
 Need urgent help with this (fork, dup, pipes)
On Sun, 25 May 2003 11:52:23 -0500, in comp.lang.c , "Ed Morton"

Quote:



>> Hi,

>> I'm trying :( to do a program for linux that envolves one main process
>> and 2 childs.

><snip>

>Quick! Post this to a Linux newsgroup and don't look back!

All this talk, it makes me sleepy....
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---



Fri, 11 Nov 2005 07:55:22 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. fork(), pipe(), dup()

2. URGENT : Help needed with system()/fork() call

3. dup()'ed pipe()'s to stdio

4. Help: pipe() and fork()

5. Help: pipe() and fork()

6. HELP: 1 pipe to connect many forked processes to 1 parent

7. Pipe/Fork Help

8. URGENT URGENT dlopen() help needed...

9. Pipes and fork problems!

10. question on fork and pipe

11. fork and pipe tyro

12. Output lost in my window after pipe and fork

 

 
Powered by phpBB® Forum Software