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;
- void koniec()
- {
- printf("Usunieto semafor i pamiec dzielona. Koniec programu\n.");
- semctl(semid,0,IPC_RMID,0);
- shmdt(adres);
- shmctl(id_shmget,IPC_RMID,buf);
- exit(0);
- }
- static struct sembuf op_lock[1]={
- {1,-1,0}
- };
- static struct sembuf op_unlock[1]={
- {0,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()
- {
- signal(SIGINT,koniec);
- srand(time(NULL));
- id_shmget=shmget(1337,sizeof(int), IPC_CREAT | IPC_EXCL |0666);
- if(id_shmget==-1)
- {
- exit(0);
- }
- else printf ("Utworzono segment pamieci wspolnej \n");
- if((semid = semget(ftok("serwer.c",0),2, IPC_CREAT |0666))<0)
- perror("Blad tworzenia semafora");
- adres=shmat(id_shmget,0,0);
- int czas;
- int liczba=1;
- while(1)
- {
- blokuj(semid);
- czas=rand()%1000000;
- usleep(czas);
- (*adres)=liczba;
- printf("%d\n",*adres);
- odblokuj(semid);
- liczba++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment