Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

help with fork() pipe and dup

i need to write a program that get the argument through argv[] and do a checksum to the input files where:
argv[1]-the program to run eg.md5sum
argv[2]-the output file with all results
argv[3..]- the files
the program need to run all checks in parallel using fork() an the output file suppose to be saved in the same order of the file input.
eg ./a.out md5dum output test test2 test3
the program suppose to run 3 child process an send the result to parent that will write
buhdubhaug80u48y9 test
ugahwerg8yu089gaw test2
uohgsdog8h840hgas test3
i wrote a good code but i can only see the first file output (test) and can't see the rest. if i dont use the dup2 command i can see the output but to console and i need ot to go to a file so i'm sure the problem is in the pipe somewhere
i really hope you can help
tnx
btw is there a way do debug child? (i'm using eclipse)
[CODE]
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SIZE 60

int main (int argc, char* argv[]){
int fdfork, fdfile,fdf;
int pfd[argc - 3][2];
char temp_arr[SIZE];
int i,k,j;

//in order to create son processes and pipe matrix for all son

for(i = 0 ; i < argc-3 ; i++){
if(pipe(pfd[i]) == -1)// if pipe() didnt succeed exit
exit(1);

fdfork = fork();
if(fdfork == -1) // if fork() didnt succeed exit
exit(1);

if (fdfork > 0) // father close the writing entrence in the pipe matrix
close(pfd[i][1]);

else if(fdfork==0)//for each son determin an index i and exit the loop
break;
}
if(fdfork==0){
//son process only will enter open files on argv simultanesly and write to father process
close(pfd[i][0]);//close reading pipe
dup2(pfd[i][1],1);//change output to pipe
execlp(argv[1],argv[1],argv[i+3],NULL);//run program
exit(-1);//if running program failed
}
//father wait for the all children
for(j = 0 ; j< argc-3 ; j++)
wait(NULL);

for(j = 0 ; j< argc-3 ; j++)
close(pfd[j][1]);//close writing pipe

fdfile = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0600); // the file for writing
dup2(fdfile,1);//chane output to write to file
for(k = 0 ; k < argc-3 ; k++){
fdf = read(pfd[k][0], temp_arr ,sizeof(temp_arr));//read from pipe
temp_arr[fdf-1]='
'; //add
to add a row
temp_arr[fdf]='';//so we can print
printf("%s
",temp_arr);//clear array (not neccesary?
for (i=0 ; i<SIZE ; i++)
temp_arr[i]='0';
}
return 0;
}
[/CODE]

Comments

Sign In or Register to comment.