Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #define _WITH_GETLINE
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<sys/types.h>
  6. #include<sys/ipc.h>
  7. #include<sys/shm.h>
  8.  
  9. /* A. Mroz - zadanie na SK, do modyfikacji */
  10. /* Brak pelnej obslugi bledow! */
  11.  
  12. #define MY_MSG_SIZE 64
  13.  
  14. key_t shmkey;
  15. int shmid;
  16. struct my_data {
  17. int typ;
  18. char txt[MY_MSG_SIZE];
  19. } *shared_data;
  20.  
  21. char *buf = NULL;
  22. size_t bufsize = MY_MSG_SIZE;
  23.  
  24.  
  25. int main(int argc, char * argv[]) {
  26.  
  27.  
  28. printf("[Klient]: tworze klucz...");
  29. shmkey = ftok(argv[1], 1);
  30. printf(" OK (klucz: %ld)\n", shmkey);
  31.  
  32. printf("[Klient]: otwieram segment pamieci wspolnej...");
  33. if( (shmid = shmget(shmkey, 0, 0)) == -1 ) {
  34. printf(" blad shmget\n");
  35. exit(1);
  36. }
  37. printf(" OK (id: %d)\n", shmid);
  38. printf("[Klient]: dolaczam pamiec wspolna...");
  39. shared_data = (struct my_data *) shmat(shmid, (void *)0, 0);
  40. if(shared_data == (struct my_data *)-1) {
  41. printf(" blad shmat!\n");
  42. exit(1);
  43. }
  44. printf(" OK (adres: %lX)\n", (long int)shared_data);
  45. printf("[Klient]: biezaca zawartosc pamieci wspolnej: ");
  46. if(shared_data->typ == 0) printf("PUSTO\n");
  47. else printf("\n%s\n", shared_data->txt);
  48.  
  49.  
  50. printf("[Klient]: podaj komunikat ktory chcesz wpisac do pamieci wspolnej:\n");
  51. getline(&buf, &bufsize, stdin);
  52.  
  53. /* wpisywanie do pamieci dzielonej */
  54. shared_data->typ = 1;
  55. buf[strlen(buf)-1] = '\0'; /* techniczne: usuwam koniec linii */
  56. strcpy(shared_data->txt, buf);
  57.  
  58.  
  59. printf("[Klient]: wpisalem komunikat do pamieci wspolnej\n");
  60.  
  61. shmdt(shared_data);
  62.  
  63. return 0;
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement