1、进程间相互通信集 美 大 学诚毅学院信息工程系实 验 报 告课程名称计算机操作系统序号名称 进程间通信(IPC)姓名孙幸杰学号2011957032专业计算1191日期2011.11.6成绩教师洪联系评语:1.实验目的:掌握用pipe进行进程间通信;掌握用共享内存进行进程间通信;理解父进程与子进程各处独立的地址空间,父进程与子进程,它们只能通过pipe、共享内存等方式进行通信;2实验环境Win7系统,vmware虚拟机下运行的Linux。3.实验内容应用Linux共享内存机制和管道机制, 实现两个进程间相互传递一个学生的记录,包括:1. 学号: SID, char型, 8位2. 姓名: NAME
2、, 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 typedef struct char name4; char sid8; char specialty8; char class4; int age;people;int main(int argc, char *argv) int shm_id,i; key_t ke
3、y; char temp; people *p_map; char *path = ./shm1.c; /* 使用 ftok根据path和作为项目标识符的单个字符生成key值确保进程间使用相同的 key 使用相同key值的shmget只会在第一次时创建新结构 */ key = ftok(path,0); if(key = -1) perror(ftok error n); return -1; shm_id = shmget(key,4096,IPC_CREAT); if(shm_id = -1) perror(shmget error n); return -1; p_map = (peop
4、le*)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(detach error); system(ipcs
5、 -m);2.shm2.c/*System V共享内存区是放在内核当中的,因此在内核重新引导之前,对数据的修改是一直保持的,这也是我们的实验能够实现的原因,因为在第二个进程起动时,第一个进程已经运行结束了.*/#include #include #include #include #include /*完成从共享内存区的读出*/typedef struct char name4; char class4; char specialty4; char sid4; int age;people;int main (int argc, char* argv) int shm_id, i; key_t
6、 key; people *p_map; char *path = ./shm1.c; key = ftok(path,0); if(key = -1) perror(ftok error n); return -1; shm_id = shmget(key,4096,IPC_CREAT); if(shm_id = -1) perror(shmget error); return -1; p_map = (people*)shmat(shm_id,NULL,0); for(i=0; i10; i+) printf(name:%st,(*(p_map+i).name); printf(speci
7、alty:%st,(*(p_map+i).specialty); printf(sid:%st,(*(p_map+i).sid); printf(class:%st,(*(p_map+i).class); printf(age:%dn,(*(p_map+i).age); system(ipcs -m); if(shmdt(p_map) = -1) perror(shmdt errorn); return -1; system(ipcs -m); exit(EXIT_SUCCESS);3Pipe.c#include #include #include #include int pfd2;int
8、process1();int process2();int main() int i,status; if(pipe(pfd)0) printf(pipe create error!n); exit(1); else printf(Pipe is created successfully!n); if(fork()=0) process1(); if(fork()=0) process2(); close(pfd0); close(pfd1); for(i=0;i2;i+) wait(&status); exit(0); int process1() printf(Process 1 is r
9、unning.n); close(pfd0); int i=0; char w_buf5; for(i=1;i10;i+) sleep(3); strcpy(w_buf,aaa0); if(write(pfd1,w_buf,4)=-1) printf(Write to pipe errorn); close(pfd1); printf(Process 1 is over!n); exit(0); int process2() printf(Process 2 is running.n); close(pfd1); char r_buf5; while(1) sleep(1); strcpy(r_buf,kkk0); if(read(pfd0,r_buf,4)=0) break; printf(Get %s from pipe in process 2.n,r_buf); close(pfd0); printf(Process 2 is over!n); exit(0); 5 .实验结果及其分析Shm1.c输入:运行:Shm2.c输入:运行:Pipe.c输入:运行:6实验小结我觉得老师可以教我们怎么把虚拟机与自己的电脑连接起来的方法。可以把自己电脑上的东西复制到虚拟机上面去。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1