Linux Multithreading :: C++

This is a discussion on Linux Multithreading :: C++ within the C++ Programming forums, part of the General Programming Boards category; Hi. I have a simple questions for Linux programmers. I am a C++ programmers, but I have no experience working ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Linux Multithreading :: C++

    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

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks.

    I am taking an OS class. The book is really good, but it is a book on OS concepts and does not teach Linux implementation. The professor wants us to use the Linux lab. I have no experience working with multithreading and multiprocessing in Linux. To this day, I am not familiar with the use of fork().

    I would like some advice on working with fork(), especially counting the total number of processes given a sample code like the one above. Here, I will give it another try.

    -----
    int main() // p
    {

    fork(); // p + p

    if (!fork()) // p + p + p
    cout << "a";

    fork(); // p + p + p
    fork(); // p + p + p + p

    if (fork()) // p + p + p + p
    fork(); // p + p + p + p + p + p

    fork(); // p + p + p + p + p + p + p

    return 0;
    -----

    I am not sure how to add the total number of processes.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thinking of upgrading to linux...
    By Yarin in forum General Discussions
    Replies: 37
    Last Post: 07-24-2009, 11:40 AM
  2. Wireless Network Linux & C Testbed
    By james457 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-11-2009, 11:03 AM
  3. Dabbling with Linux.
    By Hunter2 in forum Tech Board
    Replies: 21
    Last Post: 04-21-2005, 04:17 PM
  4. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 04:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21