日期:2014-05-16 浏览次数:20696 次
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include <errno.h>
extern int errno;
typedef struct struTest{
char lstr1[64];
int ii;
char lstr2[126];
}Test, *PTest;
int main(int c, char** v)
{
PTest pt;
int myshmid1 = shmget(0x1234, 3*sizeof(Test), IPC_CREAT|0660);
if(myshmid1==-1)
{
printf("shm1 get err %d\n", errno);
return 0;
}
printf("size test:%d\n", sizeof(Test));
pt = (PTest)shmat(myshmid1, NULL, 0);
for(int i=0;i<3;i++)
{
memset(pt+i, 0, sizeof(Test));
strcpy((pt+i)->lstr1, "aabbcc");
(pt+i)->ii = i+1;
}
int* pishm;
int myshmid2 = shmget(0x1235, sizeof(int), IPC_CREAT|0660);
pishm = (int*)shmat(myshmid2, NULL, 0);
printf("begin test...\n");
for(int i=0;i<3;i++)
{
printf("ptest%d-lstr1:%s, ii:%d\n", i, (pt+i)->lstr1, (pt+i)->ii);
}
int res = shmdt(pt);
printf("dt pt res:%d, errno:%d\n", res, errno);
res = shmdt(pishm);
printf("dt int res:%d, errno:%d\n", res, errno);
printf("end test...\n");
return 0;
}