fork(), parent and child processes???


 
Thread Tools Search this Thread
Top Forums Programming fork(), parent and child processes???
# 1  
Old 11-12-2011
fork(), parent and child processes???

Hi friends,
I have a small question regarding unix system call fork, I hope you will solve my problem. Here is the small program

Code:
 
$ cat fork1.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
int pid;
int x = 0;
x = x + 1;
pid = fork();
if(pid < 0)
{
printf("Fork failed\n");
return -1;
}
if(pid == 0)
printf("I am the child, my x is %d and my PID is %d\n",x,getpid());
else
printf("I am the parent, my x is %d and my PID is %d\n",x,getpid());
return 0;
}

And here is the output
Code:
$ ./fork1.exe
I am the child, my x is 1 and my PID is 1282
I am the parent, my x is 1 and my PID is 1281

Ok, when we call fork(), the following things happen
1. The parent process passes it's memory image, including the variables
Code:
pid

and
Code:
x

, to the child process that is created.
2. fork returns zero to the child, and the child's PID, a positive integer, to the parent.
3. If fork returns a negative value, it means there was some error, and no process was created.
Ok, I am going all right till now, here comes the question that has been bothering me.
How can the single variable
Code:
pid

contain two values at the same time? zero for the child process and a positive integer for the parent process? We have two values saved in the same memory location identified by
Code:
pid

, how does that make sense. Why the values are not overwritten???
I hope you guys will help me with this mystery thing, and explain it to me.
Thanks a lot, looking forward to your nice replies!
# 2  
Old 11-12-2011
The child is an exact* copy of the parent. Thus the child references a different chunk of memory for it's stack than the parent, and thus there are two sets of variables.


* "exact" with a few exceptions that come into play if you are doing more than just experimenting with the fork() mechanism. See the man page for fork() to find out what isn't copied.
# 3  
Old 11-12-2011
Quote:
Originally Posted by gabam
How can the single variable
Code:
pid

contain two values at the same time?
The address of, say, 0xbfffffffff can hold its own, separate value in each and every individual process. That's really the point of having processes -- each one gets its own flat memory space, as if it was the only thing running on the computer, but it's actually quite secure and controlled.

This virtual memory space works by dividing real memory into 4096-byte chunks, and keeping a big table of which process gets what real memory at what virtual address. This table is checked in hardware by the processor itself, and configured by the kernel. If a process tries to access a memory location where no real pages have been assigned to it, you get the familiar error "segmentation fault".

What fork() does is it makes an exact copy of the process at the time of the fork(), but gives it its own independent memory space, then twiddles the value of 'pid' so it's different in the child. It uses some tricks like copy-on-write to avoid duplicating too much memory, but that's mostly safe to ignore.
These 2 Users Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Kill Parent/ Child processes

I am trying to kill PIDs that are tied to a KSH "load_sqlplus" and I am using the below code LIST_PID=`ps -ef | grep -i "load_sqlplus" | grep -v grep | awk '{print $2}'` if ; then echo "Processes killed" "PID : " $LIST_PID kill -9 $LIST_PID else echo "Nothing to Kill" fi... (4 Replies)
Discussion started by: venky338
4 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Shell Programming and Scripting

Creating a pipe using parent and child processes

Hello, I am trying to create a pipe that will direct stdout to in side of the pipe, and stdin to the out side of the pipe - I created two child processes to handle this. However, my pipe doesn't seem to be working correctly. Did I use execv() correctly? Command1 and command2 represent the two... (3 Replies)
Discussion started by: jre247
3 Replies

4. UNIX for Dummies Questions & Answers

Kill child processes, when parent is "bash"

Consider this simple command line bash -c 'echo $$ ; sleep 10000'This will print the newly created bash PID and sleep for a long time. If I go to another terminal and do something like ps -flax | grep leepI'll see something like 501 92418 91910 0 0:00.00 ttys000 0:00.00 bash -c echo $$... (5 Replies)
Discussion started by: teras
5 Replies

5. UNIX for Advanced & Expert Users

How to find all the child processes of a parent process

Hi I am trying to see if there are some options in ps command or if there is a shell script which basically shows you all the processes spawned by a parent process , then all the processes of its child processes and so on down the hierarchy may be like a tree structure. It might be a generic... (6 Replies)
Discussion started by: clifford
6 Replies

6. Programming

Problems with child comunicating with parent on fork()

Hello, I'm trying to implement a version of a bucketSort (kinda) server/client, but I'm having a VERY hard time on making the server behave correctly, when talking to the children, after it forks. The server is kinda big (300+ lines), so I won't post it here, but here's what I'm doing. 1)create a... (8 Replies)
Discussion started by: Zarnick
8 Replies

7. Shell Programming and Scripting

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n",... (1 Reply)
Discussion started by: green_dot
1 Replies

8. Programming

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n", getpid(),... (0 Replies)
Discussion started by: green_dot
0 Replies

9. Shell Programming and Scripting

Parent/Child Processes

Hello. I have a global function name func1() that I am sourcing in from script A. I call the function from script B. Is there a way to find out which script called func1() dynamically so that the func1() can report it in the event there are errors? Thanks (2 Replies)
Discussion started by: yoi2hot4ya
2 Replies

10. UNIX for Dummies Questions & Answers

what are parent and child processes all about?

I don't follow what these are... this is what my text says... "When a process is started, a duplicate of that process is created. This new process is called the child and the process that created it is called the parent. The child process then replaces the copy for the code the parent... (1 Reply)
Discussion started by: xyyz
1 Replies
Login or Register to Ask a Question