您的位置:首页 > 运维架构 > Linux

0907使用write函数向共享内存中写入数据,实现不同进程间的数据信息传递

2017-11-23 19:46 519 查看
/*

编写一个程序,使用write函数向共享内存中写入数据,实现不同进程间的数据信息传递
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>

typedef struct
{
char name[4];
int age;
}people;

int main(int argc, char argv[])
{
int shm_id,i;
char temp;
people p_map;
if(argc!=2)
{
printf("Usage:atshm<identifier>");
exit(1);
}
shm_id=atoi(argv[1]);
p_map=(people )shmat(shm_id,NULL,0);
temp='a';
for(i=0;i<10;i++)
{
temp+=1;
memcpy(((p_map+i)).name,&temp,1);
(*(p_map+i)).age=20+i;
}
if(shmdt(p_map)==-1)
{
perror("detach error!\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息