进程间相互通信_第1页
进程间相互通信_第2页
进程间相互通信_第3页
进程间相互通信_第4页
进程间相互通信_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、200X级XXXX专业 集美大学诚毅学院 20112012学年第一学期集 美 大 学诚毅学院信息工程系实 验 报 告课程名称计算机操作系统序号名称 进程间通信(IPC)姓名孙幸杰学号2011957032专业计算1191日期2011.11.6成绩教师洪联系评语:1.实验目的:掌握用pipe进行进程间通信;掌握用共享内存进行进程间通信;理解父进程与子进程各处独立的地址空间,父进程与子进程,它们只能通过pipe、共享内存等方式进行通信;2实验环境Win7系统,vmware虚拟机下运行的Linux。3.实验内容应用Linux共享内存机制和管道机制, 实现两个进程间相互传递一个学生的记录,包括:1. 学

2、号: SID, char型, 8位2. 姓名: NAME, char型, 8位3. 年龄: Age, int型4. 专业: Specialty, char型, 8位5. 班级: Class, char型, 4位一个记录约30个字节。4实验程序1.Shm1.c#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <stri

3、ng.h>typedef structchar name4;char sid8;char specialty8;char class4;int age;people;int main(int argc, char *argv)int shm_id,i;key_t key;char temp;people *p_map;char *path = "./shm1.c"/*使用 ftok根据path和作为项目标识符的单个字符生成key值确保进程间使用相同的 key使用相同key值的shmget只会在第一次时创建新结构*/key = ftok(path,0);if(key =

4、 -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 = (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

5、,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 -m");2.shm2.c/*System V共享内存区是放在内核当中的,因此在内核重新引导之前,对数据的修改是一直保持的,这也是我们的实验能够实现的原因,因为在第二个进程起动时,

6、第一个进程已经运行结束了.*/#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <stdio.h>#include <stdlib.h>/*完成从共享内存区的读出*/typedef structchar name4;char class4;char specialty4;char sid4;int age;people;int main (int argc, char* argv)int shm_id, i;key_t key;people *

7、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; i<10; i+) printf("name:%st",(

8、*(p_map+i).name); printf("specialty:%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"); ret

9、urn -1;system("ipcs -m");exit(EXIT_SUCCESS);3Pipe.c#include <unistd.h>#include <sys/types.h>#include <sys/file.h>#include <unistd.h>int pfd2;int process1();int process2();int main() int i,status; if(pipe(pfd)<0) printf("pipe create error!n"); exit(1); e

10、lse printf("Pipe is created successfully!n"); if(fork()=0) process1(); if(fork()=0) process2(); close(pfd0); close(pfd1); for(i=0;i<2;i+) wait(&status); exit(0); int process1() printf("Process 1 is running.n"); close(pfd0); int i=0; char w_buf5; for(i=1;i<10;i+) sleep(3

11、); 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&qu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论