Help: fork expert needed!

Help: fork expert needed!

Post by Sylvain Lemieu » Wed, 25 Nov 1998 04:00:00



Hello,

Consider the following program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int Count=8;

int main(void)
{
    if (fork()==0)
      printf("Child: Count(0x%x)=%d\n",&Count,++Count);
    else
      printf("Parent: Count(0x%x)=%d\n",&Count,++Count);

Quote:}

and its output:

Parent: Count(0x804a0d8)=9
Child: Count(0x804a0d8)=9

According to what I can understand, this seem normal since the memory is
duplicated when creating a new process, BUT!

I would like to be able to share the global variable Count between the
two process, so the program's output would be:

Parent: Count(0x804a0d8)=9
Child: Count(0x804a0d8)=10
or
Child: Count(0x804a0d8)=9
Parent: Count(0x804a0d8)=10

depends who takes the control first. I know that I might need some
monitoring macanism to ensure the two process don't access the Count
variable at the same time, but how can I share memory between two
processes?

Thanks for any help!
--
Sylvain Lemieux, ing.
PCI-Carto Inc. (The french connection)

        On ne peut tomber que si l'on essaie de marcher
        Every rain cloud has a silver lining

 
 
 

Help: fork expert needed!

Post by Rainer Temm » Wed, 25 Nov 1998 04:00:00



> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>

> int Count=8;

> int main(void)
> {
>     if (fork()==0)
>       printf("Child: Count(0x%x)=%d\n",&Count,++Count);
>     else
>       printf("Parent: Count(0x%x)=%d\n",&Count,++Count);

> }
> I would like to be able to share the global variable Count between the
> two process, so the program's output would be:

> Parent: Count(0x804a0d8)=9
> Child: Count(0x804a0d8)=10
> or
> Child: Count(0x804a0d8)=9
> Parent: Count(0x804a0d8)=10

> depends who takes the control first. I know that I might need some
> monitoring macanism to ensure the two process don't access the Count
> variable at the same time, but how can I share memory between two
> processes?

This can't be done with a normal variable (global or not)...
You need to attach a small shared-memory segment to
your processes.
Regards Rainer

 
 
 

Help: fork expert needed!

Post by Another !**?#!#$ T » Wed, 25 Nov 1998 04:00:00


Quote:> ) According to what I can understand, this seem normal since the memory is
> ) duplicated when creating a new process, BUT!
> )
> ) I would like to be able to share the global variable Count between the
> ) two process, so the program's output would be:

On most unices, you should be able to set up shared memory with mmap or
shmem. I don't know about mmap, but if you set up shmem before the fork,
but parties get it after.

Also, it better to do

        int pid = fork();
        if (pid<0) failure
        else if (pid==0) child
        else {
                parent
                . . .
                wait for (pid)
        }

- fork can fail.
- it simplifies waiting for the child to complete if you have it's pid.

--
Then third I rede thee, threaten not        |meow.
with fools in councils, founderred thought. |I don't use no smilies.

When maddenned haste is made his rule.      +------------------------
http://www.geocities.com/SoHo/Studios/5079/index.html

 
 
 

Help: fork expert needed!

Post by F.R.M.Barn » Thu, 26 Nov 1998 04:00:00


: According to what I can understand, this seem normal since the memory is
: duplicated when creating a new process, BUT!
: I would like to be able to share the global variable Count between the
: two process, so the program's output would be:

Threads are a probably a good way of doing this (man pthread_... or
thr_...).  If you've got Linux, the system call 'clone' duplicates
the program but not the data.  To share the variable, use pthread_mutex
or something similar.  (dekkers algorithm, petersons algorithm, ...)
There are probably other methods of doing this...

Fred.

--
+----------------------------------------------------------------------+
| Fred Barnes, CS Student, UKC                http://teddy.xylene.com/ |

+----------------------------------------------------------------------+

 
 
 

1. comsat forking, forking and forking (3.2.0)


94

Has anyone an idea on what could have caused all those comsat
processes to appear in less than one night? (I got really heavy mail
traffic this nite, so I guess it's related).

Would replacing comsat with the BSD one help solving the problem?

--

   "No keyboard, press F1 to continue"

2. LOCAL: Toronto Linux Users Group status

3. ****NEED EXPERT NETWORK ADMIN TO HELP SET UP A SMALL TCP/IP NET****

4. EUID for a connection

5. RCS Expert Help Needed !

6. Linux Animation Applications

7. Need expert help please - Error Rebuilding kdebase-3.0.4

8. hosts.deny for unknown IP addresses

9. Need Expert Help: linux/limits.h linux/fs.h select

10. To all LILO's expert, HELP NEEDED!!!

11. IP Routing problem... Need expert help...

12. need expert help with unusual multi-pc networking situation

13. Need expert help!