|
請問這樣能否平衡兩者所需??
信號量及共享記憶體 初始化
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/sem.h>
- #define KEY (1492)
- void sem_lock(int sem_set_id)
- {
- /* structure for semaphore operations. */
- struct sembuf sem_op;
- /* wait on the semaphore, unless it's value is non-negative. */
- sem_op.sem_num = 0;
- sem_op.sem_op = -1;
- sem_op.sem_flg = 0;
- semop(sem_set_id, &sem_op, 1);
- }
- void sem_unlock(int sem_set_id)
- {
- /* structure for semaphore operations. */
- struct sembuf sem_op;
- /* signal the semaphore - increase its value by one. */
- sem_op.sem_num = 0;
- sem_op.sem_op = 1; /* <-- Comment 3 */
- sem_op.sem_flg = 0;
- semop(sem_set_id, &sem_op, 1);
- }
- void shared_memory()
- {
- int shmid;
- int *shm;
- key_t key = IPC_PRIVATE;
- int flag = SHM_R | SHM_W;
-
- key = 1234;
-
- shmid = shmget(key, 1000, flag);
- printf("shared memory for key %d: %d\n", key, shmid);
- if (shmid < 0) {
- perror("shmget");
- printf("Try to create this segment\n");
- shmid = shmget(key, 1000, flag | IPC_CREAT);
- if (shmid < 0) {
- perror("shmget | IPC_CREAT");
- }
- }
- shm = (int *)shmat(shmid, /*addr*/0, /*flag*/0);
- if (shm == (int *)-1) {
- perror("shmat");
- exit(1);
- }
- [color=red]*shm = 5;[/color]
- printf("shared memory content: %d\n", *shm);
- }
- void sema_init()
- {
- int id;
- union semun {
- int val;
- struct semid_ds *buf;
- ushort * array;
- } argument;
- argument.val = 0;
- id = semget(KEY, 1, 0666 | IPC_CREAT);
- if(id < 0)
- {
- fprintf(stderr, "Unable to obtain semaphore.\n");
- exit(0);
- }
- if( semctl(id, 0, SETVAL, argument) < 0)
- {
- fprintf( stderr, "Cannot set semaphore value.\n");
- }
- else
- {
- fprintf(stderr, "Semaphore %d initialized.\n", KEY);
- }
- }
- int main(){
- sema_init();
- shared_memory();
- return 0;
- }
复制代码
生產者
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/sem.h>
- #define KEY (1492)
- void sem_lock(int sem_set_id)
- {
- /* structure for semaphore operations. */
- struct sembuf sem_op;
- /* wait on the semaphore, unless it's value is non-negative. */
- sem_op.sem_num = 0;
- sem_op.sem_op = -1;
- sem_op.sem_flg = 0;
- semop(sem_set_id, &sem_op, 1);
- }
- void sem_unlock(int sem_set_id)
- {
- /* structure for semaphore operations. */
- struct sembuf sem_op;
- /* signal the semaphore - increase its value by one. */
- sem_op.sem_num = 0;
- sem_op.sem_op = 1; /* <-- Comment 3 */
- sem_op.sem_flg = 0;
- semop(sem_set_id, &sem_op, 1);
- }
- int main()
- {
- int shmid;
- int *shm;
- key_t key;
- key = 1234;
- printf("Trying shared memory %d\n", key);
- shmid = shmget(key, 1000, SHM_R | SHM_W);
- if (shmid < 0) {
- perror("shmget");
- shmid = key;
- }
- else {
- printf("shared memory: %d\n", shmid);
- }
- shm = (int *)shmat(shmid, /*addr*/0, /*flag*/0);
- if (shm == (int *)-1) {
- perror("shmat");
- exit(1);
- }
- printf("shared memory: %p\n", *shm);
- if (shm != 0) {
- printf("shared memory content: %d\n", *shm);
- }
- int sem_set_id;
- sem_set_id = semget(KEY, 1, 0666);
- sem_unlock(sem_set_id);
- int pause_time;
- int i;
- for(i=0; i<10; i++){
- sem_lock(sem_set_id);
- pause_time = rand() % 3;
- sleep(pause_time);
- if(*shm < 5){
- *shm = *shm + 1;
- printf("producer item: %d\n", *shm);
- }else {printf("Full: %d\n", *shm);}
- //printf("C");
- fflush(stdout);
- sem_unlock(sem_set_id);
- if(*shm < 5){
- *shm = *shm + 1;
- printf("producer item: %d\n", *shm);
- }else {printf("Full: %d\n", *shm);}
- //printf("C");
- fflush(stdout);
- pause_time = rand() % 2;
- sleep(pause_time);
- }
- return 0;
- } /* main */
复制代码
消費者
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/sem.h>
- #define KEY (1492)
- void sem_lock(int sem_set_id)
- {
- /* structure for semaphore operations. */
- struct sembuf sem_op;
- /* wait on the semaphore, unless it's value is non-negative. */
- sem_op.sem_num = 0;
- sem_op.sem_op = -1;
- sem_op.sem_flg = 0;
- semop(sem_set_id, &sem_op, 1);
- }
- void sem_unlock(int sem_set_id)
- {
- /* structure for semaphore operations. */
- struct sembuf sem_op;
- /* signal the semaphore - increase its value by one. */
- sem_op.sem_num = 0;
- sem_op.sem_op = 1; /* <-- Comment 3 */
- sem_op.sem_flg = 0;
- semop(sem_set_id, &sem_op, 1);
- }
- int main()
- {
- int shmid;
- int *shm;
- key_t key;
- key = 1234;
- printf("Trying shared memory %d\n", key);
- shmid = shmget(key, 1000, SHM_R | SHM_W);
- if (shmid < 0) {
- perror("shmget");
- shmid = key;
- }
- else {
- printf("shared memory: %d\n", shmid);
- }
- shm = (int *)shmat(shmid, /*addr*/0, /*flag*/0);
- if (shm == (int *)-1) {
- perror("shmat");
- exit(1);
- }
- printf("shared memory: %p\n", *shm);
- if (shm != 0) {
- printf("shared memory content: %d\n", *shm);
- }
- int sem_set_id;
- sem_set_id = semget(KEY, 1, 0666);
- sem_unlock(sem_set_id);
- int pause_time;
- int i;
- sleep(2);
- for(i=0; i<10; i++){
- sem_lock(sem_set_id);
- pause_time = rand() % 3;
- sleep(pause_time);
- if(*shm > 0){
- *shm = *shm - 1;
- printf("Consuemr item: %d\n", *shm);
- }else {printf("Out of Stock: %d\n", *shm);}
- //printf("D");
- fflush(stdout);
- sem_unlock(sem_set_id);
- if(*shm > 0){
- *shm = *shm - 1;
- printf("Consuemr item: %d\n", *shm);
- }else {printf("Out of Stock: %d\n", *shm);}
- //printf("D");
- fflush(stdout);
- pause_time = rand() % 2;
- sleep(pause_time);
- }
- return 0;
- } /* main */
复制代码 |
|