Fork() and Static vars

I’m coding a program using the fork() function…

as written in the manual, the variables are indipendent one thime the fork is called.
but, how can I set a variable common to all the processes?
I declared it as static but it doesn’t work at all…

now, is qnx that have some differences declaring a static variable(and I’m new on qnx, so I don’t know it) or am I a cretin?

(probabilly someone wil say “cretin”… stay allert: I will kill you…with -9)

You need to create a shared memory region with shm_open and mmap.

This is not perticular to QNX but to all Unixes.

mmm I’ve used fork only under linux systems ind as I can remember it always worked without mmap…

now don’t care…Thanks a lot!!

with apparently not reason (or maybe, because of my ignorance) my prog doesn’t work because I used " *i++; "…but if i use: *i = *i+1 all is ok…

omg man :slight_smile:

    i = 0;
    ...
    *i=100; // this may SIGSEGV because u did i=0 (at least it does on my 4.25e and 6.2.1)
    ...
    printf ("%d\n", i);   // and this will print 0 because u did i=0

u forgot to dereference the pointer
what u’ve been trying to do was probably this :slight_smile:

    *i = 0;
    if (pid == 0)
    {
        printf ("%d\n", *i);
    }
    else
    {
        *i=100;
        printf ("%d\n", *i);
    }

oops, u deleted your previous post
nevermind, ++ works well just have to follow the rules of operator precedence

(*i)++

More details. When using fork(), when the child is create, it will inherit all the variables content of the parent. But after that the processes are independant and have their own data space. That is the behavior of fork as defined in SVr4, SVID, POSIX, X/OPEN, and BSD 4.3.

tfork() or pthread_create does behave as you suggested.

Thanks mezek, but at first I’ve posted the wrong source :smiley: (me stupid!)
and two, I solved the problem :smiley:… I met some diffcult (??) because this is my first coding work after a loooot of time (where the hardest thing I’ve done with the pc is “route add”…sadness!!) so I’ve got some kind of rust inside mty brain :smiley:

thanks mario but I solved this using pthread_create because is more appropiate to use in my case than fork family…

Argh, You are too right!!! I’m a chiken!! :unamused: