Unix Programming - resources inherit among parent process and all of his child processes

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2005 > resources inherit among parent process and all of his child processes





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author resources inherit among parent process and all of his child processes
up3000@gmail.com

2005-08-02, 2:56 am

hi.
I have problem in understanding underlying mechanism of fork(),
especially resouces inherit.
i wrote somecode blow:

#include <pthread.h>
#include <stdio.h>
int main()
{
int v=10;
pid_t pid;
if((pid=fork())==0)
{
v=20;
printf("-----child v=%d\n",v);
printf("-----child v addr=%x\n", &v);
sleep(5);
}
else {
printf("-----parent v=%d\n",v);
printf("-----parent v addr=%x\n", &v);
waitpid(pid,&i,0);
}
v=v-11;
printf("commv=%d\n",v);
return 0;
}

--result---
-----parent v=10
-----child v=20
-----child v addr=bffffaa0
-----parent v addr=bffffaa0
commv=9
commv=-1


My original goal is to check different value of variable V where each
V has been chaged by self-process. values have been chaged as i
expected. but,i found an interesting point, when i print each address
of V. there are same. so the situation is that the different varible
has same address with different values. i lost my idea completely.
so ,anyone could express me why it happened.

/ ****************************************
************************
****************************************
*************************/
There is another problem.
recently i review some C code writed by my privious workmate, i just
maintains it.

here is code frame,for better understanding , i delete some specfic
code segment
..
..
..
pid_t pid[10];
for(int i=0;i<10;i++){

if((pid=fork())==0)
{
//do something
// focus on here,all subprocess keep on running to
AABBBAAA instead of exiting

}
}

AABBBAAA // here, code will be executed independently by each
subprocess and main process as well.

as i thought,The design above make me into troubles in
synchronization,debug is also a big issue.
i just wander whether such a design is a good framework or not. but
i do feel it made my life more paining.

i hope someone could gimme some advices. maybe you have experenced
such situation or you have better framework

Barry Margolin

2005-08-02, 2:56 am

In article <1122960012.957291.321390@g49g2000cwa.googlegroups.com>,
"up3000@gmail.com" <up3000@gmail.com> wrote:

> My original goal is to check different value of variable V where each
> V has been chaged by self-process. values have been chaged as i
> expected. but,i found an interesting point, when i print each address
> of V. there are same. so the situation is that the different varible
> has same address with different values. i lost my idea completely.
> so ,anyone could express me why it happened.


Addresses are virtual, meaning that they're private to each address.
The virtual memory system of the operating system translates them to the
hardware addresses appropriate to each process's memory.

>
> / ****************************************
************************
> ****************************************
*************************/
> There is another problem.
> recently i review some C code writed by my privious workmate, i just
> maintains it.
>
> here is code frame,for better understanding , i delete some specfic
> code segment
> .
> .
> .
> pid_t pid[10];
> for(int i=0;i<10;i++){
>
> if((pid=fork())==0)
> {
> //do something
> // focus on here,all subprocess keep on running to
> AABBBAAA instead of exiting


This is going to cause exponential process creation, because each of the
child processes will perform further iterations of the for() loop. But
maybe there's a check that you left out that prevents it.

>
> }
> }
>
> AABBBAAA // here, code will be executed independently by each
> subprocess and main process as well.
>
> as i thought,The design above make me into troubles in
> synchronization,debug is also a big issue.


Synchronization requires using some locking mechanism, like a semaphore.
Maybe things would be easier if you used threads instead of processes.

> i just wander whether such a design is a good framework or not. but
> i do feel it made my life more paining.


Your question is very vague, so I'm not sure how to advise you, except
by suggesting that you need to read a textbook on advanced Unix
programming. It's a little old, but Advanced programming in the Unix
Environment would probably be pretty good.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Pascal Bourguignon

2005-08-02, 6:05 pm

"up3000@gmail.com" <up3000@gmail.com> writes:
> [...]
> if((pid=fork())==0)
> [...]
> so ,anyone could express me why it happened.


Conceptually, fork copies the whole state of the process.

typedef struct {
registers_t registers;
/* ... other kernel stuff: file descriptors, pid, gid, etc */
unsigned byte memory[0x100000000]; } state_t;

state_t processes[MAX_PROC];

int fork(){
int child=newpid();
memcpy(&(processes[child]),&(processes[getpid()]),sizeof(state_t));
processes[child].pid=child;
/* actually, some more kernel stuff divergence */
from parent return child;
from child return 0; }

So when you write:

x=v;
p=&v;

what is executed is conceptually:

processes[getpid()].memory[&x]=processes[getpid()].memory[&v];
processes[getpid()].memory[&p]=&v;

When you are in different processes, getpid() returns a different pid,
so different real memory cells are assigned.


--
__Pascal Bourguignon__ http://www.informatimago.com/
The rule for today:
Touch my tail, I shred your hand.
New rule tomorrow.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com