Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Linux Multithreading :: C++

kuphrynkuphryn Member Posts: 266
Hi.

I have a simple questions for Linux programmers. I am a C++ programmers, but I have no experience working with multithreaded programming in Linux. I would like to understand fork().

For example, how many processes including main process does the following code produces?



code:--------------------------------------------------------------------------------
// p -> one child process

int main()
{
fork(); // p
fork(); // p
fork(); // p
fork(); // p

return 0;
--------------------------------------------------------------------------------


The code above will spawn a total of five processes including main. Is that accurate?

Secondly, I would like to know let say when you spawn a process using fork() I know the child-process will execute at that very instance and all the code after it. What if you spawn one child-process after another, does the total number of processes increases exponantially?

Lastly, if you spawn a child-process inside an if statement, will the child-process execute code outside of the if statement?


code:--------------------------------------------------------------------------------

int main()
{
fork();

// If child-process, then execute.
// fork() returns 0 for the child-process.

if (!forker()_
{
...
}

cout << "will the child-process execute this line?";

return 0;
}
--------------------------------------------------------------------------------


Thanks,
Kuphryn

Comments

  • button999button999 Member Posts: 1
    1. First Question....
    if you run below code, maybe total count of processes is above 5.
    main()
    {
    fork(); fork(); fork();
    }
    you must write like below...

    int main()
    {
    switch(fork())
    {
    case -1: /* fork error */
    break;
    case 0: /* child */
    ... child process routine...
    default: /* parent */
    ... parent process routine...
    }
    }

    2. I don't understand your question.

    3. If you understand above my code, you will solve your 3rd question.


    Comments:
    You should use pthread_create() than fork() if you want to multi-threaded programming.

  • abcabc Member Posts: 443
    : For example, how many processes including main process does the following code produces?
    :
    [code]
    : // p -> one child process
    :
    : int main()
    : {
    : fork(); // p
    : fork(); // p
    : fork(); // p
    : fork(); // p
    :
    : return 0;
    :
    [/code]
    :
    : The code above will spawn a total of five processes including main. Is that accurate?

    no, the answer is 16 - after the first fork() you have 1 parent and 1 child, after the second fork() each of them spawns one child (i.e. 4 processes), etc. thus each fork() doubles the number of processes.

    :
    : Secondly, I would like to know let say when you spawn a process using fork() I know the child-process will execute at that very instance and all the code after it. What if you spawn one child-process after another, does the total number of processes increases exponantially?

    yes - see the previous answer
    :
    : Lastly, if you spawn a child-process inside an if statement, will the child-process execute code outside of the if statement?
    :
    yes - the child process will be an exact copy of the parent w.r.t the code and data areas - thus it will just continue on executing the code as if it were the parent. The only way to get different code to execute is by using an if statement that checks the return value of fork():

    [code]
    ...
    if(fork() )
    {
    /* non-zero means this is the parent
    * and the returned value is the child's pid */
    printf("parent here
    ");
    }
    else
    {
    */ in the child process fork() returns zero */
    printf("child here");
    }

    /* from here on [b]all[/b] the code is executed in both processes */

    [/code]
  • kuphrynkuphryn Member Posts: 266
    Okay. Thanks.

    Kuphryn
  • Chris BrownChris Brown USAMember Posts: 4,503 ✭✭

    _______ [ http://forcoder.org ] free ebooks and video tutorials about ( Delphi Objective-C PHP MATLAB PL/SQL Python Go C# C++ Swift Perl JavaScript Java Ruby Visual Basic Visual Basic .NET Scratch C R Assembly Erlang Fortran Rust Julia ML Prolog Logo F# Ada Lua Bash FoxPro Awk Hack COBOL Lisp Scala SAS Clojure D Dart Apex Transact-SQL Crystal Kotlin Alice ABAP LabVIEW VBScript Scheme ) __________

Sign In or Register to comment.