Skip to contentSkip to conversation

the different effect of system call fork()

DISCUSSION Sino 3 participants 3 messages Apr 8, 2009 – Apr 9, 2009

lore
Share & tools
lore

Sino

author
#0 lore raw

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

#1 lore raw

Hi!

On Wed, 2009-04-08 at 20:53 +0800, Sino wrote:
[...]

Quoted 1 line

I don't know whether this is the right place for this email, but I

It's probably not.

Quoted 4 lines

post it here since it concerns the system call of fork. here is my fwo source code.

//fork1.cc

[...]

Quoted 1 line
   if (pid_t pid = fork() < 0)

I don't think that this line does what you want it to do.
Hint: Using a "pid_t" for the boolean result for a comparison seems strange at best.

Bernd

Signature · 3 lines
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services
#2 lore raw
Quoted 5 lines · Bernd Petrovitsch
Quoted 1 line
   if (pid_t pid = fork() < 0)

I don't think that this line does what you want it to do.
Hint: Using a "pid_t" for the boolean result for a comparison seems strange at best.

The "problem" is due to a misunderstanding of C/C++ operator precedence.

"(pid = fork() < 0)" binds like "pid = (fork() < 0)" and so pid always has a value of zero and so both parent and child process run the "first level child process" code ... soon there are 4 processes all confused about who they are.

Nick.