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
btw is there a way do debug child? (i'm using eclipse)
#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

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

if (fdfork > 0) // father close the writing entrence in the pipe matrix

else if(fdfork==0)//for each son determin an index i and exit the loop
//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++)

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
'; //add
to add a row
temp_arr[fdf]='';//so we can print
",temp_arr);//clear array (not neccesary?
for (i=0 ; i<SIZE ; i++)
return 0;


Sign In or Register to comment.

Howdy, Stranger!

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


In this Discussion