Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <signal.h>
- sigset_t signalSet;
- pthread_t tids[100];
- int n, m, k, licznik;
- void *worker(void *arg)
- {
- int sigNo;
- int threadNo = *(int*)arg;
- printf("hello world, from worker %d\n",
- threadNo);
- //pthread_detach(pthread_self());
- while (1) {
- if (sigwait(&signalSet, &sigNo)) {
- perror("sigwait");
- break;
- }
- if(sigNo == 30) break;
- int t = m;
- while(t > 0)
- t = sleep(t);
- fprintf(stderr, "%c", '@' + 1 + threadNo);
- if(++licznik > k) {
- pthread_kill(tids[(threadNo+1)%n], 30);
- } else { pthread_kill(tids[(threadNo+1)%n] , 2); }
- }
- pthread_exit((void *)0);
- }
- int main(int argc, char *argv[]) {
- if(argc != 4) {
- perror(
- n = atoi(argv[1]);
- m = atoi(argv[2]);
- k = atoi(argv[3]);
- licznik = 0;
- char buf[10];
- int args[100];
- sigfillset(&signalSet);
- pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
- for(int i = 0; i < n; ++i) args[i] = i;
- for(int i = 0; i < n; i++) {
- if (pthread_create(&tids[i], NULL, worker, (void *)(args+i)))
- exit(1);
- }
- while ((n = read(0, buf, sizeof(buf))) > 0) {
- write(1, buf, n);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement