进程间相互通信.docx
《进程间相互通信.docx》由会员分享,可在线阅读,更多相关《进程间相互通信.docx(13页珍藏版)》请在冰豆网上搜索。
进程间相互通信
集美大学
诚毅学院信息工程系
实验报告
课程名称
计算机操作系统
序号名称
进程间通信(IPC)
姓名
孙幸杰
学号
2011957032
专业
计算1191
日期
2011.11.6
成绩
教师
洪联系
评语:
1.实验目的:
掌握用pipe进行进程间通信;
掌握用共享内存进行进程间通信;
理解父进程与子进程各处独立的地址空间,父进程与子进程,它们只能通过pipe、共享内存等方式进行通信;
2.实验环境
Win7系统,vmware虚拟机下运行的Linux。
3.实验内容
应用Linux共享内存机制和管道机制,实现两个进程间相互传递一个学生的记录,包括:
1.学号:
SID,char型,8位
2.姓名:
NAME,char型,8位
3.年龄:
Age,int型
4.专业:
Specialty,char型,8位
5.班级:
Class,char型,4位
一个记录约30个字节。
4.实验程序
1.Shm1.c
#include
#include
#include
#include
#include
#include
#include
typedefstruct{
charname[4];
charsid[8];
charspecialty[8];
charclass[4];
intage;
}people;
intmain(intargc,char*argv[])
{
intshm_id,i;
key_tkey;
chartemp;
people*p_map;
char*path="./shm1.c";
/*
使用ftok根据path和作为项目标识符的单个字符生成key值确保进程间使用相同的key
使用相同key值的shmget只会在第一次时创建新结构
*/
key=ftok(path,0);
if(key==-1)
{
perror("ftokerror\n");
return-1;
}
shm_id=shmget(key,4096,IPC_CREAT);
if(shm_id==-1)
{
perror("shmgeterror\n");
return-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);
memcpy((*(p_map+i)).sid,&temp,1);
memcpy((*(p_map+i)).specialty,&temp,1);
memcpy((*(p_map+i)).class,&temp,1);
(*(p_map+i)).age=20+i;
}
system("ipcs-m");
if(shmdt(p_map)==-1)
{
perror("detacherror");
}
system("ipcs-m");
}
2.shm2.c
/*
SystemV共享内存区是放在内核当中的,因此在内核重新引导之前,对数据的修改是一直保持的,
这也是我们的实验能够实现的原因,因为在第二个进程起动时,第一个进程已经运行结束了.
*/
#include
#include
#include
#include
#include
/*完成从共享内存区的读出*/
typedefstruct{
charname[4];
charclass[4];
charspecialty[4];
charsid[4];
intage;
}people;
intmain(intargc,char*argv)
{
intshm_id,i;
key_tkey;
people*p_map;
char*path="./shm1.c";
key=ftok(path,0);
if(key==-1)
{
perror("ftokerror\n");
return-1;
}
shm_id=shmget(key,4096,IPC_CREAT);
if(shm_id==-1)
{
perror("shmgeterror");
return-1;
}
p_map=(people*)shmat(shm_id,NULL,0);
for(i=0;i<10;i++)
{
printf("name:
%s\t",(*(p_map+i)).name);
printf("specialty:
%s\t",(*(p_map+i)).specialty);
printf("sid:
%s\t",(*(p_map+i)).sid);
printf("class:
%s\t",(*(p_map+i)).class);
printf("age:
%d\n",(*(p_map+i)).age);
}
system("ipcs-m");
if(shmdt(p_map)==-1)
{
perror("shmdterror\n");
return-1;
}
system("ipcs-m");
exit(EXIT_SUCCESS);
}
3.Pipe.c
#include
#include
#include
#include
intpfd[2];
intprocess1();
intprocess2();
intmain()
{
inti,status;
if(pipe(pfd)<0)
{
printf("pipecreateerror!
\n");
exit
(1);
}
else
{
printf("Pipeiscreatedsuccessfully!
\n");
if(fork()==0)
process1();
if(fork()==0)
process2();
}
close(pfd[0]);
close(pfd[1]);
for(i=0;i<2;i++)
wait(&status);
exit(0);
}
intprocess1()
{
printf("Process1isrunning.......\n");
close(pfd[0]);
inti=0;
charw_buf[5];
for(i=1;i<10;i++)
{
sleep(3);
strcpy(w_buf,"aaa\0");
if(write(pfd[1],w_buf,4)==-1)
printf("Writetopipeerror\n");
}
close(pfd[1]);
printf("Process1isover!
\n");
exit(0);
}
intprocess2()
{
printf("Process2isrunning.......\n");
close(pfd[1]);
charr_buf[5];
while
(1)
{
sleep
(1);
strcpy(r_buf,"kkk\0");
if(read(pfd[0],r_buf,4)==0)break;
printf("Get%sfrompipeinprocess2.\n",r_buf);
}
close(pfd[0]);
printf("Process2isover!
\n");
exit(0);
}
5.实验结果及其分析
Shm1.c输入:
运行:
Shm2.c输入:
运行:
Pipe.c输入:
运行:
6.实验小结
我觉得老师可以教我们怎么把虚拟机与自己的电脑连接起来的方法。
可以把自己电脑上的东西复制到虚拟机上面去。