Guest User

Untitled

a guest
Nov 13th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <sys/types.h>
  4. #include <sys/ipc.h>
  5. #include <sys/shm.h>
  6. #include <stdlib.h>
  7.  
  8. int *A;
  9. int Id;
  10.  
  11. void brisi(int sig)
  12. {
  13. (void) shmdt((char*) A);
  14. (void) shmctl(Id, IPC_RMID, NULL);
  15. exit(0);
  16. }
  17.  
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21. int i;
  22. int N = atoi(argv[1]); // atoi: string u int
  23. int M = atoi(argv[2]);
  24.  
  25.  
  26. Id=shmget(IPC_PRIVATE, sizeof(int), 0600);
  27.  
  28. if(Id==-1){
  29. printf("Neuspjelo dohvacanje memorije");
  30. exit(0);
  31. }
  32. A=(int*)shmat(Id,NULL,0);
  33. *A=0;
  34. for(int j=0; j<N; j++){
  35. for (i = 0; i < M; i++){
  36. switch (fork()) {
  37. case 0:
  38.  
  39. *A+=1;
  40. exit(0);
  41. case -1:
  42. printf("Nije bilo moguce stvoriti proces!");
  43. exit(1);
  44. }
  45. }
  46. }
  47. while (i--) {
  48. wait (NULL);
  49. }
  50.  
  51. printf("A=%d\n", *A);
  52. (void) wait (NULL);
  53. brisi(0);
  54. return 0;
  55. }
Add Comment
Please, Sign In to add comment