深圳智锐通科技:Linux环境进程间通信: 共享内存
来源:百度文库 编辑:偶看新闻 时间:2024/05/01 13:14:35
/***shmwritetest.c**/
#include
#include
#include
#include
typedef struct{
char name[4];
int age;
} people;
main(int argc, char** argv)
{
int shm_id,i;
key_t key;
char temp;
people *p_map;
char* name = "/home";
key = ftok(name,0);
if(key==-1)
perror("ftok error");
shm_id=shmget(key,4096,IPC_CREAT);
if(shm_id==-1)
{
perror("shmget error");
return;
}
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 ");
}
/****shmreadtest.c*****/
#include
#include
#include
#include
typedef struct{
char name[4];
int age;
} people;
main(int argc, char** argv)
{
int shm_id,i;
key_t key;
people *p_map;
char* name = "/home";
key = ftok(name,0);
if(key == -1)
perror("ftok error");
shm_id = shmget(key,4096,IPC_CREAT);
if(shm_id == -1)
{
perror("shmget error");
return;
}
p_map = (people*)shmat(shm_id,NULL,0);
for(i = 0;i<10;i++)
{
printf( "name:%s\n",(*(p_map+i)).name );
printf( "age %d\n",(*(p_map+i)).age );
}
if(shmdt(p_map) == -1)
perror(" detach error ");
}
注意:若编译运行时出现段错误 那么必须在运行时加上sudo!
#include
#include
#include
#include
typedef struct{
char name[4];
int age;
} people;
main(int argc, char** argv)
{
int shm_id,i;
key_t key;
char temp;
people *p_map;
char* name = "/home";
key = ftok(name,0);
if(key==-1)
perror("ftok error");
shm_id=shmget(key,4096,IPC_CREAT);
if(shm_id==-1)
{
perror("shmget error");
return;
}
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 ");
}
/****shmreadtest.c*****/
#include
#include
#include
#include
typedef struct{
char name[4];
int age;
} people;
main(int argc, char** argv)
{
int shm_id,i;
key_t key;
people *p_map;
char* name = "/home";
key = ftok(name,0);
if(key == -1)
perror("ftok error");
shm_id = shmget(key,4096,IPC_CREAT);
if(shm_id == -1)
{
perror("shmget error");
return;
}
p_map = (people*)shmat(shm_id,NULL,0);
for(i = 0;i<10;i++)
{
printf( "name:%s\n",(*(p_map+i)).name );
printf( "age %d\n",(*(p_map+i)).age );
}
if(shmdt(p_map) == -1)
perror(" detach error ");
}
注意:若编译运行时出现段错误 那么必须在运行时加上sudo!
TAO进程间的共享内存通信如何进行?
linux环境下的进程调度算法有哪些?
进程通信
进程间的通信方式 四种
进程创建及进程通信
两个进程如何通信
linux 进程问题
linux 进程状态有几种??
linux /proc 下如何统计一个进程的内存占用?
linux共享问题
什么是共享进程
Linux下的UDP安全通信问题
虚拟机linux 与windows的通信问题!
linux如何设置共享上网?
linux 下 共享 ftp 目录
linux和windows文件共享
linux内存管理
湖南移动通信宏观环境的分析
[通信]三重动态环境是那三重?
怎么理解进程间的通信啊?怎么理解管道啊?
进程通信有哪几种工作方式呀?
关于内存,cpu,进程
内存和系统进程
windowsXP 下可不可以摹拟linux环境