1

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

Magnum
  • 83
  • 1
  • 5
  • 1
    Possible duplicate of [About fork system call and global variables](http://stackoverflow.com/questions/1252712/about-fork-system-call-and-global-variables) – DYZ Feb 08 '17 at 00:59
  • There's no such thing as "multi-process global variable" at C language level. This is something you will have to implement manually through platform-specific means. (That is not even mentioning that there are no global variables in your code at all.) – AnT stands with Russia Feb 08 '17 at 00:59
  • Maybe you should be using one or perhaps several pipes so that the children can write the information back to the parent and the parent can process the data. – Jonathan Leffler Feb 08 '17 at 01:21

2 Answers2

7

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.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
1

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.

axiac
  • 68,258
  • 9
  • 99
  • 134