Добавил:
nickolai_oreschenko
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:УТС 4 семестр / Lab7_Oreshchenko / main2
.cpp#include <pthread.h>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
bool bReader = true;
pthread_t thread_reader; // идентификатор потока чтения
int fd; // дескриптор
void* read_fifo(void* arg)
{
int rc;
char buffer[8];
while(bReader){
rc = read(fd, buffer, 7);
if(rc > 0) {
std::cout << buffer << std::endl;
} else if(rc == 0) {
std::cout << "Конец файла" << std::endl;
} else {
perror("Error");
}// if
sleep(1);
}// while
pthread_exit(NULL);
}// read_fifo
int main()
{
mkfifo("/tmp/FIFO", 0644);
fd = open("/tmp/FIFO", O_RDONLY|O_NONBLOCK);
pthread_t thread_read;
pthread_create(&thread_read, NULL, &read_fifo, NULL);
getchar();
bReader = false;
pthread_join(thread_read, NULL);
close(fd);
unlink("/tmp/FIFO");
return 0;
}// main