Thread: Executing more than one command with fork() and exec()

  1. #1
    Registered User
    Join Date
    Mar 2024
    Posts
    9

    Executing more than one command with fork() and exec()

    The following program:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void) {
        int status = system("ls /");
        
        if (status == -1) {
            fputs("Error when creating child process or retrieving its exit status.\n", stderr);
        }
        else {
            printf("Child process exited with exit status %d\n", status);
        }
        
        return 0;
    }
    Is roughly equivalent to the following one using fork() and exec():
    Code:
    #define _POSIX_C_SOURCE 200809L
    
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/wait.h>
    
    int main(void) {
        pid_t pid = fork();
        
        switch (pid) {
            case -1: // Couldn't create process
                fputs("Couldn't create another process\n", stderr);
                break;
            
            case 0: // Child process
                execlp("ls", "ls", "/", (char *)0);
                fputs("Couldn't replace the child process image\n", stderr);
                break;
                
            default: // Parent process
                ;
                int status;
                pid_t wpid = wait(&status);
                if (wpid == pid) {
                    printf("Child process exited with exit status %d\n", status);
                }
        }
        
        return 0;
    }
    But how could this one be rewritten using fork() and exec()?
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void printstatus(int status) {
        if (status == -1) {
            fputs("Error when creating child process or retrieving its exit status.\n", stderr);
        }
        else {
            printf("Child process exited with exit status %d\n", status);
        }
    }
    
    int main(void) {
        int status = system("ls /");
        printstatus(status);
        
        status = system("ls /tmp");
        printstatus(status);
        
        return 0;
    }
    Basically my question is how to create a second fork of the process and change its flow so that it executes a different command. Or better still, how to create a fork and execute a command specified in an arbitrary string the same way it is passed to system() as an argument.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    Simplistic example
    Code:
    #define _POSIX_C_SOURCE 200809L
     
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/wait.h>
     
    int mysystem(const char *command) {
        int status = -1;
        const char *cmd = command ? command : "exit 0";
        
        pid_t pid = fork();
    
        switch (pid) {
            case -1: // Couldn't create process
                break;
             
            case 0: // Child process
                execl("/bin/sh", "sh", "-c", cmd, (char *) NULL);
                break;
    
            default: // Parent process
                wait(&status);
        }
    
        return command ? status : !status;
    }
    
    void printstatus(int status) {
        if (status == -1)
            fputs("Error when creating child process or retrieving its exit status.\n\n", stderr);
        else
            printf("Child process exited with status %d\n\n", status);
    }
     
    int main(void) {
        int status = mysystem(NULL);
        printf("Checking if command processor exists: %d\n\n", status);
    
        status = mysystem("ls /");
        printstatus(status);
         
        status = mysystem("ls /tmp");
        printstatus(status);
         
        return 0;
    }
    Last edited by john.c; 1 Week Ago at 05:15 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Mar 2024
    Posts
    9
    Oh, so the child process' instructions begin at the point where the fork() function was called..... I was expecting it to begin from main(), so I was overcomplicating stuff!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fork + Exec + ? Question!
    By Ignorant in forum Linux Programming
    Replies: 1
    Last Post: 04-08-2015, 07:46 PM
  2. Replies: 5
    Last Post: 02-27-2014, 03:53 AM
  3. Fork() and exec() help
    By TuXaKoS in forum C Programming
    Replies: 3
    Last Post: 11-01-2010, 11:35 AM
  4. Question about fork and exec
    By steli89 in forum Linux Programming
    Replies: 4
    Last Post: 04-13-2010, 07:17 AM
  5. fork + exec
    By vipul_vgp in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 08:00 AM

Tags for this Thread