'fork' query

M

Mike

Hi everyone

i am currently learning about child processes. can anyone explain this
code:


#define N (10)
int i;
int pidarray[N];

void main(argc, argv)
int argc; char *argv[];
{
printf("Create Children ...\n");
for (i = 0; i < N; i++)
if (!fork()) {
printf("Child %d createdwith pid %d \n", i + 1, getpid());
pidarray = getpid();
_exit(0);
}
sleep(3);
for (i = 1; i < N; i++) {
printf("%d\n", pidarray);
}
}


It prints out the child pid values, but when i try to read the values back
from the array, they are all zero. Why?
 
I

Ian Collins

Mike said:
Hi everyone

i am currently learning about child processes. can anyone explain this
code:
comp.unix.programmer is the place to ask.


A couple of C points:
#define N (10)
int i;
int pidarray[N];
Why are these global?
void main(argc, argv)
int argc; char *argv[];

Why the archaic K&R form?
It prints out the child pid values, but when i try to read the values back
from the array, they are all zero. Why?

Copy on write, for more details, ask on c.u.p.
 
A

Antoninus Twink

if (!fork()) {
printf("Child %d createdwith pid %d \n", i + 1, getpid());
pidarray = getpid();


Here you change the value of the child's copy of pidarray, but later
on you read from the parent's pidrray. These are complete separate
memory locations - the child is an independent process, and it gets a
distinct copy of the parent's address space. (You also neglect to check
whether the fork()s succeed.)

You should also include the necessary headers, and look up modern
function declarations. Here's an improved version of your code - look
for the differences, and ask again if there are any changes where you
don't see why they're necessary.


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

#define N 10

int main(void)
{
int i, pidarray[N];
printf("Create Children ...\n");
for (i = 0; i < N; i++)
switch(pidarray = fork()) {
case 0:
printf("Child %d created with pid %d\n", i + 1, getpid());
exit(0);

case -1:
perror("fork");
exit(1);
}
sleep(3);
for (i = 0; i < N; i++)
printf("%d\n", pidarray);
return 0;
}
 
K

Kaz Kylheku

Hi everyone

It prints out the child pid values, but when i try to read the values back
from the array, they are all zero. Why?

Because your assignments to the array are done in the child process (where fork
returns zero) but the printing is done in the parent. With the exception of the
inheritance of shared memory mappings, parents and children have separate
address spaces. The child's memory is a copy of the parent. Changes to
variables done in the parent after the fork are not reflected in the child, and
changes to variables in the child do not affect the parent.
i am currently learning about child processes.

Now you know the second or third thing about child processes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top