Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/shm.h>
- #include <sys/ipc.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/sem.h>
- #include <errno.h>
- #include <unistd.h>
- /* id segmentu pamieci wspoldzielonej */
- int shmID;
- /* taka bedzie struktura danych segmentu */
- struct Dane {
- int numer;
- char napis[20];
- } *dane_ptr;
- int getKey()
- {
- return (ftok(".",'a'));
- }
- void P(int zbior,int element){
- struct sembuf bs = {element,-1,0};
- if(semop(zbior,&bs,1)==-1){
- perror("Blad: funkcja P");
- exit(6);
- }
- }
- void V(int zbior,int element){
- struct sembuf bs = {element,1,0};
- if(semop(zbior,&bs,1)==-1){
- perror("Blad: funkcja V");
- exit(7);
- }
- }
- int creatSem(int number)
- {
- int semid = semget(getKey(), number, 0644 | IPC_CREAT);
- return (semid);
- }
- int creatMemory(){
- shmID=shmget(getKey(),sizeof(struct Dane),IPC_CREAT | SHM_R | SHM_W | SHM_R >> 3);
- if(shmID == -1){
- perror("Blad utworzenia pamieci wspoldzielonej");
- return 1;
- }
- }
- /*dołączenie segmentu pamięci współdzielonej i rzutowanie wskaźnika na "nasz" typ danych przechowywanych w pamięci wpółdzielonej*/
- int main(){
- shmID=shmget(getKey(),sizeof(struct Dane),IPC_CREAT | SHM_R | SHM_W | SHM_R >> 3);
- if(shmID == -1){
- perror("Blad utworzenia pamieci wspoldzielonej");
- return 1;
- }
- dane_ptr = (struct Dane *) shmat(shmID, 0, 0);
- if(((long)dane_ptr)==-1){
- perror("Blad dolaczenia");
- return 2;
- }
- int semid=creatSem(2);
- P(semid,1);
- printf("%s\n",&dane_ptr->napis);
- V(semid,0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement