Erinevus lehekülje "Nonblocking IO (ITS8020)" redaktsioonide vahel
Mine navigeerimisribale
Mine otsikasti
(Uus lehekülg: '{{Its8020}} 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 qui...') |
|||
3. rida: | 3. rida: | ||
Practice with nonblocking IO. | Practice with nonblocking IO. | ||
− | |||
− | Goal: To write a program which | + | = 1. 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:''' | '''Hints:''' | ||
− | * 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. | + | * Try to put the terminal into noncanonical mode (you want to turn off line buffering) 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: | * Working with bits: | ||
15. rida: | 17. rida: | ||
flags ^= NEWFLAG // Toggle NEWLAG | flags ^= NEWFLAG // Toggle NEWLAG | ||
− | |||
− | Goal: To write a program which | + | = 2. Reading from stidin = |
+ | |||
+ | 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:''' |
Viimane redaktsioon: 27. september 2019, kell 08:59
Practice with nonblocking IO.
1. 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:
- Try to put the terminal into noncanonical mode (you want to turn off line buffering) 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. Reading from stidin
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:
- 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 line buffering (or turn it off).
- Function usleep() can be used for half-a second pause here.