1 include pthread h include semaphore h int

  • Slides: 11
Download presentation

(1) פעולות על סמפורים #include <pthread. h> #include <semaphore. h> : • אתחול סמפור

(1) פעולות על סמפורים #include <pthread. h> #include <semaphore. h> : • אתחול סמפור לפני השימוש int sem_init(sem_t *sem, int pshared, unsigned int value); : • פינוי סמפור בתום השימוש int sem_destroy(sem_t *sem);

#include <pthread. h> #include <stdio. h> #include <stdlib. h> סמפור בינארי #include <semaphore. h>

#include <pthread. h> #include <stdio. h> #include <stdlib. h> סמפור בינארי #include <semaphore. h> #define NITER 100000 sem_t sema; long count = 0; void * Thread. Add(void * a) { for(long i = 0; i < NITER; i++) { sem_wait( &sema); count++; sem_post( &sema); } } int main(int argc, char * argv[]) { pthread_t tid 1, tid 2; sem_init(&sema, 0, 1); pthread_create(&tid 1, NULL, Thread. Add, NULL); pthread_create(&tid 2, NULL, Thread. Add, NULL); pthread_join(tid 1, NULL); /* wait for the thread 1 to finish */ pthread_join(tid 2, NULL); /* wait for the thread 2 to finish */ if (count < 2 * NITER) printf("n BOOM! count is [%ld], should be %ldn", count, 2*NITER); else printf("n OK! count is [%ld]n", count); sem_destroy(&sema); pthread_exit(NULL); } OK! count is [200000]

#include <pthread. h> #include <stdio. h> #include <stdlib. h> סמפור בינארי #include <semaphore. h>

#include <pthread. h> #include <stdio. h> #include <stdlib. h> סמפור בינארי #include <semaphore. h> #define NITER 100000 sem_t mutex; long count = 0, count 1 = 0; void * Thread. Add(void * a) { for(long i = 0; i < NITER; i++) { sem_wait( &mutex ); count++; sem_post( &mutex ); } for(i = 0; i < NITER; i++) count++; } int main(int argc, char * argv[]) { pthread_t tid 1, tid 2; sem_init(&mutex, 0, 1); pthread_create(&tid 1, NULL, Thread. Add, NULL); pthread_create(&tid 2, NULL, Thread. Add, NULL); pthread_join(tid 1, NULL); /* wait for the thread 1 to finish */ pthread_join(tid 2, NULL); /* wait for the thread 2 to finish */ if (count < 2 * NITER) printf("n BOOM! count is [%ld], should be %ldn", count, 2*NITER); else printf("n OK! count is [%ld]n", count); if (count < 2 * NITER) printf("n BOOM! count 1 is [%ld], should be %ldn", count 1, 2*NITER); else printf("n OK! count 1 is [%ld]n", count 1); sem_destroy(&mutex); pthread_exit(NULL); } OK! count is [200000]

#include < stdio. h > #include < pthread. h > #include < semaphore. h

#include < stdio. h > #include < pthread. h > #include < semaphore. h > #include < signal. h > sem_t semname; int qval=1; void *func 1(void *arg) { while(qval) { sem_wait(&semname); printf("Thread one prints # %dn", (*(int *)(arg))++); sem_post(&semname); sleep(1); } } void *func 2(void *arg) { while(qval) { sem_wait(&semname); printf("Thread two prints # %dn", (*(int *)(arg))++); sem_post(&semname); sleep(1); } } void quit() { qval=0; } main() { int i=0; signal(SIGINT, quit); //ctrl+c sem_init(&semname, 0, 1); pthread_t thread 1, thread 2; pthread_create(&thread 1, NULL, func 1, &i); pthread_create(&thread 2, NULL, func 2, &i); pthread_join(thread 1, &i); pthread_join(thread 2, &i); printf("n. Quiting. . . n"); return 0; }