simple fork question


 
Thread Tools Search this Thread
Top Forums Programming simple fork question
# 1  
Old 02-09-2002
simple fork question

When executing this simple program:

#include <unistd.h>
void main()
{
int f;
printf("\n Parent procces ID=%d\n",getpid());
f=fork();
if(f==0)
{
printf("\n Child process ID=%d father=%d\n",getpid(),getppid());
}
exit(0);
}


I get this result:

#BadCommand in ~>flag2
#
# Parent procces ID=8297
#
# Child process ID=8298 father=1
#BadCommand in ~>

So, my question is: Why is father=1 when it should be 8297 ???

If I insert the command
sleep(2)
just before exit(0) then the 'father' is displayed correctly.
Why is that? Can anyone help me out?
# 2  
Old 02-09-2002
When a process exits, a lot of stuff happens. One thing is that the kernel scans the process table to see if the exiting process has any children. If so, those children get a new parent. The new parent is always init which has a pid of 1.

The ppid is the current parent, not the, um, biological parent.

If you want reliably get the pid of the process that actually issued the fork, record it before the fork. But it isn't useful for anything, except maybe logging.
# 3  
Old 02-19-2002
try running this code...

#include <unistd.h>
void main()
{
int f;
printf("\n Parent procces ID=%d\n",getpid());
f=fork();
if(f==0)
{
printf("\n Child process ID=%d father=%d\n",getpid(),getppid());
}
else
{
wait(0);
}
exit(0);
}


Here's the output:

./a.out

Parent procces ID=27711

Child process ID=27712 father=27711

The problem in the previous code is that the parent does not wait for the termination of the child process....Hence sometimes the parent process dies before the child ..Hence the init process(pid=1) becomes the parent of the child process...

By adding wait() or waitpid() we ensure that the parent waits for the termination of the child...


Pls. correct me if I am wrong....!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

2. Programming

Linux fork, execv, wait question

Hi All, I have a program for class that needs to do the following: 1. Print the directory entries from the current directory using ncurses 2. Provide a prompt next to each directory entry and allow the user to enter commands that may or may not be about the file 3. Execute those commands in... (1 Reply)
Discussion started by: afulldevnull
1 Replies

3. Programming

question about fork

i'm experimenting fork function and i found this code #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <wait.h> #include <fcntl.h> #include <unistd.h> int main(void) { int fd; pid_t p; p = fork(); fork(); if (p>0) { fork();} fork(); fork();... (6 Replies)
Discussion started by: blob84
6 Replies

4. Programming

Newbie question on exec,fork, wait,pipe C

Hello everybody.I want to make clear that i am not going to ask from anybody to build my asignement but i have a big problem. I can't seem to find anywhere ONE good example on C about what i am trying to do:wall:.I think it is simple. All i ask is one example, even a link is fine. So, i want to... (1 Reply)
Discussion started by: Cuervo
1 Replies

5. Programming

multiple fork() question

I writing a program that forks three times but only on the parent process. The three children processes then produces output in order. 1, 2, 3. I am confused on how to do this. I have tried multiple if and else if statements but the output does not come out right. How should I go about doing this? (1 Reply)
Discussion started by: TWhitt24
1 Replies

6. UNIX for Dummies Questions & Answers

simple fork() problem

I have this little program ... int main(void){ printf("Before"); fork(); printf("After"); } output is this..... BeforeAfterBeforeAfter Shouldnt it be.....BeforeAfterAfter After parent is forked child receives the copy of program and continues from next statement... (3 Replies)
Discussion started by: joker40
3 Replies

7. Programming

Beginners question about fork

Hi everyone: I'm developing a dynamic library for notifications, this library is used for a daemon that i've programmed, when something goes wrong the library should send an email to an administrator, but since sending an email is a non-vital process then it can fail (it should work as an... (4 Replies)
Discussion started by: edgarvm
4 Replies

8. Programming

Question About Multi-Processed Applications... fork()

Assume we have an application built on *nix that uses fork()...then the processes procedure is going to act as follow: X is considered a parent process (first click on application) Y is considered a child process of X (second click on application) Z is considered a child process of Y (third... (6 Replies)
Discussion started by: f.ben.isaac
6 Replies

9. Programming

A small question about fork()

Hello experts, I am using fork() in my code but I am confused which output comes first child or parent? I did the following code .My book shows parent first but my linux shows child first.Can anyone tell me why? #include <stdio.h> int main(){ int pid; printf("I am original process with pid... (5 Replies)
Discussion started by: mlhazan
5 Replies

10. UNIX for Dummies Questions & Answers

Ok simple question for simple knowledge...

Ok what is BSD exactly? I know its a type of open source but what is it exactly? (1 Reply)
Discussion started by: Corrail
1 Replies
Login or Register to Ask a Question