So I have 2 command line programs that are made to work together. The first one receives 2+ files as parameters. The first file is a list of commands, the following parameters are files to call the commands on.
My first program create a fork for each file to be modified and execute the second program with execl().
Here's a part of the code
Main program:
Code:
int main(int argc, char const *argv[]){
...
// Multiple forks
for(i=2; i < argc; i++){
...
pipe(pipes[forks]);
forks++;
pid = fork();
if(pid == 0) break;
...
}
if(pid == 0){
dup2(pipes[forks-1][0], STDIN_FILENO);
// Execute the second program in the fork,
// pass a file to be read as a single argument
execl("second_program", "second_program" , argv[i], NULL);
fprintf(stderr, "execl() failed\n");
return EXIT_FAILURE;
} else {
...
// Reads a file containing commands, normalize it and output a string
char *cmds = getcmds(argv[1]);
int f;
for(f=0; f < forks; f++){
// Send the list of commands to the second program
write(pipes[f][1], cmds, strlen(cmds));
}
wait(NULL);
}
return 0;
}
Child program:
Code:
int main(int argc, char const *argv[]){
...
char buffer[READ_SIZE];
do {
scanf("%s",buffer);
if(strcmp(buffer,"COMMAND_A") == 0){
...
printf("`COMMAND_A on %s\n", filename);
} if(strcmp(buffer,"COMMAND_b") == 0){
...
printf("`COMMAND_b on %s\n", filename);
} else {
break;
}
...
} while(1);
return 0;
}
My problem is that rather than running concurrently, each fork waits for the previous one to have completed all it's commands before starting it's own.
I get something like is as an output:
Code:
COMMAND_A on file1
COMMAND_B on file1
...
COMMAND_Z on file1
COMMAND_A on file2
COMMAND_B on file2
...
COMMAND_Z on file2
COMMAND_A on file3
COMMAND_B on file3
...
COMMAND_Z on file3