daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Apr 17th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <signal.h>
  7.  
  8. sigset_t signalSet;
  9. pthread_t tids[100];
  10. int n, m, k, licznik;
  11.  
  12. void *worker(void *arg)
  13. {
  14.     int sigNo;
  15.     int threadNo = *(int*)arg;
  16.     printf("hello world, from worker %d\n",
  17.            threadNo);
  18.     //pthread_detach(pthread_self());
  19.    
  20.     while (1) {
  21.  
  22.         if (sigwait(&signalSet, &sigNo)) {
  23.             perror("sigwait");
  24.             break;
  25.         }
  26.         if(sigNo == 30) break;
  27.         int t = m;
  28.         while(t > 0)
  29.             t = sleep(t);
  30.            
  31.         fprintf(stderr, "%c", '@' + 1 + threadNo);
  32.        
  33.         if(++licznik > k) {
  34.             pthread_kill(tids[(threadNo+1)%n], 30);
  35.         } else { pthread_kill(tids[(threadNo+1)%n] , 2); }
  36.  
  37.     }
  38.     pthread_exit((void *)0);
  39. }
  40.  
  41. int main(int argc, char *argv[]) {
  42.    
  43.     if(argc != 4) {
  44.         perror(
  45.     n = atoi(argv[1]);
  46.     m = atoi(argv[2]);
  47.     k = atoi(argv[3]);
  48.    
  49.     licznik = 0;
  50.     char buf[10];
  51.     int args[100];
  52.     sigfillset(&signalSet);
  53.  
  54.     pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
  55.     for(int i = 0; i < n; ++i) args[i] = i;
  56.     for(int i = 0; i < n; i++) {
  57.         if (pthread_create(&tids[i], NULL, worker, (void *)(args+i)))
  58.             exit(1);
  59.     }
  60.    
  61.     while ((n = read(0, buf, sizeof(buf))) > 0) {
  62.         write(1, buf, n);
  63.     }
  64.    
  65.     return 0;
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top