嵌入式技术
#include
//获取key值, key值是System V IPC的标识符,成功返回key,失败返回-1设errno//同pathname+同 proj_id==>同key_t;key_t ftok(const char *pathname, int proj_id);
pathname :文件名
proj_id: 1~255的一个数,表示project_id
key_t key=ftok(".",100); //“.”就是一个存在且可访问的路径, 100是假设的proj_id if(-1==key) perror("ftok"),exit(-1);
//创建/获取共享内存,成功返回共享内存的标识符shmid,失败返回-1设errnoint shmget(key_t key, size_t size, int shmflg); //多设为int shmid=... 和shmat()一起用比较好看
key :ftok()的返回值
size:共享内存的大小,实际会按照页的大小(PAGE_SIZE)来分配。0表示获取已经分配好的共享内存
shmflg:具体的操作标志
//创建shared memoryshmid=shmget(key,4,IPC_CREAT|IPC_EXCL|0664);if(-1==shmid) perror("shmget"),exit(-1);
Q:既然shmget()可以创建, 那要ftok()有啥用
A:shmget才是创建共享内存, ftok()只是用来产生一个key,其实这个key的位置自己随意填一个数也可以运行,但是相对系统生成的,很容易造成冲突,所以最好用ftok产生一个key
//挂接共享内存,成功返回映射内存的地址,失败返回(void*)-1设errnovoid *shmat(int shmid, const void *shmaddr, int shmflg);
shmid: shmget()的返回值
shmaddr
//挂接共享内存void* pv=shmat(shmid,NULL,0);if((void*)-1==pv) perror("shmat"),exit(-1);
//脱接共享内存,成功返回0,失败返回-1设errnoint shmdt(const void *shmaddr);
//脱接shmint res=shmdt(pv);if(-1==res) perror("shmdt"),exit(-1);
//共享内存管理,成功返回0,失败返回-1设errnoint shmctl(int shmid, int cmd, struct shmid_ds *buf);
shmid:共享内存的id,由shmget()返回
buf : shmid_ds类型的指针
struct shmid_ds { struct ipc_perm shm_perm; /* Ownership and permissions */ size_t shm_segsz; /* Size of segment (bytes) */ time_t shm_atime; /* Last attach time */ time_t shm_dtime; /* Last detach time */ time_t shm_ctime; /* Last change time */ pid_t shm_cpid; /* PID of creator */ pid_t shm_lpid; /* PID of last shmat(2)/shmdt(2) */ shmatt_t shm_nattch; /* No. of current attaches */ ...};//
cmd
IPC_INFO(Linux-specific)返回系统对共享内存的限制写入到buf指向的时shminfo结构体中
//_GNU_SOURCEstruct shminfo { unsigned long shmmax; /* Maximum segment size */ unsigned long shmmin; /* Minimum segment size; always 1 */ unsigned long shmmni; /* Maximum number of segments */ unsigned long shmseg; /* Maximum number of segments that a process can attach; unused within kernel */ unsigned long shmall; /* Maximum number of pages of shared memory, system-wide */ }; //shmmni, shmmax, and shmall 可以童工/proc里的同名文件进行修改
SHM_INFO(Linux-specific) 返回一个shm_info结构体来表示该共享内存消耗的系统资源
//_GNU_SOURCEstruct shm_info { int used_ids; /* # of currently existing segments */ unsigned long shm_tot; /* Total number of shared memory pages */ unsigned long shm_rss; /* # of resident shared memory pages */ unsigned long shm_swp; /* # of swapped shared memory pages */ unsigned long swap_attempts; /* Unused since Linux 2.4 */ unsigned long swap_successes;/* Unused since Linux 2.4 */ };
SHM_UNLOCK 解除锁定,即允许共享内存被系统放到swap区
//使用IPC_RMID删除共享内存int res=shmctl(shmid,IPC_RMID,NULL);if(-1==res) perror("shmctl"),exit(-1);
//Sys V IPC shmint shmid; //定义全局变量记录idvoid fa(int signo){ printf("deleting shared memories...\n"); sleep(3);//其实没用 int res=shmctl(shmid,IPC_RMID,NULL); if(-1==res) perror("shmctl"),exit(-1); printf("delete success\n"); exit(0); //ctrl+C已经不能结束while(1),用exit(0)来终结}int main(){ //获取key key_t key=ftok(".",100); //.就是一个存在且可访问的路径, 100是随便给的 if(-1==key) perror("ftok"),exit(-1); printf("key=%#x\n",key); //打印出进制的标示,即0x //创建shared memory shmid=shmget(key,4,IPC_CREAT|IPC_EXCL|0664); if(-1==shmid) perror("shmget"),exit(-1); printf("shmid=%d\n",shmid); //挂接shm void* pv=shmat(shmid,NULL,0); if((void*)-1==pv) perror("shmat"),exit(-1); printf("link shared memory success\n"); //访问shm int* pi=(int*)pv; *pi=100; //脱接shm int res=shmdt(pv); if(-1==res) perror("shmdt"),exit(-1); printf("unlink success\n"); //如果不再使用,删除shm printf("删除共享内存请按Ctrl C...\n"); if(SIG_ERR==signal(SIGINT,fa)) perror("signal"),exit(-1); while(1); return 0;}
全部0条评论
快来发表一下你的评论吧 !