shared memory problem

This is a discussion on shared memory problem within the C Programming forums, part of the General Programming Boards category; I am supposed to create shared variables in which multiple processes (p1 to p8) update it. Say processes p1-p4 update ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    12

    shared memory problem

    I am supposed to create shared variables in which multiple processes (p1 to p8) update it. Say processes p1-p4 update shared variable x1 and processes p5-p8 update shared variable x2.

    how do it do it ?

    is it like ?
    Code:
    main 
    
    create shared variables x1 and x2
    
    for (i=1;i<=4;i++)
    {pid=fork()
    if(pid==0)
    update x1;
    }
    
    for (j=1;j<=4;j++)
    {pid=fork()
    if(pid==0)
    update x2;
    }
    I doubt in it because. I think second fork will create some problems as it will fork children which has childrens generated in earlier for loop..

    I am confused how to solve it...

    --------------------------------------------------------------------------------

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    Code:
    isChild = 0;
    for ( group = 0 ; group < 2 && !isChild; group++ ) {
      for ( i = 0 ; i < 4 && !isChild ; i++ ) {
        if ( fork() == 0 ) { isChild = 1; }
      }
    }
    
    // At this point in the code, you will have 9 processes running in parallel
    // the parent will have isChild = 0, the rest will have isChild = 1
    // 4 of the children will have group = 0
    // 4 of the children will have group = 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    12
    ohhh....thanks a lot Salem, I will try it out and let you know if i fall into any trap...

    Thanks a lot ...

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    12
    say if i use only like this

    for (i=0;i<2;i++)
    fork()

    then in that case, will it go like this ??

    when i =0

    child1 , similar to parent created

    when i=1;

    child 2 similar to child1 (which is again similar to parent) created.

    when i=2;
    child3 , similar to child2 (which is again similar to child1, which is similar to parent) is created..

    so,

    child1 will be like parent
    child 2 will be something like parent, child1 and other child
    child3 will be something like parent,child1,child2 and child3

    in total we will have 6 childs..
    am i correct ??

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    12
    But when I wrote simple prog. and tested, it produced 8 children...

    why ??

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Trace is as follows:

    Code:
    i = 0;
    parent forks()....
    We now have 2 processes.  1 parent and 1 child
    
    i = 1;
    parent forks()....
    We now have 3 processes.  1 parent and 2 children
    child forks()....
    We now have 4 processes.  1 parent and 2 children and 1 grandchild
    
    i = 2;
    Loop ends.
    Total processes: 4
    I wrote the following bad code really quickly:

    Code:
    #include <stdio.h>
    #include <wait.h>
                            
    int main()
    {
            int i, iReturn;
            pid_t pid;
            for(i=0;i<2;i++)
            {
                    pid = fork();
                    printf("%d reporting for duty\n",getpid());
                    if(pid > 0) 
                    {
                            pid = wait(&iReturn);
                    }
            }
            return 0;
    }
    Output:

    Code:
    17244 reporting for duty
    17245 reporting for duty
    17245 reporting for duty
    17246 reporting for duty
    17244 reporting for duty
    17247 reporting for duty
    Now you'll get 6 entries, but notice the identical PID's? That's because you're in a loop and printing notices multiple times by some of the processes.

    Taking out the duplicate entries:

    Code:
    17244 reporting for duty
    17245 reporting for duty
    17246 reporting for duty
    17247 reporting for duty
    Surprise! Only 4 processes.
    Last edited by MacGyver; 03-25-2007 at 01:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 11:27 AM
  2. Partly shared memory
    By DrSnuggles in forum C++ Programming
    Replies: 13
    Last Post: 01-21-2009, 02:35 AM
  3. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  4. Shared memory woes
    By joshdick in forum C Programming
    Replies: 4
    Last Post: 07-28-2005, 08:59 AM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21