Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <time.h>
- #include <sys/types.h>
- #include <sys/syscall.h>
- #include <sys/ipc.h>
- //n kufli
- //kufli < klientow
- //kazdy 2l
- //losowy czas picia
- //po 2 piwach out
- //do ost klienta
- int n;
- int clients;
- int t;
- sem_t s;
- void drink()
- {
- for(int i=0; i<2; i++)
- {
- sem_wait(&s);
- printf("Klient nr %d zamawia piwo w kuflu nr %d\n", syscall(SYS_gettid), i);
- int x = rand()%t;
- sleep(x);
- printf("Klient nr %d oddaje kufel nr %d po czasie %d\n", syscall(SYS_gettid), i, x);
- sem_post(&s);
- }
- }
- int main(int argc, char** argv)
- {
- int result;
- pthread_t threads[clients];
- if (argc !=4)
- {
- exit(EXIT_FAILURE);
- }
- srand(time(NULL));
- clients = atoi(argv[1]);
- n = atoi(argv[2]);
- t = atoi(argv[3]);
- sem_init(&s, 0, n);
- for(int i=0; i<clients; i++)
- {
- pthread_create(&threads[i], NULL, drink, NULL);
- }
- for(int j=0; j<clients; j++)
- {
- pthread_join(threads[j], &result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement