tags:

views:

337

answers:

1

I have these two functions

    void set_dram_channel_width(int channel_width){
      printf("one\n");
          getchar();
    }


    void set_dram_transaction_granularity(int cacheline_size){
      printf("two\n");
          getchar();
    }
    //output:
    one
    f //my keyboard input
    two
    one
    f  //keyboard input
    two
    one
    f  //keyboard input
    //No more calls

Then I change the functions to:

    void set_dram_channel_width(int channel_width){
      printf("one\n");
    }


    void set_dram_transaction_granularity(int cacheline_size){
      printf("two\n");
      getchar();
    }
    //output
    one
    two 
    f //keyboard input
    //No more calls

Both functions are called by an external code, the code for both programs is the same, just changing the getchar() I get those two different outputs. Is this possible or there is something that is really wrong in my code?

Thanks

This is the output I get with GDB**

For the first code

(gdb) break mem-dram.c:374
Breakpoint 1 at 0x71c810: file build/ALPHA_FS/mem/dramsim/mem-dram.c, line 374.
(gdb) break mem-dram.c:381
Breakpoint 2 at 0x71c7b0: file build/ALPHA_FS/mem/dramsim/mem-dram.c, line 381.
(gdb) run -d ./tmp/MyBench2/ 
one
f
[Switching to Thread 47368811512112 (LWP 17507)]

Breakpoint 1, set_dram_channel_width (channel_width=64)
(gdb) c
Continuing.
two
one
f

Breakpoint 2, set_dram_transaction_granularity (cacheline_size=64)
(gdb) c
Continuing.

Breakpoint 1, set_dram_channel_width (channel_width=8)
374     void set_dram_channel_width(int channel_width){
(gdb) c
Continuing.
two
one
f

For the second code

(gdb) break mem-dram.c:374
Breakpoint 1 at 0x71c7b6: file build/ALPHA_FS/mem/dramsim/mem-dram.c, line 374.
(gdb) break mem-dram.c:380
Breakpoint 2 at 0x71c7f0: file build/ALPHA_FS/mem/dramsim/mem-dram.c, line 380.
(gdb) run
one
two
f
[Switching to Thread 46985688772912 (LWP 17801)]

Breakpoint 1, set_dram_channel_width (channel_width=64)
(gdb) c
Continuing.

Breakpoint 2, set_dram_transaction_granularity (cacheline_size=64)
(gdb) c
Continuing.

Breakpoint 1, set_dram_channel_width (channel_width=8)
(gdb) c
Continuing.
+4  A: 

Since you haven't provided the external code (yet?), here's a guess.

while(some condition) {
    foo1();
    foo2();
}
  • foo1 prints 'one' then waits for some input. You type 'f[enter]'.
  • foo1 consumes the 'f'.
  • foo2 prints 'two' then consumes the [enter] (a newline character).
  • Then you go back to the start, and it all happens again.

With your second version, foo1() doesn't read anything any more.

So:

  • foo1 prints 'one'
  • foo2 prints 'two' then waits for some input. You type 'f[enter]'
  • foo2 consumes the 'f'

The only remaining question is why it stops when it does. To help you with that, we'd have to see what (some condition) actually is.

Note that it's fairly unusual to call getchar() without keeping the result (as in c = getchar();). Do you have a reason for doing this?

One useful C idiom is:

(void) getchar();

The cast to void is an indication from the programmer that they know they're discarding the return value.

slim
This is what I was thinking he was getting at but was waiting around for the code. Without that, this is probably the best/safest guess. Everyone forgets at one time or another that ENTER == '\n'. Taking bets on if "some condition" is != EOF?
Kyle Walsh
Possibly but neither of these functions returns the char that was read. So we have plenty of guesswork to work out where that EOF would get read.
slim
Indeed. This should be interesting!
Kyle Walsh
The thing with the getchar() was just to stop the program. The thing with the one and the two was just to see the order of calling. I posted the gdb output, I hope it helps
Eduardo