Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <pthread.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <time.h>
- using namespace std;
- typedef struct Arguments {
- bool* flag;
- timespec* tm;
- int* fd;
- } arguments;
- void *thread(void *args){
- puts("read thread started");
- arguments *argument = (arguments*) args;
- (*(argument->tm)).tv_sec = 0;
- (*(argument->tm)).tv_nsec = 100000000;
- char buf[100];
- while (*(argument->flag)) {
- while (read(*(argument->fd), buf, 100) <= 0) {
- if (!*(argument->flag))
- return 0;
- nanosleep(argument->tm, NULL);
- }
- printf("Readed: %s\n", buf);
- fflush(stdout);
- sleep(1);
- }
- printf("write thread finished\n");
- fflush(stdout);
- pthread_exit(NULL);
- }
- int main() {
- char fifo_name[] = "current_fifo_name";
- pthread_t threadInit;
- bool flag = true;
- arguments args;
- timespec tm;
- int fd;
- unlink(fifo_name);
- mkfifo(fifo_name, 0777);
- fd = open(fifo_name, O_RDONLY | O_NONBLOCK);
- args.flag = &flag;
- args.fd = &fd;
- args.tm = &tm;
- if(pthread_create(&threadInit, NULL, &thread, (void*)&args))
- return EXIT_FAILURE;
- getchar();
- flag = false;
- pthread_join(threadInit, NULL);
- close(fd);
- unlink(fifo_name);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment