Find the answer to your Linux question:
Results 1 to 4 of 4
hi all, How many times does the following program write "hello world"? main() {fork();fork();fork();printf("hello world"); }...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2012
    Posts
    43

    c problem


    hi all,

    How many times does the following program write "hello world"?

    main() {fork();fork();fork();printf("hello world"); }

  2. #2
    Just Joined!
    Join Date
    Apr 2013
    Location
    Gurgaon, India
    Posts
    6
    The answer is 9.

  3. #3
    Just Joined!
    Join Date
    May 2010
    Location
    Madrid
    Posts
    32
    Quote Originally Posted by tintu1212 View Post
    hi all,

    How many times does the following program write "hello world"?

    main() {fork();fork();fork();printf("hello world"); }

    The right answer is 8

    with the first fork there will be 2 processes (father and son)
    with the next fork there will be 4 (each process creates another one -> 2x2 = 4 processes)
    with the next fork there will be 8 (each process creates another one -> 4x2 = 8 processes)

    so at the end there are 8 processes to print hello world

  4. #4
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Quote Originally Posted by occam25 View Post
    The right answer is 8
    Only if (among other things) none of the calls fail in any of the processes...
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •