Magnum
Magnum

Reputation: 83

Global variable not updated by child processes in C

I am trying to print the words couted by the child processes and add them up.

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

count_t word_count(char *file)
{
   -counts the words from a file---
 }



 int main(int argc, char **argv)
{
    int i,j, numFiles,pid_count[10],pid;

    int global;



    numFiles = atoi(argv[1]);


    printf("counting %d files..\n", numFiles);
    j=0;
    for(i = 0; i < numFiles; i++)
    {


            pid_count[i] = fork();

                if(pid_count[i] < 0) {
                printf("Error creating the child process\n");

                } else if (pid_count[i] == 0) {

                char filename[100];
                sprintf(filename, "%s/text.%02d", FILEPATH, i);
                printf("read: %s\n", filename);
                int local;
                printf("Child PID: %d Handling File No : %d\n",getpid(), i);
                local = word_count(filename);
                global += local;

                exit(0);

                } else  {
                continue;
                }



    }
    int wstatus;
    for(i = 0; i < numFiles; i++)
    {
            waitpid(pid_count[i], &wstatus, 0);

    }
        printf("total word count is %d",global);

    return(0);
 }

but in the output the global variable prints 0 only while each local variable has the exact count, it just dont add up to global

Upvotes: 1

Views: 2912

Answers (2)

axiac
axiac

Reputation: 72206

A child process is a different process. It runs the same code as the parent process but it has its own copy of the data segment of its parent.

Each child process modifies its own copy of local and global variables. They are different than the global variable defined in the parent process (because they are in a different process and there is no memory shared between them.)

There are several ways to let the child processes send data to the parent process. The easiest way, I think, is to use pipes. Read this tutorial. It contains a small program that can help you understand the concept.

Upvotes: 1

rodrigo
rodrigo

Reputation: 98348

Processes just do not work that way. Each child process gets a copy of the memory of the parent process, but it is just that: a copy. Changes made to the children memory are not visible to the parent, that is they do not share memory.

If you want to share memory with the children, you may consider using threads instead of processes. Or if you are willing, you can use shared memory (there are several ways to share memory between processes). But anyway, you will need some synchronization primitives, you do not want to have all the threads/processes stepping into the same memory location at the same time...

Another different alternative would be not to share memory at all, but instead each child could send the result value to the parent though some interprocess communication (pipe or something), and the parent will add them up.

Upvotes: 7

Related Questions