Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fcntl.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <stdio.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <time.h>
- #include <sys/mman.h>
- #define buf_size 16
- struct argument
- {
- int flag;
- int fd;
- };
- void* func(void* arg)
- {
- printf("Thread enter\n");
- char buf[buf_size];
- while(((argument*)arg)->flag)
- {
- srand(time(0));
- for(int i = 0; i < buf_size; ++i)
- buf[i] = rand() % 26 + 64;
- printf("%s\n", buf);
- write(((argument*)arg)->fd, (void*)buf, buf_size);
- sleep(1);
- }
- printf("Thread closed\n");
- pthread_exit((void*)1030);
- }
- int main()
- {
- const char name[] = "my_fifo";
- int retval;
- argument arg;
- arg.flag = 1;
- pthread_t thread;
- if(!mkfifo(name, 0766))
- printf("channel created\n");
- arg.fd = open(name, O_CREAT | O_WRONLY);
- if(arg.fd != -1)
- printf("channel opened for WR\n");
- if(!pthread_create(&thread, NULL, func, (void*)&arg))
- printf("pthread created\n");
- getchar();
- arg.flag = 0;
- pthread_join(thread, (void**)&retval);
- close(arg.fd);
- unlink(name);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement