Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/msg.h>
  5. #include <stdlib.h>
  6. #include <sys/sem.h>
  7. #include <signal.h>
  8. #include <sys/shm.h>
  9. #include <err.h>
  10.  
  11.  
  12.  
  13. int main()
  14. { //Zmienne na klucze
  15. key_t key;
  16.  
  17. if ((key = ftok(".", 'B')) == -1)
  18. errx(1, "Blad tworzenia klucza!");
  19.  
  20.  
  21. //Deklaracja pamieci wspoldzielonej
  22. int shmid = shmget(key, 4, IPC_CREAT|0666);
  23. //Pobieram wskaznik na adres pamieci wspoldzielonej
  24.  
  25.  
  26. if(shmid == -1){
  27. perror("Utworzenie segmentu pamieci wspoldzielonej");
  28. exit(1);
  29. }
  30.  
  31.  
  32.  
  33. if(fork() == 0) {/**PROCES K1*/
  34. int i;
  35. int *buf;
  36. buf = shmat(shmid, NULL, 0);
  37. for(i = 0; i <= 100; i++) {
  38. printf("Producent zapisuje liczbe %d\n", i);
  39.  
  40. buf[0] = i;
  41. buf[1] = 0;
  42. sleep(1);
  43. }
  44.  
  45. }
  46.  
  47. if(fork() == 0) {/**PROCES K2*/
  48. int *buf;
  49. int n;
  50. buf = shmat(shmid, NULL, 0);
  51. while(1){
  52. if(buf[1] == 0)
  53. {
  54. n = *buf;
  55. printf("K1 od producenta otrzymalem : %i\n",n);
  56. buf[1] = 1; }
  57. }
  58.  
  59. }
  60. if(fork() == 0) {/**PROCES K3*/
  61.  
  62.  
  63. }
  64.  
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement