Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/mman.h>
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6. #include <sys/stat.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <semaphore.h>
  10.  
  11. #define shm_path "shm123"
  12. #define sem_path "sem123"
  13. #define MAX_MSG_LENGTH 50
  14.  
  15. struct msg_s {
  16. int type;
  17. int content;
  18. };
  19.  
  20.  
  21. int main(int argc, char *argv[]) {
  22.  
  23. int deskryptor;
  24. int rozmiar = (1 * sizeof(struct msg_s));
  25. struct msg_s *shared_msg;
  26.  
  27. sem_t * sem = sem_open(sem_path, 0);
  28. sem_wait(sem);
  29.  
  30. deskryptor = shm_open(shm_path, O_RDWR, S_IRWXU | S_IRWXG);
  31.  
  32. printf("Otwarto pamiec wspoldzielona %s\n", shm_path);
  33.  
  34. shared_msg = (struct msg_s *)mmap(NULL, rozmiar, PROT_READ | PROT_WRITE, MAP_SHARED, deskryptor, 0);
  35.  
  36. printf("Odczytany pid w SHM: %d\n", shared_msg->content);
  37.  
  38. shared_msg->content=getpid();
  39.  
  40. printf("Ustawianie pid w SHM na: %d\n", shared_msg->content);
  41.  
  42. munmap(0, rozmiar);
  43.  
  44. sem_post(sem);
  45.  
  46. return 0;
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement