Nonblocking IO (ITI8510): erinevus redaktsioonide vahel

Allikas: Lambda
Mine navigeerimisribale Mine otsikasti
Resümee puudub
 
(ei näidata sama kasutaja 20 vahepealset redaktsiooni)
1. rida: 1. rida:
We have two tasks today. Both with practicing nonblocking IO.
{{Iti8510}}
Practice with nonblocking IO.


= 1. Canonical mode =
= 1. Canonical mode =


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


'''Hints:'''
'''Hints:'''
* Try to put the terminal into noncanonical mode. See 'man termios' or [ [http://www.google.ee/search?q=noncanonical+mode Google] for that.
* Try to put the terminal into noncanonical mode and remove terminal echo See '[http://linux.die.net/man/3/termios man termios]' or [http://www.google.ee/search?q=noncanonical+mode Google "noncanonical mode"] for that.
* Note that you should restore the terminal mode before quitting since otherwise the terminal might get messed up (no echo).
* Note that you should restore the terminal mode before quitting since otherwise the terminal might get messed up (no echo).
* Working with bits:
flags &= ~NEWFLAG  // Clears the bit NEWFLAG on flags
flags |= NEWFLAG    // Set the bit NEWFLAG on flags
flags ^= NEWFLAG    // Toggle NEWLAG


= 2. Nonblocking reading from a FIFO =
= 2. Nonblocking reads 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.
Goal: To write a program which prints a '.' character every half-a second unless some input is provided on a fifo file.


'''Hints:'''
'''Hints:'''
*Use open() with O_NONBLOCK flag,  
* Use [http://linux.die.net/man/2/open open()] with O_NONBLOCK flag.
* If [http://linux.die.net/man/2/read read()] does not have anything to read, it returns with -1 and sets [http://linux.die.net/man/3/errno errno] to constant EAGAIN - you can check that or try messing around with [http://linux.die.net/man/3/select select()] function.
* [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 (open two terminal windows).
* It helps to set output to non-buffered mode (the default is line-buffered).
* Write your output with [http://linux.die.net/man/2/write write()] function to bypass buffering.
* Function usleep() can be used for half-a second pause here.
 
[[Kategooria:RTOS]]

Viimane redaktsioon: 15. september 2017, kell 08:41

Real-Time Operating Systems and Systems Programming
[ @ ]

Practice with nonblocking IO.

1. Canonical mode

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

Hints:

  • Try to put the terminal into noncanonical mode and remove terminal echo See 'man termios' or Google "noncanonical mode" for that.
  • Note that you should restore the terminal mode before quitting since otherwise the terminal might get messed up (no echo).
  • Working with bits:
flags &= ~NEWFLAG   // Clears the bit NEWFLAG on flags
flags |= NEWFLAG    // Set the bit NEWFLAG on flags
flags ^= NEWFLAG    // Toggle NEWLAG

2. Nonblocking reads 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.
  • If read() does not have anything to read, it returns with -1 and sets errno to constant EAGAIN - you can check that or try messing around with select() function.
  • FIFO can be made with a 'mkfifo fifoname' command and written to using 'cat > fifoname'.
  • You have to open fifo before reading (open two terminal windows).
  • Write your output with write() function to bypass buffering.
  • Function usleep() can be used for half-a second pause here.