?? sharemem-use.c
字號(hào):
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#define BUF_NUM (10)
int main()
{
int id, pid, i;
char *add;
id=shmget(IPC_PRIVATE,BUF_NUM,0666);
if(-1 == id)
{
printf("shmget error\r\n");
return -1;
}
else
{
printf("creat memory successful, shmget is %d\r\n",id);
}
pid = fork();
if(0 < pid)
{
add = shmat(id,0,0);
if((char *)-1 == add)
{
printf("shmat error\r\n");
return -1;
}
else
{
printf("attached memory, address is %p\r\n", add);
}
memset(add, 20, BUF_NUM);
printf("father: write buffer over\r\n");
}
else if(0 == pid)
{
sleep(3);
#if 1
add = shmat(id,(const void *)0x300000,0);
#else
add = shmat(id,0,0);
#endif
if((char *)-1 == add)
{
printf("shmat error\r\n");
return -1;
}
else
{
printf("attached memory, address is %p\r\n", add);
}
printf("child: read buffer\r\n");
for(i = 0; i < BUF_NUM; i++)
{
printf("%d",add[i]);
}
printf("\r\n");
}
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -