Advertisement
rabbitekk312

Untitled

Dec 12th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/shm.h>
  4. #include <stdio.h>
  5.  
  6. int main()
  7. {
  8. int Len;
  9. int ShmId;
  10. int res;
  11. int suma = 0;
  12. key_t Klucz = 3120;
  13.  
  14. void *Addr;
  15. char *Napis;
  16.  
  17. int tab[5] = {1,2,3,4,5};
  18. int i;
  19. ShmId = shmget(Klucz, 10000, IPC_CREAT | 0660);
  20.  
  21. if (ShmId < 0)
  22. {
  23. fprintf(stderr, "\nWystapil w trakcie kreowania sagment pamieci wspolnej");
  24. exit(1);
  25. } //if
  26. if (fork() == 0)
  27. {
  28. Addr = shmat(ShmId, 0, 0);
  29. Napis = (char*) Addr;
  30. for (i=0;i<5;i++){
  31. suma = suma + tab[i];
  32.  
  33. }//for
  34. sprintf(Napis, "---->Proces Potomny %d<-----\nSuma tablicy: %d", getpid(),suma);
  35.  
  36. if (Addr < 0)
  37. {
  38. fprintf(stderr, "\nWystapil w trakcie dolaczania pamieci wspolnej");
  39. exit(1);
  40. } //if addr
  41.  
  42. fprintf(stderr, "\nShared memory atteched from %x to %x", Addr, Addr + 1000);
  43. res = shmdt(Addr);
  44. if (res < 0)
  45. {
  46. fprintf(stderr, "\nWystapil w trakcie odlaczania sagmentu pamieci wspolnej");
  47. exit(1);
  48. } //if res
  49. sleep(10);
  50. printf("\nProces potomny zakonczony");
  51. exit(0);
  52. } //if fork
  53.  
  54. wait(0); //proces gowny
  55. Addr = shmat(ShmId, 0, 0);
  56. Napis = (char*) Addr;
  57. printf("\n\nKOMUNIKAT: %s", Napis);
  58.  
  59. if (Addr < 0)
  60. {
  61. fprintf(stderr, "\nWystapil w trakcie dolaczania pamieci wspolnej");
  62. exit(1);
  63. } //if addr
  64. fprintf(stderr, "\nShared memory attached from %x to %x \n", Addr, Addr + 1000);
  65. res = shmdt(Addr);
  66. if (res < 0)
  67. {
  68. fprintf(stderr, "\nWystapil w trakcie odlaczania sagmentu pamieci wspolnej");
  69. exit(1);
  70. } //if res
  71. res = shmctl(ShmId, IPC_RMID, 0);
  72.  
  73. if (res < 0)
  74. {
  75. fprintf(stderr, "\nWystapil w trakcie usuwania pamieci wspolnej");
  76. exit(1);
  77. } //if res
  78.  
  79. printf("\n \n");
  80. } //main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement