Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/sem.h>
- #include <signal.h>
- static struct sembuf op_lock1[1] = {0,-1,1};
- static struct sembuf op_unlock1[1] = {0,1,0};
- static struct sembuf op_lock2[1] = {1,-1,0};
- static struct sembuf op_unlock2[1] = {1,1,0};
- void blokuj_sem1(int semid){
- if(semop(semid, &op_lock1[0],1)<0)
- perror("blad blokowania semafora");
- }
- void odblokuj_sem1(int semid){
- if(semop(semid,&op_unlock1[0],1)<0)
- perror("blad odblokowania semafora");
- }
- void blokuj_sem2(int semid){
- if(semop(semid, &op_lock2[0],1)<0)
- perror("blad blokowania semafora");
- }
- void odblokuj_sem2(int semid){
- if(semop(semid,&op_unlock2[0],1)<0)
- perror("blad odblokowania semafora");
- }
- char *shmp;
- int semid;
- int shmid;
- int main(){
- srand(getpid());
- int shmid = shmget(ftok("serwer.c",3),sizeof(int),0666);
- if(shmid == -1){
- perror("blad tworzenia pamieci dzielonej");
- exit(0);
- }
- char *shmp = shmat(shmid,0,0);
- if(shmp == NULL){
- perror("blad dolaczenia pamieci dzielonej");
- exit(0);
- }
- semid = semget(ftok("serwer.c",3), 2 ,0666);
- if(semid<0){
- perror("blad tworzenia zbioru semaforów");
- }
- int liczba;
- int i;
- for(i = 0;i<10;i++){
- odblokuj_sem2(semid);
- blokuj_sem1(semid);
- liczba=*shmp;
- printf("liczba %d\n",liczba);
- int time = rand()%1000001;
- usleep(time);
- }
- shmdt(shmp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement