Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define _POSIX_SOURCE
#include <termios.h>
int tcflush(int fildes, int where);
If tcflush() is called from a background process group against the caller's controlling terminal, a SIGTTOU signal may be generated depending how the process is handling SIGTTOUs:
Processing for SIGTTOU | System Behavior |
---|---|
Default or signal handler | The SIGTTOU signal is generated, and the function is not performed. tcflush() returns -1 and sets errno to EINTR. |
Ignored or blocked | The SIGTTOU signal is not sent, and the function continues normally. |
If successful, tcflush() returns 0.
⁄* CELEBT05
This example flushes a string.
*⁄
#define _POSIX_SOURCE
#include <termios.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys⁄stat.h>
main() {
char Master[]="master.tty";
char Slave[]="slave.tty";
char text1[]="string that will be flushed from buffer";
char text2[]="string that will not be flushed from buffer";
char data[80];
int master, slave;
if (mknod(Master, S_IFCHR|S_IRUSR|S_IWUSR, 0x00010000 + 10) != 0)
perror("mknod() error for master tty");
else {
if (mknod(Slave, S_IFCHR|S_IRUSR|S_IWUSR, 0x00020000 + 10) != 0)
perror("mknod() error for slave tty");
else {
if ((master = open(Master, O_RDWR|O_NONBLOCK)) < 0)
perror("open() error for master tty");
else {
if ((slave = open(Slave, O_RDWR|O_NONBLOCK)) < 0)
perror("open() error for slave tty");
else {
if (write(slave, text1, strlen(text1)+1) == -1)
perror("write() error");
else if (tcflush(slave, TCOFLUSH) != 0)
perror("tcflush() error");
else {
puts("first string is written and tty flushed");
puts("now writing string that will not be flushed");
if (write(slave, text2, strlen(text2)+1) == -1)
perror("write() error");
else if (read(master, data, sizeof(data)) == -1)
perror("read() error");
else printf("read '%s' from the tty\n", data);
}
close(slave);
}
close(master);
}
unlink(Slave);
}
unlink(Master);
}
}
first string is written and tty flushed
now writing string that will not be flushed
read 'string that will not be flushed from buffer' from the tty