Nonblocking IO (ITI8510): erinevus redaktsioonide vahel

Allikas: Lambda
Mine navigeerimisribale Mine otsikasti
14. rida: 14. rida:


'''Hints:'''
'''Hints:'''
*Use open() with O_NONBLOCK flag,  
*Use [http://linux.die.net/man/2/open open()] with O_NONBLOCK flag,  
* [http://en.wikipedia.org/wiki/Named_pipe FIFO] can be made with a 'mkfifo fifoname' command and written to using 'cat > fifoname'.
* [http://en.wikipedia.org/wiki/Named_pipe FIFO] can be made with a 'mkfifo fifoname' command and written to using 'cat > fifoname'.
* You have to open fifo ''before'' reading (use two terminals).
* You have to open fifo ''before'' reading (use two terminals).
* It helps to set output to non-buffered mode (the default is line-buffered).
* It helps to set output to non-buffered mode (the default is line-buffered).

Redaktsioon: 18. veebruar 2010, kell 17:39

We have two tasks today. Both with practicing nonblocking IO.

1. Canonical mode

Goal: To write a program which echoes characters on screen while typed until button 'q' is pressed. Then prints a newline.

Hints:

  • Try to put the terminal into noncanonical mode. See 'man termios' or [ Google for that.
  • Note that you should restore the terminal mode before quitting since otherwise the terminal might get messed up (no echo).

2. Nonblocking reading from a FIFO

Goal: To write a program which prints a '.' character every half-a second unless some input is provided on a fifo file.

Hints:

  • Use open() with O_NONBLOCK flag,
  • FIFO can be made with a 'mkfifo fifoname' command and written to using 'cat > fifoname'.
  • You have to open fifo before reading (use two terminals).
  • It helps to set output to non-buffered mode (the default is line-buffered).