版權聲明:本文爲博主原創文章,未經博主允許不得轉載。
- int shmget(key_t key, size_t size, int shmflg);
- void *shmat(int shm_id, const void *shm_addr, int shmflg);
- int shmdt(const void *shmaddr);
- int shmctl(int shm_id, int command, struct shmid_ds *buf);
- struct shmid_ds
- {
- uid_t shm_perm.uid;
- uid_t shm_perm.gid;
- mode_t shm_perm.mode;
- };
- #ifndef _SHMDATA_H_HEADER
- #define _SHMDATA_H_HEADER
- #define TEXT_SZ 2048
- struct shared_use_st
- {
- int written;//作爲一個標誌,非0:表示可讀,0表示可寫
- char text[TEXT_SZ];//記錄寫入和讀取的文本
- };
- #endif
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/shm.h>
- #include "shmdata.h"
- int main()
- {
- int running = 1;//程序是否繼續運行的標誌
- void *shm = NULL;//分配的共享內存的原始首地址
- struct shared_use_st *shared;//指向shm
- int shmid;//共享內存標識符
- //創建共享內存
- shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666|IPC_CREAT);
- if(shmid == -1)
- {
- fprintf(stderr, "shmget failed\n");
- exit(EXIT_FAILURE);
- }
- //將共享內存連接到當前進程的地址空間
- shm = shmat(shmid, 0, 0);
- if(shm == (void*)-1)
- {
- fprintf(stderr, "shmat failed\n");
- exit(EXIT_FAILURE);
- }
- printf("\nMemory attached at %X\n", (int)shm);
- //設置共享內存
- shared = (struct shared_use_st*)shm;
- shared->written = 0;
- while(running)//讀取共享內存中的數據
- {
- //沒有進程向共享內存定數據有數據可讀取
- if(shared->written != 0)
- {
- printf("You wrote: %s", shared->text);
- sleep(rand() % 3);
- //讀取完數據,設置written使共享內存段可寫
- shared->written = 0;
- //輸入了end,退出循環(程序)
- if(strncmp(shared->text, "end", 3) == 0)
- running = 0;
- }
- else//有其他進程在寫數據,不能讀取數據
- sleep(1);
- }
- //把共享內存從當前進程中分離
- if(shmdt(shm) == -1)
- {
- fprintf(stderr, "shmdt failed\n");
- exit(EXIT_FAILURE);
- }
- //刪除共享內存
- if(shmctl(shmid, IPC_RMID, 0) == -1)
- {
- fprintf(stderr, "shmctl(IPC_RMID) failed\n");
- exit(EXIT_FAILURE);
- }
- exit(EXIT_SUCCESS);
- }
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/shm.h>
- #include "shmdata.h"
- int main()
- {
- int running = 1;
- void *shm = NULL;
- struct shared_use_st *shared = NULL;
- char buffer[BUFSIZ + 1];//用於保存輸入的文本
- int shmid;
- //創建共享內存
- shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666|IPC_CREAT);
- if(shmid == -1)
- {
- fprintf(stderr, "shmget failed\n");
- exit(EXIT_FAILURE);
- }
- //將共享內存連接到當前進程的地址空間
- shm = shmat(shmid, (void*)0, 0);
- if(shm == (void*)-1)
- {
- fprintf(stderr, "shmat failed\n");
- exit(EXIT_FAILURE);
- }
- printf("Memory attached at %X\n", (int)shm);
- //設置共享內存
- shared = (struct shared_use_st*)shm;
- while(running)//向共享內存中寫數據
- {
- //數據還沒有被讀取,則等待數據被讀取,不能向共享內存中寫入文本
- while(shared->written == 1)
- {
- sleep(1);
- printf("Waiting...\n");
- }
- //向共享內存中寫入數據
- printf("Enter some text: ");
- fgets(buffer, BUFSIZ, stdin);
- strncpy(shared->text, buffer, TEXT_SZ);
- //寫完數據,設置written使共享內存段可讀
- shared->written = 1;
- //輸入了end,退出循環(程序)
- if(strncmp(buffer, "end", 3) == 0)
- running = 0;
- }
- //把共享內存從當前進程中分離
- if(shmdt(shm) == -1)
- {
- fprintf(stderr, "shmdt failed\n");
- exit(EXIT_FAILURE);
- }
- sleep(2);
- exit(EXIT_SUCCESS);
- }