Thread: process execution

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    13

    process execution

    i ran the following code and got strange results....


    Code:
      int main(int argc, char *argv[] )
     {
    	int i =0;
    	
    	for(i=0;i<2;i++)
    	{
    		printf("%d child proc b4 %d\n",i, (int) getpid());
    
    		printf(" parent proc %d\n", (int) getppid());
    		fork();
    
    		printf("%d child proc af %d\n\n",i, (int) getpid());
    		
    	}
    	
    	
    	return EXIT_SUCCESS;
       }

    the results i got


    0 child proc b4 16544 <-------------
    parent proc 16027
    0 child proc af 16544

    1 child proc b4 16544
    parent proc 16027
    1 child proc af 16544 <-------------- does this indicate that the fork(); call takes longer
    ------------------------------------------------- than the loop to output to stdout ??


    0 child proc af 16545

    1 child proc b4 16545
    parent proc 16544 <---------------- does this mean that the parent of this fork is the child
    ------------------------------------------------ of the first parent
    1 child proc af 16546

    1 child proc af 16545 <--------------- ?????? and is this that the process somehow calls
    ------------------------------------------------ the print statement again ??? or stdout ???

    1 child proc af 16547


    really confused, would appreciate any insight ....

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you print the result of getpid() and getppid() in a single printf call, then you should be able to draw a family tree.
    Also printing the value of i as well would help.

    And yes, you have grand-children in this code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    If you are confused, then take a look at the man pages. And you'll never see this kind of use of fork in a real program, so you shouldn't care much if the output seem confusing to you. What you'll typically see is

    Code:
    int main()
    {
       if (fork() == 0)
       {
          // You are in child process
          // Do something, like calling exec
       }
    
       else
       {
          // You are in the parent process
       }
    
       return 0;
    }
    Now, if you do not understand this kind of program, then you have a problem.
    I hate real numbers.

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by foxman View Post
    If you are confused, then take a look at the man pages. And you'll never see this kind of use of fork in a real program, so you shouldn't care much if the output seem confusing to you. What you'll typically see is

    Code:
    int main()
    {
       if (fork() == 0)
       {
          // You are in child process
          // Do something, like calling exec
       }
    
       else
       {
          // You are in the parent process
       }
    
       return 0;
    }
    Now, if you do not understand this kind of program, then you have a problem.
    I dont understand threading in C at all. Do the if and else blocks run concurrently in seperate threads?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    They run concurrently in separate processes.

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    oooh ok processes. Any good standard text or tutorials I should look over for c/c++ threading? It's so easy to do in c# i feel like im cheating.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    For threads, check out pthreads.

  8. #8
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Thankyou very much. Can these be used in the *nix and windows world, as well as with C/C++ ?

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes. There should be a Windows version of it that you can download somewhere. Cygwin is also an option for Windows.

  10. #10
    Registered User
    Join Date
    Aug 2008
    Posts
    13
    thanks for the replies !!

    and i do understand the fork code above...... and thanks to another post i think i have an understanding of the processes now, i guess the thing that was throwing me was that the without the use of wait() and similar functions that they are running independently of main and the functions i was using ... hence the initial code (above )to try and understand how they run....
    basically without the use of the wait()... and similar functions there is no way to control the flow of execution ... its all handled by the os....

    thanks again for the insights

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. What is the best way to record a process execution time?
    By hanash in forum Linux Programming
    Replies: 7
    Last Post: 03-15-2006, 07:17 AM