How to pass arguments from parent process to child process in Unix?

Question

Inside my code I want to...

1) the parent process will create an array with at least 10 element

2) the child process will calculate the production of all elements with odd index inside the array

3) the child process will provide the result to the parent process when it finish calculation and then the child process will terminate

4) the parent will calculate the production after it get the result from the child process

5) the parent process will finally output the results.

Now the CODE LOGIC is easy to write which is down below

int cal(int arr[10]) {
    int i=0;
    int sum = 0;
    for (i=1; i<10; i=i+2) {
        sum = sum + arr[i];
    }

    return sum;
} // end of calc

int main() {
    int arr[] = { 10, 20, 25, 5, 6, 45, 87, 98, 23, 45};
    int sum = cal(arr);

    printf("Sum of all odd indexs element is : %d", sum);

    return 0;
} // end of main

And here is the code for creating a child process using fork()

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int main() {
    pid t pid;
    /* fork a child process */
    pid = fork();
    if (pid < 0) { /* error occurred */
        fprintf(stderr, "Fork Failed");
        return 1;
    }
    else if (pid == 0) { /* child process */
        execlp("/bin/ls","ls",NULL);

    }
    else { /* parent process */
        /* parent will wait for the child to complete */
        wait(NULL);
        printf("Child Complete");
    }
    return 0;
} // end of main

My questions are...

  • How would I use the CODE LOGIC and combine it with creation of the child process using fork()? If the pid == 0, then the creation of a child process was successful so I think that is where we insert the code for step 2... 2) the child process will calculate the production of all elements with odd index inside the array.

  • How would the parent send the array to the child process so that the child process could sum the elements with odd index?

UPDATED CODE: I combined both codes above into one

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

/*
calculate the production of all elements with odd index inside the array
*/
int cal(int arr[10]) {
    int i=0;
    int sum = 0;
    for (i=1; i<10; i=i+2) {
        sum = sum + arr[i];
    }

    return sum;
} // end of calc

int main() {
    pid t pid;
    /* fork a child process */
    pid = fork();
    if (pid < 0) { /* error occurred */
        fprintf(stderr, "Fork Failed");
        return 1;
    }
    else if (pid == 0) { /* child process */
        print("I am the child process");
        // the child process will calculate the production 
        // of all elements with odd index inside the array
        calc();
        // the child process will provide the result to the parent process 
        // when it finish calculation and then the child process will terminate
        exit(0);

    }
    else { /* parent process */
        /* parent will wait for the child to complete */
        printf("I am the parent, waiting for the child to end");
        // the parent process will create an array with at least 10 element
        int arr[] = { 1, 2, 5, 5, 6, 4, 8, 9, 23, 45 };
        int sum = calc(arr);
        wait(NULL);
        printf("Child completed calculating the production of all elements with odd index inside the array");
        // the parent will calculate the production after it get the result from the child process
        // the parent process will finally output the results.
        printf("Sum of all odd indexs element is : %d", sum);
    }
    return 0;
} // end of main

Show source
| C   | arrays   | child-process   | parent-child   | process   2017-10-04 23:10 2 Answers

Answers to How to pass arguments from parent process to child process in Unix? ( 2 )

  1. 2017-10-05 00:10

    There are inter-process communication (IPC) mechanisms allowing you to pass information between processes.

    As a rule, fork is the only way to create new processes in Unix-like systems. At that, child process inherits code and address space of parent. It means that child is a duplicate (in some degree, see link above) of parent at this point of time.

    In modern Unix variants and in Linux, fork is implemented using copy-on-write pages. It just means that when parent or child process tries to modify shared memory page, operating system creates a copy of this page. Now parent and child have own memory page.

    System call exec replaces the current process image with a new process image. It means that parent and child processes wouldn't share any memory pages or code now.

    In your program you shouldn't call execlp(). Use advantages of copy-on-write mechanism. So do fork() in the main() function in your CODE LOGIC program after defining the arr. Then access arr from the child process. Use wait() system call to make parent is blocked until child doesn't finish.

    You should use IPC to return result from the child process. In your case pipes are the best choice. But it's obvious you do lab assignment about Unix processes, and not about IPC. So you may return result via exit code of child process. Pass result to the exit() function. Note that you can pass only 8 bits (see comments under my answer).

    This is a working code:

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    
    int calc(int *arr, int n) {
        int sum = 0;
        for (i = 1; i < n; i += 2) {
            sum = sum + arr[i];
        }
        return sum;
    }
    
    int main(void) {
        int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        int n = sizeof(arr) / sizeof(arr[0]);
    
        pid_t pid = fork();
        if (pid < 0) {
            perror("fork failed");
            return 1;
        }
        else if (pid == 0) {
            printf("I am the child process\n");
    
            int child_sum = calc(arr, n);
            exit(child_sum);
        }
        else {
            printf("I am the parent process\n");
    
            int parent_sum = calc(arr, n);
    
            int child_sum;
            if (wait(&child_sum) == -1) {
                perror("wait failed");
            }
            else {
                printf("Sum by child: %d\n", child_sum);
            }
    
            printf("Sum by parent: %d\n", parent_sum);
        }
    
        return 0;
    }
    
  2. 2017-10-05 01:10

    Here execlp will give your child process a new address space. So you practically can't send an argument to the child process from parent process. But you can do as follows,

    #include <sys/types.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int main() {
        pid_t pid;
        /* fork a child process */
        pid = fork();
        int sum = 0;
        int arr[] = { 10, 20, 25, 5, 6, 45, 87, 98, 23, 45};
        if (pid < 0) { /* error occurred */
            fprintf(stderr, "Fork Failed");
            return 1;
        }
        else if (pid == 0) { /* child process */
            int i=0;
    
            for (i=1; i<10; i=i+2) {
                sum = sum + arr[i];
            }
    
            return sum;
    
    
        }
        else { /* parent process */
            /* parent will wait for the child to complete */
            wait(NULL);
    
        int i=0;
    
            for (i=1; i<10; i=i+2) {
                sum = sum + arr[i];
            }
    
            printf("%d\n",sum);
        }
        return 0;
    }
    

    ps- Here the array is declared after the fork() so it is common to both parent and child processes.

Leave a reply to - How to pass arguments from parent process to child process in Unix?

◀ Go back