0

This is my code system call in C.

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

int n;
int i;
pid_t pid;
int time = 1000;
int sum = 0;

int main(void) {
    printf("n: ");
    scanf("%d", &n);
    pid = fork();
    
    if (pid < 0) {
        printf("Fork Failed");
        exit(-1);
    } else if (pid == 0) {
        //child
        for (i = 1; i <= n; i++) {
            sum += i;
        }
        printf("Sum of 1 to %d: %d\n", n, sum); // this is ok
    } else {
        // parent
        wait(&time);
        printf("Sum of 1 to %d: %d\n", n, sum); // this always return 0;
    }
    return 0;
}

I don't know why in parent's code block, the sum is always equal to 0. How to make parent wait for child or am I doing something wrong ?

Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    `fork` creates a complete copy of your process. The variables are not shared between the two processes. (See also https://stackoverflow.com/questions/37347140/fork-and-changing-local-variables or https://stackoverflow.com/questions/2158381/c-fork-dealing-with-global-variable) – UnholySheep Jan 22 '22 at 09:58

2 Answers2

2

Waiting for the child works. However, your expectations are wrong.

Apparently you think that computations in the child process after the fork are visible in the parent process. They are not. The child is a new copy of the parent program at the time of fork. At that time, the parent's sum is 0 and stays that way.

There are several mechanisms to pass data from child to parent (the search term is interprocess communication, IPC).

  • exit() status
  • files
  • shared memory
  • pipes
  • signals
  • message queues
  • anything else I have missed
Jens
  • 69,818
  • 15
  • 125
  • 179
0

The issue here is the variable sum is not shared by the parent & child process, after fork() call the child will have its own copy of the variable sum. Use shmget(),shmat() from POSIX api. Or use pthread which will share the same memory space for the newly created thread. Update--- Added the shared memory to your code hopes this helps.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/shm.h>

int n;
int i;
pid_t pid;
int time = 1000;

int main(void) {
    int shmid;
    int *sum;
    printf("n: ");
    scanf("%d", &n);
    /*request the shared memory from the OS using the shmget()*/
    shmid = shmget(IPC_PRIVATE, sizeof(int), 0777|IPC_CREAT);

    pid = fork();
    
    if (pid < 0) {
        printf("Fork Failed");
        exit(-1);
    } else if (pid == 0) {
        //child
        /*  shmat() returns a char pointer which is typecast here
            to int and the address is stored in the int pointer. */
        sum = (int *) shmat(shmid, 0, 0);

        for (i = 1; i <= n; i++) {
            *sum += i;
        }
        printf("Sum of 1 to %d: %d\n", n, *sum); // this is ok
        /* each process should "detach" itself from the
           shared memory after it is used */
        shmdt(sum);
    } else {
        // parent
        wait(&time);
        sum = (int *) shmat(shmid, 0, 0);
        printf("Sum of 1 to %d: %d\n", n, *sum); // this always return 0;
        shmdt(sum);
        /*delete the cretaed shared memory*/
        shmctl(shmid, IPC_RMID, 0);
    }
    return 0;
}

Refer for more info- https://man7.org/linux/man-pages/man2/shmget.2.html

Stebin
  • 24
  • 1
  • could u give me code example ? thank alot – Augustus Flynn Jan 22 '22 at 15:46
  • @AugustusTung You almost certainly do not want to see an example using shared memory until you have a better understanding of how to do this with a simpler method. Have the child write the sum to a pipe and read that in the parent. After you have experimented with that, then play with threads. When you understand the threading, then experiment (maybe) with shared memory. – William Pursell Jan 22 '22 at 17:06