Results 1 to 3 of 3
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
int p1, p2, p3;
p1 = fork(); //first child
if(p1<0)
{
printf("fork failed");
return 1;
}
if(p1==0)
{
printf("First child\n");
printf("my ...
- 11-09-2007 #1Just Joined!
- Join Date
- Nov 2007
- Posts
- 2
Need Help!
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
int p1, p2, p3;
p1 = fork(); //first child
if(p1<0)
{
printf("fork failed");
return 1;
}
if(p1==0)
{
printf("First child\n");
printf("my parent is %ld \n",getppid());
printf(" am %ld \n", getpid());
p2=fork(); //second child
if(p2<0)
{
printf("fork failed");
}
if(p2==0)
{
printf(" my parent is %ld\n",getppid() );
printf(" my id is %ld\n", getpid());
printf(" second child\n\n");
}
p3 =fork(); \\third child
if(p3<0)
{
printf("fork failed ");
}
if(p3==0)
{
printf(" third child\n");
printf(" my parent is %ld\n ", getppid());
printf(" my id is %ld \n\n", getpid());
}
exit(0);
}
in this code third child is executing two times and am not understanding y is this happening.
so if anybody can help me plz do so....
exit(0);
return 0;
}
- 11-09-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Place an exit() statement after printing your messages in the childs.
And .... please place your code within code brackets because it's almost unreadable.
Regards
- 11-09-2007 #3Just Joined!
- Join Date
- Nov 2007
- Posts
- 2
thnx a lot......it is done...
i'll take care of the other thing also


Reply With Quote