📄 pub_shm.c
字号:
/* *共享内存操作的一些函数 *zyq - shilyu - cff 0901 *functionnum 005 */ #include "./../inc/pub.h"#if !defined( DEBUG )#define DEBUG#endif/* 001 *获得共享内存( 存在时获得 不存在时建立 ) * -1 错误 >0 成功 */ int CrtShm( key, shmsize )key_t key ; /*关键字*/int shmsize ; /*大小 */{ int shmid ; shmid = 0 ; if(( shmid=shmget( key, shmsize, IPC_CREAT|IPC_EXCL|0666 ) ) == -1 ) { #if defined( DEBUG ) fprintf( stderr, "shared memory segment exists-opening as client \n" ) ; #endif if(( shmid = shmget( key, shmsize, 0 ) ) == -1 ) { perror("shmget" ) ; return ERROR ; } } else { #if defined ( DEBUG ) fprintf( stderr, "creating new shared memory segment\n") ; #endif } return shmid ;}/* 002 * 将共享内存映射到进程自已的内存空间 */int GetPtrShmat( shmid, segptr, shmflag )int shmid ; /*共享内存标识符 */char *segptr ; /*没有分配空间的指针*/int shmflag ; /* 0 一般 * SHM_END 020000 round attach address to SHMLBA * SHM_RDONLY attach read-only (else read-write) */{ if( ( segptr = shmat( shmid, ( char *)0, shmflag ) ) == NULL ) { perror("shmat" ) ; return ERROR ; } return SUCCESS ;} /* 003 * 删除共享内存 */ int RemoveShm( shmid )int shmid ;{ if( shmctl( shmid, IPC_RMID, 0 ) == -1 ) /* 并不时立即删除,只是做了一个标记,等与其连接 * 的进程断开连接,在所在连接都断开后才能执行真 * 正的删除操作 */ { return ERROR ; } #if defined ( DEBUG ) fprintf( stderr, "shard memory segment marked for delete \n") ; #endif return SUCCESS ;}/* 004 * 向共享内存写数据 */int WriteShm( segptr, position, text )char *segptr ; /* 指向共享内存的指针 */int position ; /* 共享内存起始的位置 */char *text ; /* 写入共享内的数据 */{ if( text == NULL ) return ERROR ; memcpy( segptr + position, text, strlen( text ) ) ; return SUCCESS ;}/* 005 * 设定(改变)共享内存的信息 */ int ChangeShm( shmid, mode )int shmid ;char *mode ;{ struct shmid_ds myshmds ; if( shmctl( shmid, IPC_STAT, &myshmds ) == -1 ) return ERROR ; sscanf( mode, "%o", &myshmds.shm_perm.mode ) ; if( shmctl( shmid, IPC_SET, &myshmds ) == -1 ) return ERROR ; return SUCCESS ; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -