Beispiel #1
0
int main(int argc,char *argv[])
{
	int shmid,size;
	if(argc != 3) return 0;

	shmid = atoi(argv[1]);

	if(strcmp(argv[2],"v") == 0)  //查询共享内存
	{
		StatShm(shmid);
	}

	else if(strcmp(argv[2],"d") == 0)  //删除共享内存
	{
		VERIFYERR(shmctl(shmid,IPC_RMID,NULL) < 0, "Delete Shm");
	}

	else  //创建共享内存
	{
		size = atoi(argv[2]);
		VERIFYERR(shmget(shmid,size,0666|IPC_CREAT|IPC_EXCL) < 0,"Create Shm");
	}
	
	return 0;
}
Beispiel #2
0
void main()
{
	int shmid;
	char *pmat = NULL;
	char buf[1024];
	int no;

	/* 打开信号量 */	
	VERIFYERR((shmid = shmget(0x1234, 10*1024, 0666|IPC_CREAT)) == -1, "Open(Create) Shm");
	/* 映射共享内存,地址存储与pmat处 */
	VERIFYERR((pmat = (char *)shmat(shmid, 0, 0)) == 0, "Link Shm");
	/* 输入更新的内存块号 */
	printf("Please Input No.(0~9):");
	scanf("%d", &no);
	VERIFYERR(no < 0 || no > 9, "Input No.");
	/* 输入更新的数据 */
	printf("Please Input Data:");
	memset(buf, 0, sizeof(buf));
	scanf("%s", buf);
	/* 更新共享内存数据 */;
	memcpy(pmat+no*1024, buf, 1024);
	/* 释放共享内存映射 */
	shmdt(pmat);
}
Beispiel #3
0
//向共享内存中指定位置写入数据
int main(int argc, const char * argv[])
{
    int shmid, no;
    char * pmat = NULL, buf[1024];
    /*打开共享内存*/
    VERIFYERR((shmid = shmget(0x1234, 10 * 1024, 0666 | IPC_CREAT)) == -1, "Open shm");
    /*映射共享内存, 地址存储在pmat处*/
    VERIFYERR((pmat = (char *)shmat(shmid, 0, 0)) == 0, "Link shm");
    /*输入更新的内存块*/
    printf("Please Input No.(0-9):");
    scanf("%d", &no);
    VERIFYERR(no < 0 || no > 9, "Input No.");
    /*输入更新的数据*/
    printf("please Input Data:");
    memset(buf, 0, sizeof(buf));
    scanf("%s", buf);
    /*更新共享内存数据*/
    memcpy(pmat + no * 1024, buf, 1024);
    
    printf("%s", pmat + no*1024);
    /*释放共享内存映射*/
    shmdt(pmat);
    return 0;
}