Thread: Help with fork() and execl() running synchronously

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    1

    Unhappy Help with fork() and execl() running synchronously

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps you should just remove the wait(NULL) call.

    Instead read the manual page to find out all the other wait... variant calls, which allow you to say test whether any process has exited, without actually blocking the parent process.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    2
    sounds like you need a memory barrier

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Only the outout let you think that the childs runs one after one, but this is actually the buffering of each process.
    Write after all printf-commands in the child the line:
    Code:
    fflush(stdout)
    With this, the childs flushes there buffer and print out immediately.

    And how Salem wrote, look at the wait-commands.
    You should have one wait for every child that you forked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-27-2014, 03:53 AM
  2. Replies: 1
    Last Post: 09-22-2010, 05:11 AM
  3. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  4. Calling other binaries, use fork/execl or system?
    By spotvt01 in forum Linux Programming
    Replies: 3
    Last Post: 08-16-2008, 04:20 AM
  5. execl
    By laasunde in forum C Programming
    Replies: 2
    Last Post: 11-19-2002, 05:07 PM

Tags for this Thread