Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <semaphore.h>
- #include <pthread.h>
- #include <unistd.h>
- sem_t s1, s2, s3;
- pthread_t thread1, thread2, thread3;
- void *thread_1();
- void *thread_2();
- void *thread_3();
- int main() {
- sem_init(&s1,0,1);
- sem_init(&s2,0,1);
- pthread_create(&thread1, NULL, thread_1, NULL);
- pthread_create(&thread2, NULL, thread_2, NULL);
- pthread_join(thread1, NULL);
- pthread_join(thread2, NULL);
- printf("\n");
- }
- void *thread_1(){
- for(int i=0; i<10;i++)
- {
- sem_wait(&s1);
- sleep(1);
- sem_wait(&s2);
- printf("\nOperacje na wspolnych danych watek 2 (%d)", i);
- sleep(1);
- sem_post(&s1);
- sem_post(&s2);
- }
- }
- void *thread_2(){
- for(int i=0; i<10;i++)
- {
- sem_wait(&s1);
- sleep(1);
- sem_wait(&s2);
- printf("\nOperacje na wspolnych danych watek 1 (%d)", i);
- sleep(1);
- sem_post(&s1);
- sem_post(&s2);
- }
- }
- void *thread_3(){
- for(int i=0; i<10;i++)
- {
- sem_wait(&s1);
- sleep(1);
- sem_wait(&s2);
- printf("\nOperacje na wspolnych danych watek 3 (%d)", i);
- sleep(1);
- sem_post(&s1);
- sem_post(&s2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement