the different effect of system call fork()

From: Sino
Date: Wed Apr 08 2009 - 08:53:33 EST


Hi, all

I don't know whether this is the right place for this email, but I
post it here since it concerns the system call of fork.
here is my fwo source code.

//fork1.cc

#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>

using namespace std;

int main(int argc, char **argv)
{
cout << "main process id: "<< getpid() << "; Parent process pid:"
<<getppid() <<endl;
if (pid_t pid = fork() < 0)
{
cout << "Faild to fork child" <<endl;
}
else if (pid == 0)
{
cout << "first level child procss pid: "<< getpid() << "paraent
pid is : "<<getppid()<<endl;
if(pid_t spid = fork() < 0)
{
cout<<"failed to create second leve child process"<<endl;
}
else if (spid == 0)
{
cout<<"second level child process pid is: "<<getpid() <<"
its parent pid is: "<<getppid()<<endl;
while(1){
sleep(5);
}
}
else
{
_exit(0);
}
}else
{
if(waitpid(pid,NULL,0) != pid)
{
cout << "failed to wait process: " << pid <<endl;
}

}
return 0;
}

Here its result after I run this program
main process id: 4831; Parent process pid:2931
first level child procss pid: 4831paraent pid is : 2931
second level child process pid is: 4831 its parent pid is: 2931
first level child procss pid: 4832paraent pid is : 4831
second level child process pid is: 4834 its parent pid is: 4832
second level child process pid is: 4832 its parent pid is: 4831
second level child process pid is: 4833 its parent pid is: 4831


When I run ps -ef |grep fork
here is the output
work 4831 2931 0 20:45 pts/2 00:00:00 ./fork
work 4832 4831 0 20:45 pts/2 00:00:00 ./fork
work 4833 4831 0 20:45 pts/2 00:00:00 ./fork
work 4834 4832 0 20:45 pts/2 00:00:00 ./fork

//fork2.cc
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>

using namespace std;

int main(int argc, char **argv)
{
cout << "main process id: "<< getpid() << "; Parent process pid:
" <<getppid() <<endl;
pid_t pid = fork();
if (pid < 0)
{
cout << "Faild to fork child" <<endl;
}
else if (pid == 0)
{
cout << "first level child procss pid: "<< getpid() << ";
paraent pid is : "<<getppid()<<endl;
pid_t spid = fork();
if(spid < 0)
{
cout<<"failed to create second leve child process"<<endl;
}
else if (spid == 0)
{
cout<<"second level child process pid is: "<<getpid() <<";
its parent pid is: "<<getppid()<<endl;

while(1){
sleep(5);
}
}
else
{
_exit(0);
}
}else
{
if(waitpid(pid,NULL,0) != pid)
{
cout << "failed to wait process: " << pid <<endl;
}

}
return 0;
}

the result for the same command are here
./fork
main process id: 4867; Parent process pid:2931
first level child procss pid: 4868paraent pid is : 4867
second level child process pid is: 4869 its parent pid is: 4868

for ps -ef |grep fork

work 4869 1 0 20:50 pts/2 00:00:00 ./fork

The results are very different. For my understanding the results
should be same. but they are not. Why??

Appreciation for your kindly help.

Please let me know where I should post it if this is not the right.

Thanks very much

BRs
Zongjun
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/