SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <sys/types.h> | |
| 2 | #include <sys/ipc.h> | |
| 3 | #include <sys/shm.h> | |
| 4 | #include <sys/sem.h> | |
| 5 | #include <stdio.h> | |
| 6 | #include <errno.h> | |
| 7 | #include <stdlib.h> | |
| 8 | #define array_size 1000 /* no of elements in shared memory */ | |
| 9 | - | extern char *shmat(); |
| 9 | + | extern void *shmat (int __shmid, __const void *__shmaddr, int __shmflg); |
| 10 | void P(int *s); | |
| 11 | void V(int *s); | |
| 12 | int main() | |
| 13 | {
| |
| 14 | int shmid, s, pid; /* shared memory, semaphore, proc id */ | |
| 15 | char *shm; /*shared mem. addr returned by shmat()*/ | |
| 16 | int *a, *addr, *sum; /* shared data variables*/ | |
| 17 | int partial_sum; /* partial sum of each process */ | |
| 18 | int i; | |
| 19 | /* initialize semaphore set */ | |
| 20 | int init_sem_value = 1; | |
| 21 | s = semget(IPC_PRIVATE, 1, (0600 | IPC_CREAT)); | |
| 22 | if (s == -1) { /* if unsuccessful*/
| |
| 23 | perror("semget");
| |
| 24 | exit(1); | |
| 25 | } | |
| 26 | if (semctl(s, 0, SETVAL, init_sem_value) < 0) {
| |
| 27 | perror("semctl");
| |
| 28 | exit(1); | |
| 29 | } | |
| 30 | /* create segment*/ | |
| 31 | shmid = shmget(IPC_PRIVATE,(array_size*sizeof(int)+1), | |
| 32 | (IPC_CREAT|0600)); | |
| 33 | if (shmid == -1) {
| |
| 34 | perror("shmget");
| |
| 35 | exit(1); | |
| 36 | } | |
| 37 | /* map segment to process data space */ | |
| 38 | shm = shmat(shmid, NULL, 0); | |
| 39 | /* returns address as a character*/ | |
| 40 | if (shm == (char*)-1) {
| |
| 41 | perror("shmat");
| |
| 42 | exit(1); | |
| 43 | } | |
| 44 | addr = (int*)shm; /* starting address */ | |
| 45 | sum = addr; /* accumulating sum */ | |
| 46 | addr++; | |
| 47 | a = addr; /* array of numbers, a[] */ | |
| 48 | *sum = 0; | |
| 49 | for (i = 0; i < array_size; i++) /* load array with numbers */ | |
| 50 | *(a + i) = i+1; | |
| 51 | pid = fork(); /* create child process */ | |
| 52 | if (pid == 0) { /* child does this */
| |
| 53 | partial_sum = 0; | |
| 54 | for (i = 0; i < array_size; i = i + 2) | |
| 55 | partial_sum += *(a + i); | |
| 56 | }else { /* parent does this */
| |
| 57 | partial_sum = 0; | |
| 58 | for (i = 1; i < array_size; i = i + 2) | |
| 59 | partial_sum += *(a + i); | |
| 60 | } | |
| 61 | P(&s); /* for each process, add partial sum */ | |
| 62 | *sum += partial_sum; | |
| 63 | V(&s); | |
| 64 | printf("\nprocess pid = %d, partial sum = %d\n", pid, partial_sum);
| |
| 65 | if (pid == 0) exit(0); else wait(0); /* terminate child proc */ | |
| 66 | printf("\nThe sum of 1 to %i is %d\n", array_size, *sum);
| |
| 67 | /* remove semaphore */ | |
| 68 | if (semctl(s, 0, IPC_RMID, 1) == -1) {
| |
| 69 | perror("semctl");
| |
| 70 | exit(1); | |
| 71 | } | |
| 72 | /* remove shared memory */ | |
| 73 | if (shmctl(shmid, IPC_RMID, NULL) == -1) {
| |
| 74 | perror("shmctl");
| |
| 75 | exit(1); | |
| 76 | } | |
| 77 | } /* end of main */ | |
| 78 | void P(int *s) /* P(s) routine*/ | |
| 79 | {
| |
| 80 | struct sembuf sembuffer, *sops; | |
| 81 | sops = &sembuffer; | |
| 82 | sops->sem_num = 0; | |
| 83 | sops->sem_op = -1; | |
| 84 | sops->sem_flg = 0; | |
| 85 | if (semop(*s, sops, 1) < 0) {
| |
| 86 | perror("semop");
| |
| 87 | exit(1); | |
| 88 | } | |
| 89 | return; | |
| 90 | } | |
| 91 | void V(int *s) /* V(s) routine */ | |
| 92 | {
| |
| 93 | struct sembuf sembuffer, *sops; | |
| 94 | sops = &sembuffer; | |
| 95 | sops->sem_num = 0; | |
| 96 | sops->sem_op = 1; | |
| 97 | sops->sem_flg = 0; | |
| 98 | if (semop(*s, sops, 1) <0) {
| |
| 99 | perror("semop");
| |
| 100 | exit(1); | |
| 101 | } | |
| 102 | return; | |
| 103 | } |