Unix programming puzzle

Unix programming puzzle

Post by Land Transport Authorit » Wed, 18 Jun 1997 04:00:00



#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void main(void)
{
fork(); printf("hee\n");
fork(); printf("ho\n");

Quote:}

How many lines of output do you expect to see ?
I got it wrong . Need your help to understand this.

Got the inspiration from
John Shapley Gray's InterProcess Communications in Unix.

 
 
 

Unix programming puzzle

Post by Herve Couppe de Lahongrais - CD » Wed, 18 Jun 1997 04:00:00



> #include <stdio.h>
> #include <sys/types.h>
> #include <unistd.h>
> void main(void)
> {
> fork(); printf("hee\n");
> fork(); printf("ho\n");
> }

> How many lines of output do you expect to see ?
> I got it wrong . Need your help to understand this.

> Got the inspiration from
> John Shapley Gray's InterProcess Communications in Unix.

Here is the execution diagram :

process 1
        | fork
        +--------->-------------------------  process 2
        | printf("hee\n")                     | printf("hee\n")
        v                               v
        | fork                          | fork
        +--------->-------  process 4        +-------->--------  process 3
        | printf("ho\n")      | printf("ho\n")      | printf("ho\n")      | printf("ho\n")
        |               |               |               |
        x exit          x exit          x exit          x exit

Output is one of the following, depending of the process scheduling:

        hee     hee     hee
        ho      ho      hee
        ho      hee     ho
        hee     ho      ho
        ho      ho      ho
        ho      ho      ho
--
Herve Couppe de Lahongrais (SEU)       | Eurocontrol Experimental Centre


 
 
 

Unix programming puzzle

Post by Tien Song Chua » Wed, 18 Jun 1997 04:00:00


Sorry,
Your answer is wrong.
I have run it under HP.

 
 
 

Unix programming puzzle

Post by Dennis Taylo » Wed, 18 Jun 1997 04:00:00




Quote:> Sorry,
> Your answer is wrong.
> I have run it under HP.

Sorry. Your answer to his answer is wrong. I ran it under SCO and got:

hee
ho
ho
hee
ho
ho

 
 
 

Unix programming puzzle

Post by James Youngma » Wed, 18 Jun 1997 04:00:00



Quote:> void main(void)

This isn't a valid declaration for main() so the result could be
anything (this isnt a conforming C program).  Read the FAQ for
comp.lang.c for more details.
 
 
 

Unix programming puzzle

Post by Don Re » Wed, 18 Jun 1997 04:00:00


During a brain fsck on Tue, 17 Jun 1997 17:51:39 +0800,  Land Transport

Quote:

>#include <stdio.h>
>#include <sys/types.h>
>#include <unistd.h>
>void main(void)
>{
>fork(); printf("hee\n");
>fork(); printf("ho\n");
>}

>How many lines of output do you expect to see ?
>I got it wrong . Need your help to understand this.

two hee's & four ho's

|__________
|          |
hee        hee
|_______   |_______    
|       |  |       |
ho      ho ho      ho

ho'pe this hee'lp s

--


Calcasieu Lumber Co.                        Austin TX
- If NT is the answer, you don't understand the question

 
 
 

Unix programming puzzle

Post by Robert Nem » Wed, 18 Jun 1997 04:00:00



: #include <stdio.h>
: #include <sys/types.h>
: #include <unistd.h>
: void main(void)
: {
: fork(); printf("hee\n");
: fork(); printf("ho\n");
: }
:
: How many lines of output do you expect to see ?
: I got it wrong . Need your help to understand this.
:
parent prints: hee
      |        ho
      |
      |
      |-- 1st child prints: hee
      |        |            ho
      |        |
      |        |-- 1st child kid prints: ho
      |
      |-- 2nd child prints: ho

 I think this will help you.

--
  GE/CS dx s-:- a-- C++ LU++ P+>++ L+++ E> W++ N++ K- w--- V
  PS+++ PE+ Y+ PGP t 5? X++++ R tv b+ DI? D+ G e h-- r z+>++
  |==================================|

  |==================================|

 
 
 

Unix programming puzzle

Post by Richard Turne » Thu, 19 Jun 1997 04:00:00


Herve Couppe de Lahongrais - CDL wrote ...

Quote:> Here is the execution diagram :

> process 1
>    | fork
>    +--------->-------------------------  process 2
>    | printf("hee\n")                     | printf("hee\n")
>    v                               v
>    | fork                          | fork
>    +--------->-------  process 4        +-------->--------  process 3
>    | printf("ho\n")      | printf("ho\n")      | printf("ho\n")      | printf("ho\n")
>    |               |               |               |
>    x exit          x exit          x exit          x exit

> Output is one of the following, depending of the process scheduling:

>    hee     hee     hee
>    ho      ho      hee
>    ho      hee     ho
>    hee     ho      ho
>    ho      ho      ho
>    ho      ho      ho

What about multiple flushings of the duplicated stdout buffers?

I was under the impression that the child should call _exit() and not
exit()
to guarantee sanity. Isn't this in the FAQ?

Richard.

 
 
 

Unix programming puzzle

Post by Herve Couppe de Lahongrais - CD » Thu, 19 Jun 1997 04:00:00



> Herve Couppe de Lahongrais - CDL wrote ...
> > Here is the execution diagram :

> > process 1
> >       | fork
> >       +--------->-------------------------  process 2
> >       | printf("hee\n")                       | printf("hee\n")
> >       v                               v
> >       | fork                          | fork
> >       +--------->-------  process 4   +-------->--------  process 3
> >       | printf("ho\n")        | printf("ho\n")        | printf("ho\n")        | printf("ho\n")
> >       |               |               |               |
> >       x exit          x exit          x exit          x exit

> > Output is one of the following, depending of the process scheduling:

> >       hee     hee     hee
> >       ho      ho      hee
> >       ho      hee     ho
> >       hee     ho      ho
> >       ho      ho      ho
> >       ho      ho      ho

> What about multiple flushings of the duplicated stdout buffers?

> I was under the impression that the child should call _exit() and not
> exit()
> to guarantee sanity. Isn't this in the FAQ?

> Richard.

In the given example, stdout buffer is flushed before fork because of
the ending new-line character '\n' in the strings "hee" and "ho".
Otherwise, it is needed to fflush(3) the buffer before calling fork(2).

--
Herve Couppe de Lahongrais (SEU)       | Eurocontrol Experimental Centre

 
 
 

Unix programming puzzle

Post by Scott Tompkin » Thu, 19 Jun 1997 04:00:00


warning. dont RUN this on a live machine.

can you spell "c-r-a-s-h-" ?



Quote:> #include <stdio.h>
> #include <sys/types.h>
> #include <unistd.h>
> void main(void)
> {
> fork(); printf("hee\n");
> fork(); printf("ho\n");
> }

> How many lines of output do you expect to see ?
> I got it wrong . Need your help to understand this.

> Got the inspiration from
> John Shapley Gray's InterProcess Communications in Unix.

 
 
 

Unix programming puzzle

Post by Chandrashekhar P. Bhi » Thu, 19 Jun 1997 04:00:00



: #include <stdio.h>
: #include <sys/types.h>
: #include <unistd.h>
: void main(void)
: {
: fork(); printf("hee\n");
: fork(); printf("ho\n");
: }
:
: How many lines of output do you expect to see ?
: I got it wrong . Need your help to understand this.
:

        Hi,
                I ran it under DRS/6000.
                Got the following output :
                hee
                hee
                ho
                ho
                ho
                ho

--
|-------------------------------------------------------------------------|
|  It is hard to think of you as product     (Chandrashekhar P.Bhide)     |

|-------------------------------------------------------------------------|

 
 
 

Unix programming puzzle

Post by r.. » Fri, 20 Jun 1997 04:00:00


Indeed a puzzle,
I ran the program in a loop directing the output to files and each file
was different.
for i in 1 2 3 4 5 6
do
 tstfork > $i
done

some of the files had 2 hee's and 4 ho's as they should have, but some
had only 1 hee and 3 ho's and all other combination


Quote:> #include <stdio.h>
> #include <sys/types.h>
> #include <unistd.h>
> void main(void)
> {
> fork(); printf("hee\n");
> fork(); printf("ho\n");
> }
> How many lines of output do you expect to see ?
> I got it wrong . Need your help to understand this.
> Got the inspiration from
> John Shapley Gray's InterProcess Communications in Unix.

 
 
 

Unix programming puzzle

Post by Casper H.S. Dik - Network Security Engine » Fri, 20 Jun 1997 04:00:00



Quote:>#include <stdio.h>
>#include <sys/types.h>
>#include <unistd.h>
>void main(void)
>{
>fork(); printf("hee\n");
>fork(); printf("ho\n");
>}
>How many lines of output do you expect to see ?
>I got it wrong . Need your help to understand this.

If th e outptu is line buffered, you'd expect 2 hee's and 2 ho's and atleast
one ho before the first he.

But if the output is fully buffered, you'd get 4 hee's as each the
output buffers are flushed multiple times.  (The second fork will
effectively cause a doubling of hee's)
Adding "fflush(stdout)" before each fork() should remedy that.

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.

 
 
 

Unix programming puzzle

Post by Brian McCaule » Fri, 20 Jun 1997 04:00:00



Quote:

> #include <stdio.h>
> #include <sys/types.h>
> #include <unistd.h>
> void main(void)
> {
> fork(); printf("hee\n");
> fork(); printf("ho\n");
> }

> How many lines of output do you expect to see ?

8 (however the above program is not strictly deterministic).

Quote:> I got it wrong . Need your help to understand this.

You didn't fflush(sdtout) after the first printf();

Your process splits into 4.  Each of these processes has built up a
stdout buffer containing "hee\nho\n".  Each will flush this as they
exit.  Since this string is short the outputs from the 4 processes
will not get interleaved.  You'll see:

hee
ho
hee
ho
hee
ho
hee
ho

--

 .  _\\__[oo       from       | Phones: +44 121 471 3789 (home)

.  l___\\    /~~) /~~[  /   [ | PGP-fp: D7 03 2A 4B D8 3A 05 37...
 # ll  l\\  ~~~~ ~   ~ ~    ~ | http://wcl-l.bham.ac.uk/~bam/

 
 
 

1. pick script in The UNIX Programming Environment (The UNIX Programming Environment )

Hi.

In K&P 159p, there is program called pick.
as a exercise Kernighan and Pike said
"write new pick program reading in stdin but argumentes"

that is very difficult for me to write.
what make this exercise complicated is that there is no way to treat
input from stdin like input from arguments.

did anyone solve it?

2. PLIP setup

3. compile C programs with UNIX system calls (= Unix Programs??)

4. Porting Linux to 68020 based mini

5. NEED HELP WITH PROGRAMMING POP3 MAIL PROGRAM TO RETRIEVE MAIL ON UNIX-TO-UNIX

6. Which X server for hp kayaks?

7. informix program command line arg puzzle

8. FTP Problem

9. Here is a puzzle for you in shell programming kindly help please

10. Unix Word Search Puzzle

11. XBomb - A puzzle game for UNIX/X-Windows (like minesweeper).

12. Unix Encrypted Password have me Puzzled...