Advertisement
EternalHaru

Untitled

May 28th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <iostream>
  6. #include <fcntl.h>
  7. #include <cstring>
  8.  
  9. using namespace std;
  10.  
  11. typedef struct Arguments {
  12.     bool* flag;
  13.     int *fd;
  14. } arguments;
  15.  
  16.  
  17. void* threadOne(void* args){
  18.     arguments *argument = (arguments*) args;
  19.     void* counter = malloc(sizeof(int));
  20.     *(int*)counter = 0;
  21.     puts("threadOne started");
  22.     char string[] = "LETI 2018 OS";
  23.     int y;
  24.     while (*(argument->flag)) {
  25.         write(argument->fd[1], string, (strlen(string)+1));
  26.         printf("Writed\n");
  27.         sleep(3);
  28.         (*(int*)counter)++;
  29.     }
  30.     puts("threadOne finished");
  31.     pthread_exit(counter);
  32. }
  33.  
  34.  
  35. void* threadTwo(void* args){
  36.     arguments *argument = (arguments*) args;
  37.     void* counter = malloc(sizeof(int));
  38.     *(int*)counter = 0;
  39.     puts("threadTwo started");
  40.     char readbuffer[80];
  41.     while (*(argument->flag)) {
  42.        if(read(argument->fd[0], readbuffer, sizeof(readbuffer))==-1){
  43.            puts("threadTwo's waiting for the record");
  44.            sleep(3);
  45.            (*(int*)counter)++;
  46.            continue;
  47.        } else {
  48.            printf("Readed: ");
  49.            puts(readbuffer);
  50.            (*(int*)counter)++;
  51.            sleep(3);
  52.        }
  53.     }
  54.     puts("threadTwo finished");
  55.     pthread_exit(counter);
  56. }
  57.  
  58.  
  59. int main(int argc, char** argv) {
  60.    
  61.     pthread_t thread1;
  62.     pthread_t thread2;
  63.     bool flag = true;
  64.     arguments args;
  65.     int* fd = new int[2];
  66.    
  67.     pipe(fd);
  68.    
  69.     fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL, 0) | O_NONBLOCK);
  70.    
  71.     args.flag = &flag;
  72.     args.fd = fd;
  73.    
  74.     if(pthread_create(&thread1, NULL, threadOne, (void*)&args)){
  75.         return EXIT_FAILURE;
  76.     }
  77.    
  78.     if(pthread_create(&thread2, NULL, threadTwo, (void*)&args)){
  79.         return EXIT_FAILURE;
  80.     }
  81.    
  82.     getchar();
  83.    
  84.     flag = false;
  85.    
  86.     int *firstCounter = 0;
  87.     int *secondCounter = 0;
  88.    
  89.     if(pthread_join(thread1, (void**)&firstCounter)){
  90.         return EXIT_FAILURE;
  91.     }
  92.    
  93.     if(pthread_join(thread2, (void**)&secondCounter)){
  94.         return EXIT_FAILURE;
  95.     }
  96.    
  97.     cout<<*firstCounter<<endl;
  98.     cout<<*secondCounter<<endl;
  99.    
  100.     free(firstCounter);
  101.     free(secondCounter);
  102.     close(fd[0]);
  103.     close(fd[1]);
  104.     free(fd);
  105.     return EXIT_SUCCESS;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement