- 论坛徽章:
- 0
|
- if(iRet < 0) {
- printf("shmget error:%s \n", strerror(errno));
- return(iRet);
- }
复制代码
改成:
- if(iShmID < 0) {
- printf("shmget error:%s \n", strerror(errno));
- return(iRet);
- }
复制代码
- #include <stdio.h>
- #include <errno.h>
- #include <sys/shm.h>
- key_t
- Ftok(const char *psPath, int iId, char *psMsg) {
- key_t tRet;
- char sPath[1024+1];
- if((psPath == NULL) || (psMsg == NULL)) {
- printf("Ftok error: parameter error \n");
- exit(-1);
- }
- memset(sPath, 0x00, sizeof(sPath));
- memset(psMsg, 0x00, sizeof(psMsg));
- strcpy(sPath, psPath);
- tRet = ftok(sPath, iId);
- if(tRet < 0) {
- sprintf(psMsg, "ftok error: %s", strerror(errno));
- return(tRet);
- }
- return(tRet);
- }
- int
- main(int argc, char **argv) {
- key_t tRet;
- int iRet;
- int iShmID;
- char sMsg[1024+1];
- char *psShm;
- tRet = Ftok(".", 9, sMsg);
- if(tRet < 0) {
- printf("Ftok error:%s \n", sMsg);
- return(tRet);
- }
- iShmID = shmget(tRet, 256, IPC_CREAT|SHM_R|SHM_W);
- if(iShmID < 0) {
- printf("shmget error:%s \n", strerror(errno));
- return(iRet);
- }
- psShm = shmat(iShmID, 0, 0);
- if(psShm == (void *)(-1)) {
- printf("shmat error:%s \n", strerror(errno));
- return(-1);
- }
- printf("[%x]--[%x]\n", psShm, psShm+256);
- iRet = shmdt((void *)psShm);
- if(iRet < 0) {
- printf("shmdt error:%s \n", strerror(errno));
- return(iRet);
- }
- iRet = shmctl(iShmID, IPC_RMID, 0);
- if(iRet < 0) {
- printf("shmctl error:%s \n", strerror(errno));
- return(iRet);
- }
- return 0;
- }
复制代码 |
|