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:
}