Thread: forks and children

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    1

    forks and children

    hello,

    I created 5 forks from the parent process using a recusive function.

    This is the output:
    I am the parent of all and my process ID is 371
    My process ID is 374
    Children ID 374 finish printing, time to sleep...
    My process ID is 375
    Children ID 375 finish printing, time to sleep...
    My process ID is 377
    Children ID 377 finish printing, time to sleep...
    My process ID is 378
    Parent ID 371 has finish creating forks, so time to sleep...
    Children ID 378 finish printing, time to sleep...
    My process ID is 380
    Children ID 380 finish printing, time to sleep...

    Why did the parent ID 371 started to sleep even before children ID 380 prints its line? I wanted the program to have the parent forks 5 times and each child will print a line.

    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,659
    Well as soon as you fork() a new process, all syncronisation between parent and child is lost - basically, anything can happen in any order.

    So if you want specific things to happen in a specific order, then you need some form of inter-process communication (say a message or a semaphore) to make it happen in the way that you want.

    Oh, and next time, post some code. My program produces "this output" is not a great deal of use to anyone.
    Please use [code][/code]Tags
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple forks in one client connection (ftpclient)
    By Dynamo in forum Networking/Device Communication
    Replies: 5
    Last Post: 01-16-2011, 12:41 PM
  2. Linux Putting my children to sleep with futex!?
    By Abs in forum Linux Programming
    Replies: 18
    Last Post: 02-12-2009, 06:43 PM
  3. Forks v's threads
    By clancyPC in forum C Programming
    Replies: 7
    Last Post: 11-11-2005, 06:29 AM