Скачиваний:
6
Добавлен:
08.08.2022
Размер:
854 б
Скачать
#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
Соседние файлы в папке Lab7_Oreshchenko
  • #
    08.08.202269 б5compiler2
  • #
    08.08.202218.16 Кб4main
  • #
    08.08.20221.28 Кб6main.cpp
  • #
    08.08.20225.32 Кб4main.o
  • #
    08.08.202218.01 Кб4main2
  • #
    08.08.2022854 б6main2.cpp
  • #
    08.08.20224.5 Кб4main2.o