1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/sem.h> #include <sys/wait.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>
#define BUFFER_SIZE 256
union semun { int val; struct semid_ds *buf; unsigned short *array; };
int init_sem(int sem_id, int init_val) { union semun sem_union; sem_union.val = init_val; if(semctl(sem_id, 0, SETVAL, sem_union) == -1) { perror("init semaphore error.\n"); return -1; } return 0; }
int del_sem(int sem_id) { union semun sem_union; if(semctl(sem_id, 0, IPC_RMID, sem_union) == -1) { return -1; } return 0; }
int sem_p(int sem_id) { struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = -1; sem_b.sem_flg = SEM_UNDO;
if(semop(sem_id, &sem_b, 1) == -1) { perror("P error.\n"); return -1; } return 0; }
int sem_v(int sem_id) { struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = 1; sem_b.sem_flg = SEM_UNDO;
if(semop(sem_id, &sem_b, 1) == -1) { perror("P error.\n"); return -1; } return 0; }
int main(int argc, char *argv[]) { pid_t pid; int sem_id; int shmid; char *shm_addr = NULL; char buf[BUFFER_SIZE];
sem_id = semget(ftok(".", 'a'), 1, 0666|IPC_CREAT); init_sem(sem_id, 0); shmid = shmget(IPC_PRIVATE, BUFFER_SIZE, 0666); if(shmid < 0) { perror("shmget"); exit(1); } printf("Create Shared-memory : %d\n", shmid);
system("ipcs -m"); pid = fork(); if(pid < 0) { perror("fork() error"); exit(1); } else if(pid == 0) { shm_addr = shmat(shmid, 0, 0); if(shm_addr == (char *)-1) { perror("Child: shmat"); exit(1); } printf("[%d]Child : Attach shared-memory [%p]\n", getpid(), shm_addr); system("ipcs -m");
printf("[%d]Child : Wait for enable data...\n", getpid());
while(1) { memset(buf, 0, BUFFER_SIZE); sem_p(sem_id); strcpy(buf, shm_addr); memset(shm_addr, 0, BUFFER_SIZE); printf("[%d]Child : Shared-memory: %s\n", getpid(), buf);
if((!strncmp(buf, "q", 1)) || (!strncmp(buf, "Q", 1))) { printf("[%d]Child Will Quit.\n", getpid()); break; } }
sem_v(sem_id); del_sem(sem_id); if((shmdt(shm_addr)) < 0) { perror("shmdt error"); exit(1); } printf("[%d]Child : Deattach shared-memory\n", getpid()); system("ipcs -m");
if(shmctl(shmid, IPC_RMID, NULL) == -1) { perror("Child : shmctl(IPC_RMID"); exit(1); } printf("[%d]Child : Delete shared-memory\n", getpid()); system("ipcs -m");
exit(0); } else { shm_addr = shmat(shmid, 0, 0); if(shm_addr == (char *)-1) { perror("Parent: shmat"); exit(1); } printf("[%d]Parent : Attach shared-memory : [%p]\n", getpid(), shm_addr); sleep(1);
while(1) { memset(buf, 0, BUFFER_SIZE); printf("\nInput some string:\n"); fgets(buf, BUFFER_SIZE, stdin); strncpy(shm_addr, buf, strlen(buf)); sem_v(sem_id); if((!strncmp(buf, "q", 1)) || (!strncmp(buf, "Q", 1))) { printf("[%d]Parent Will Quit.\n", getpid()); break; } }
if((shmdt(shm_addr)) < 0) { perror("Parent : shmdt error"); exit(1); } printf("[%d]Parent : Deattach shared-memory\n", getpid()); system("ipcs -m"); }
waitpid(pid, NULL, 0); printf("\nFinished...\n");
return 0; }
|