forks and children

This is a discussion on forks and children within the C Programming forums, part of the General Programming Boards category; hello, I created 5 forks from the parent process using a recusive function. This is the output: I am the ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 11:41 AM
  2. Linux Putting my children to sleep with futex!?
    By Abs in forum Linux Programming
    Replies: 18
    Last Post: 02-12-2009, 05:43 PM
  3. Forks v's threads
    By clancyPC in forum C Programming
    Replies: 7
    Last Post: 11-11-2005, 05:29 AM

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