LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-02-2004, 09:44 AM   #1
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Rep: Reputation: 15
fork() problem


watch in this: I want to code a stupid program tha fork 1 child, in this i fork other 2 child that execute pwd and id, and in the end after i wait the 3 child and subchild print the pid of th PARENTS.But i have an error that not understand, why at last i receive also the pid of the first child if i ask only the ppid.Tnx in advance : )

#define PWD "/bin/pwd"
#define ID "/usr/bin/id"

int
main(void)
{
pid_t pid_1, pid_2, pid_3;
int status_1, status_2, status_3;
char *en[1];
char *in[2];

pid_1 = fork();
if (pid_1 == 0) {
fprintf(stdout,"First child with pid :: %i", getpid());

en[0] = ID;
pid_2 = fork();
if (pid_2 == 0) {
fprintf("Second child of child with pid :: %i, and parent pid :: %i", getpid(), getppid());
execv(en[0], en);
exit(0);
}

in[0] = PWD;
pid_2 = fork();
if (pid_2 == 0) {
fprintf("Third child of child with pid :: %i, and parent pid :: %i", getpid(), getppid());
execv(en[0], en);
exit(0);
}
wait(&status_3);
wait(&status_2);
}
wait(&_status1);
fprintf(stdout,"We are in parent with pid :: %i", getpid());
exit( EXIT_SUCCESS );
}

0k n0w if i run obatin this ::

First child with pid :: 1573 //ok
Second child of child with pid :: 1574 and parent pid :: 1573 //ok
/root/System //ok
Second child of child with pid :: 1575 and parent pid :: 1573 //ok
uid=0(root)...........groups=0(root) //ok
We are in parent with pid :: 1573 <- ouCh :/ why i obtain this ???
We are in parent with pid :: 1572 //ok
 
Old 04-02-2004, 11:42 AM   #2
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Hi there,

I think I know what's going on here.
When you call fork(), both processes (the original one and the newly-created child) begin execution of the rest of the code from the fork() command. And, since you're not guarding your if statements according to which process you want to execute them, you've still got 2 processes left to execute that last if statement. Try it like this:

Code:
#define PWD "/bin/pwd"
#define ID "/usr/bin/id"

int
main(void)
{
    pid_t pid_1, pid_2, pid_3;
    int status_1, status_2, status_3;
    char *en[1];
    char *in[2];

    pid_1 = fork();
    if (pid_1 == 0)  //child 1 will execute this
    { 
        fprintf(stdout,"First child with pid :: %i", getpid());
    
        en[0] = ID;
        pid_2 = fork(); //first "grandchild" will execute this
        if (pid_2 == 0) 
        {
            fprintf("Second child of child with pid :: %i, and parent pid :: %i",     getpid(),getppid());
            execv(en[0], en);
            exit(0);  //kills off grandchild
        }

        in[0] = PWD;
        pid_2 = fork();
        if (pid_2 == 0) 
        {
             fprintf("Third child of child with pid :: %i, and parent pid :: %i", getpid(), getppid());
             execv(en[0], en);
             exit(0);
          }
          wait(&status_3);  //waits for the first grandchild
          wait(&status_2);  //waits for the second grandchild
    }

    //There's no if statement guarding this block - therefore, both the parent process and the first child will execute it.
    wait(&_status1);
    fprintf(stdout,"We are in parent with pid :: %i", getpid());
    exit( EXIT_SUCCESS );
} //ends main

/*-----------------------------------------------------------------
Try: 
....
wait(&status_3);  //waits for the first grandchild
wait(&status_2);  //waits for the second grandchild
....
else if(pid_1 != 0)
    {
       wait(&status_1);  //waits for the first child
       fprintf(stdout, "We are in parent with pid:: %i", getpid());
       exit(EXIT_SUCCESS);
    } //ends if
} //ends main

--------------------------------------------------------------------/*
The best advice, when dealing with fork() and processes, is ALWAYS put EVERYTHING inside if statements. Specifically say which process you want doing things, even if you want both parent and child doing the same thing. It makes things much more readable, not to mention eliminating errors.

Post back if it's still being weird

Last edited by rose_bud4201; 04-02-2004 at 11:54 AM.
 
Old 04-02-2004, 05:05 PM   #3
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Original Poster
Rep: Reputation: 15
PERFECT PERFECT PERFECT : ) ) tnx & greetings
 
Old 04-02-2004, 05:20 PM   #4
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Excellent ^_^ You're quite welcome.

Glad it turned out alright,
Laura
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fork problem with new compieled kernel janux Linux - Software 2 11-27-2004 07:22 PM
Fork() problem MrBrain Programming 3 10-10-2004 04:52 PM
problem of using fork() in QT Designer sooraj_pathe Linux - Software 1 03-11-2004 11:04 AM
problem of using fork() in QT Designer sooraj_pathe Linux - Software 1 03-11-2004 10:47 AM
Fork problem Rajneesh Programming 3 12-22-2003 06:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration