4 Replies - 3975 Views - Last Post: 13 March 2010 - 06:26 AM Rate Topic: -----

#1 DamienCurr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 03-May 09

Parent process opens two child processes

Posted 12 March 2010 - 07:31 PM

Evening!

Okay. This time out, I'm trying to run two child processes from one parent process. Each prints its pid. Here is the code:


#include <stdio.h>
int main()
{
int pid, i;
pid = vfork();
switch(pid)
{
case -1: /* an error occurred */
printf("Fork error");
break;
case 0: /* this code is executed by child process #1*/
for(i=1; i<10; i++)
printf("I am child C1, my pid is: %d\n", getpid());
sleep(2);

case 1: /* this code is executed by child process #2*/
for(i=1; i<10; i++)
printf("I am child C2, my pid is: %d\n", getpid());
exit(0);

default: /* this code is executed by the parent process */
for(i=1; i<10; i++)
printf("I am parent P, my pid is: %d\n", getpid());
}
}




When I run it, I get:

I am child C1, my pid is: 13796
I am child C1, my pid is: 13796
I am child C1, my pid is: 13796
I am child C1, my pid is: 13796
I am child C1, my pid is: 13796
I am child C1, my pid is: 13796
I am child C1, my pid is: 13796
I am child C1, my pid is: 13796
I am child C1, my pid is: 13796
I am child C2, my pid is: 13796
I am child C2, my pid is: 13796
I am child C2, my pid is: 13796
I am child C2, my pid is: 13796
I am child C2, my pid is: 13796
I am child C2, my pid is: 13796
I am child C2, my pid is: 13796
I am child C2, my pid is: 13796
I am child C2, my pid is: 13796
I am parent p, my pid is: 13795
I am parent p, my pid is: 13795
I am parent p, my pid is: 13795
I am parent p, my pid is: 13795
I am parent p, my pid is: 13795
I am parent p, my pid is: 13795
I am parent p, my pid is: 13795
I am parent p, my pid is: 13795
I am parent p, my pid is: 13795

So, it appears to be working except that child processes #1 and #2 seem to have the same pid. Is this correct?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Parent process opens two child processes

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Parent process opens two child processes

Posted 12 March 2010 - 07:43 PM

No, they should have unique pid's. There is a missing break between case 1 and case 2. I don't think that will help your understanding of the problem. Fix that and try again.
Was This Post Helpful? 1
  • +
  • -

#3 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Parent process opens two child processes

Posted 12 March 2010 - 11:50 PM

not sure why you have the loopswhich just prints the same message 10 times.

also you should fork() twice in the parent to create two child processes. try
#include <stdio.h>
int main()
{
int pid, i;
pid = vfork();
switch(pid)
{
case -1: /* an error occurred */
printf("Fork error");
break;
case 0: /* this code is executed by child process #1*/
//for(i=1; i<10; i++)
printf("I am child C1, my pid is: %d\n", getpid());
sleep(2);

//case 1: /* this code is executed by child process #2*/
//for(i=1; i<10; i++)
exit(0);

default: /* this code is executed by the parent process */
//for(i=1; i<10; i++)
printf("I am parent P, my pid is: %d\n", getpid());
if(fork()==0)
   printf("I am child C2, my pid is: %d\n", getpid());

}
}

gives
I am parent P, my pid is: 1940
I am child C2, my pid is: 3152
I am child C1, my pid is: 3044


Was This Post Helpful? 0
  • +
  • -

#4 DamienCurr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 03-May 09

Re: Parent process opens two child processes

Posted 13 March 2010 - 05:24 AM

Thanks everyone who posted a reply to my question. Horace your suggestion looks great, but has the parent process run before child process 2. What I would like to do is run child 1, child 2 and then parent. I tried moving some code around to get child 2 to run before the parent, but it did not work out quite like I wanted.

As it stands, my code is this:


#include <stdio.h>
int main()
{
 int pid, i;
 pid = vfork();
 switch(pid)
 {
           
           case -1: /* an error occurred */
           printf("Fork error");
           break;

           case 0: /* this code is executed by child process #1*/
           printf("I am child C1, my pid is: %d\n", getpid());
           sleep(2);
           exit(0);

           //case 1:
           if(fork()==0)
           printf("I am child C2, my pid is: %d\n", getpid());
           sleep(2);
           exit(0);
           
           default: /* this code is executed by the parent process */
           printf("I am parent P, my pid is: %d\n", getpid());
           //if(fork()==0)
           //printf("I am child C2, my pid is: %d\n", getpid());

  }
}




When the executable is run, however, I get this:

I am child C1, my pid is: 5481
I am parent p, my pid is: 5480

So, child 2 does not run. What am I missing?

Thanks in advance.

Was This Post Helpful? 0
  • +
  • -

#5 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Parent process opens two child processes

Posted 13 March 2010 - 06:26 AM

View PostDamienCurr, on 13 March 2010 - 11:24 AM, said:

Thanks everyone who posted a reply to my question. Horace your suggestion looks great, but has the parent process run before child process 2. What I would like to do is run child 1, child 2 and then parent. I tried moving some code around to get child 2 to run before the parent, but it did not work out quite like I wanted.

[font="Arial"]When the executable is run, however, I get this:

I am child C1, my pid is: 5481
I am parent p, my pid is: 5480

So, child 2 does not run. What am I missing?

Thanks in advance.

you are running child 2 in the child 1 process and you exit() before the fork() is called

if you want to make sure of the order of execution use wait() or waitpid(), e.g.

#include <stdio.h>
int main()
{
int pid, i;
pid = vfork();
switch(pid)
{
case -1: /* an error occurred */
printf("Fork error");
break;
case 0: /* this code is executed by child process #1*/
//for(i=1; i<10; i++)
printf("I am child C1, my pid is: %d\n", getpid());
sleep(2);

//case 1: /* this code is executed by child process #2*/
//for(i=1; i<10; i++)
exit(0);

default: /* this code is executed by the parent process */
wait();
if(fork()==0)
   {
   printf("I am child C2, my pid is: %d\n", getpid());
   exit(0);
   }
wait();
//for(i=1; i<10; i++)
printf("I am parent P, my pid is: %d\n", getpid());

}
}


gives
I am child C1, my pid is: 4088
I am child C2, my pid is: 5352
I am parent P, my pid is: 5424



This post has been edited by horace: 13 March 2010 - 06:26 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1