Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <semaphore.h>
- int main(){
- sem_t Sem;
- int Wartosc;
- int i;
- Wartosc=10;
- sem_init(&Sem,0,Wartosc);
- sem_getvalue(&Sem,&Wartosc);
- printf("\nWartosc zmiennej semaforowej Sem=%d ", Wartosc);
- for(i=Wartosc; i>0;i--){
- sem_wait(&Sem); sem_getvalue(&Sem, &Wartosc);
- printf("\nWartosc zmiennej semaforowej Sem=%d ", Wartosc);
- }
- printf("\n");
- sem_destroy(&Sem); //usuniecie semafora
- exit(0);
- }
- #include <stdio.h>
- #include <semaphore.h>
- int main(){
- sem_t Sem;
- int Wartosc;
- int i;
- Wartosc=10;
- sem_init(&Sem, 0, Wartosc);
- sem_getvalue(&Sem, &Wartosc);
- printf("\nWartosc zmiennej semaforowej Sem= %d ", Wartosc);
- for(i=Wartosc; i>0; i--){
- sem_post(&Sem); sem_getvalue(&Sem ,&Wartosc);
- printf("\nWartosc zmiennej semaforowej Sem=%d ", Wartosc);
- }
- printf("\n");
- sem_destroy(&Sem);
- exit(0);
- }
- #include <stdio.h>
- #include <semaphore.h>
- int main(){
- sem_t Sem;
- int Wartosc, result ,i;
- Wartosc = 10;
- sem_init(&Sem, 0, Wartosc);
- sem_getvalue(&Sem, &Wartosc);
- printf("\nWartosc zmiennej semaforowej Sem=%d ",Wartosc);
- for(i=Wartosc; i>=-1; i--){
- result=sem_trywait(&Sem);
- sem_getvalue(&Sem, &Wartosc);
- if(result==0)
- printf("\nWartosc zmiennej semaforowej Sem= %d ", Wartosc);
- else
- printf("\nWartosc zmiennej semaforowej Sem=%d Watek zostanie zablkowany", Wartosc);
- }
- printf("\n"); sem_destroy(&Sem);
- exit(0);
- } //main
- #include <stdio.h>
- #include <semaphore.h>
- void * Down();
- void * Up();
- static sem_t Sem;
- int main() {
- pthread_t Tid_Up, Tid_Down;
- sem_init(&Sem, 0, 5);
- pthread_create(&Tid_Down, NULL, Down, NULL);
- pthread_create(&Tid_Up, NULL, Up, NULL);
- pthread_join(Tid_Up, NULL);
- pthread_join(Tid_Down, NULL);
- printf("\n");
- sem_destroy(&Sem);
- exit(0);
- }
- void *Down(){
- int Wartosc;
- int i;
- int result;
- for(i=1; i<10;i++){
- sem_getvalue(&Sem,&Wartosc);
- printf("\nFUNKCJA DOWN: Sem=%d ", Wartosc);
- result=sem_trywait(&Sem);
- if(result==0){
- sem_wait(&Sem);
- }
- else {
- printf("wartosc zmiennej result %d --> watek ulegnie zawieszeniu", result);
- sem_wait(&Sem);
- }
- sleep(2);
- }
- }
- void *Up(){
- int Wartosc;
- int i;
- for(i=1;i<10;i++){
- sem_getvalue(&Sem, &Wartosc);
- printf("\nFUNKCJA Up:(przed operacja SEM_POS Sem=%d) ", Wartosc);
- sem_post(&Sem);
- sem_getvalue(&Sem,&Wartosc);
- printf("(po operacji SEM_POS: Sem=%d) ", Wartosc);
- sleep(4);
- }
- }
- #include <stdio.h>
- #include <semaphore.h>
- void * Down();
- void * Up();
- static sem_t Sem;
- int main() {
- pthread_t Tid_Up, Tid_Down1, Tid_Down2;
- sem_init(&Sem, 0, 5);
- pthread_create(&Tid_Down1, NULL, Down, NULL);
- pthread_create(&Tid_Down2, NULL, Down, NULL);
- pthread_create(&Tid_Up, NULL, Up, NULL);
- pthread_join(Tid_Up, NULL);
- pthread_join(Tid_Down1, NULL);
- pthread_join(Tid_Down2, NULL);
- printf("\n");
- sem_destroy(&Sem);
- exit(0);
- }
- void *Down(){
- int Wartosc;
- int i;
- int result;
- for(i=1; i<10;i++){
- sem_getvalue(&Sem,&Wartosc);
- printf("\nFUNKCJA DOWN: Sem=%d ", Wartosc);
- result=sem_trywait(&Sem);
- if(result==0){
- sem_wait(&Sem);
- }
- else {
- printf("wartosc zmiennej result %d --> watek ulegnie zawieszeniu", result);
- sem_wait(&Sem);
- }
- sleep(2);
- }
- }
- void *Up(){
- int Wartosc;
- int i;
- for(i=1;i<10;i++){
- sem_getvalue(&Sem, &Wartosc);
- printf("\nFUNKCJA Up:(przed operacja SEM_POS Sem=%d) ", Wartosc);
- sem_post(&Sem);
- sem_getvalue(&Sem,&Wartosc);
- printf("(po operacji SEM_POS: Sem=%d) ", Wartosc);
- sleep(4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement