Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/sem.h>
- #include <time.h>
- #include <unistd.h>
- #include <signal.h>
- int id_shmget,semid=-1;
- int*adres;
- struct shmid_ds *buf;
- static struct sembuf op_lock[1]={
- {0,-1,0}
- };
- static struct sembuf op_unlock[1]={
- {1,1,0}
- };
- void blokuj(int semid)
- {
- if(semop(semid,&op_lock[0],1)<0)
- perror("blad blokowania semafora");
- }
- void odblokuj(int semid)
- {
- if(semop(semid,&op_unlock[0],1)<0)
- perror("blad odlokowania semafora");
- }
- int main()
- {
- srand(time(NULL));
- id_shmget=shmget(1337,sizeof(int),0666);
- if(id_shmget==-1)
- {
- exit(0);
- }
- else printf ("Utworzono segment pamieci wspolnej \n");
- if((semid = semget(ftok("serwer.c",0),2, 0666))<0)
- perror("Blad tworzenia semafora");
- adres=shmat(id_shmget,0,0);
- int czas;
- int liczba=1;
- int i = 0;
- for (i = 0; i<10;i++)
- {
- odblokuj(semid);
- blokuj(semid);
- liczba = (*adres);
- printf("%d\n",*adres);
- czas=rand()%1000000;
- usleep(czas);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment