Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <stdlib.h>
- int *A;
- int Id;
- void brisi(int sig)
- {
- (void) shmdt((char*) A);
- (void) shmctl(Id, IPC_RMID, NULL);
- exit(0);
- }
- int main(int argc, char *argv[])
- {
- int i;
- int N = atoi(argv[1]); // atoi: string u int
- int M = atoi(argv[2]);
- Id=shmget(IPC_PRIVATE, sizeof(int), 0600);
- if(Id==-1){
- printf("Neuspjelo dohvacanje memorije");
- exit(0);
- }
- A=(int*)shmat(Id,NULL,0);
- *A=0;
- for(int j=0; j<N; j++){
- for (i = 0; i < M; i++){
- switch (fork()) {
- case 0:
- *A+=1;
- exit(0);
- case -1:
- printf("Nije bilo moguce stvoriti proces!");
- exit(1);
- }
- }
- }
- while (i--) {
- wait (NULL);
- }
- printf("A=%d\n", *A);
- (void) wait (NULL);
- brisi(0);
- return 0;
- }
Add Comment
Please, Sign In to add comment