• 操作系统课程设计六、进程通信


    操作系统课程设计六、进程通信#

    实验内容###

    编写一程序可以由用户选择如下三种进程通信方式:

    1.使用管道来实现父子进程之间的进程通信子进程向父进程发送自己的进程标识符,以及字符串“is sending a message to parent”。父进程则通过管道读出子进程发来的消息,将消息显示在屏幕上,然后终止。

    2.使用消息缓冲队列来实现 client 进程和 server 进程之间的通信server 进程先建立一个关键字为 SVKEY(如 75)的消息队列,然后等待接收类型为 REQ(例如 1)的消息;在收到请求消息后,它便显示字符串“serving for client”和接收到的 client 进程的进程标识数,表示正在为 client 进程服务;然后再向 client 进程发送应答消息,该消息的类型是 client 进程的进程标识数,而正文则是 server 进程自己的标识ID。client 进程则向消息队列发送类型为 REQ 的消息(消息的正文为自己的进程标识 ID) 以取得 sever 进程的服务,并等待 server 进程发来的应答;然后显示字符串“receive reply from”和接收到的 server 进程的标识 ID。

    3.使用共享存储区来实现两个进程之间的进程通信进程 A 创建一个长度为 512 字节的共享内存,并显示写入该共享内存的数据;进程 B 将共享内存附加到自己的地址空间,并向共享内存中写入数据。

    实验功能及设计思路###

    程序功能:

    提供三种进程通信方式,供用户选择。第一个是管道通信,子进程向父进程发送自己的进程标识符,以及字符串“is sending a message to parent”,父进程接受消息打印屏幕。第二个是消息缓冲队列实现,client 进程和 server 进程之间的通信传递消息。第三个是使用共享存储区来实现两个进程之间的进程通信,进程A创建共享内存,写入数据,进程 B添加到自己地址空间,写入数据。

    设计思路:

    1.主程序中设计一个菜单,提供用户三种进程通信的选择,每当用户选择其中一项,调用相关响应函数。

    2.管道机制类似实验4中的处理。

    3.消息队列:消息队列是消息的链接表,包括 Posix 消息队列 systemV 消息队列。它克服了另外两种通信方式中信息量有限的缺点,对消息队列具有写权限的进程可以向消息队列中按照一定的规则添加新消息;对消息队列有读权限的进程则可以从消息队列中读取消息。消息队列通过系统调用 msgget()、msgsnd()、msgrcv()来实现,函数的功能和实现过程分别如下。

    原型:int msgge(key_t key,int msgflg)  
    

    4.共享内存:共享内存是操作系统常采用的进程间通信方式。它使得多个进程可以访问同一块内存空间,不同进程可以及时看到对方进程中对共享内存中数据的更新。这种通信方式需要依靠某种同步机制,如互斥锁和信号量等。共享内存通过系统调用 shmget()、shmat()、shmdt()、shmctl()来实现,函数的功能和实现过程分别如下。

    系统调用:shmget()    
    
    原型:int	shmget(key_t key, int size, int shmflg)  
    
    

    源代码##

    #include <iostream>
    #include<stdio.h>
    #include<sys/ipc.h>
    #include<sys/msg.h>
    #include<sys/shm.h>
    #include<sys/types.h>
    #include<stdlib.h>
    #include<string.h>
    #include<unistd.h>
    #include<sys/wait.h>
    using namespace std;
    const int SIZE=1<<8;
    struct msgp{
          long long mtype;
          char mtext[250];
    }msg;
    const int SVKEY=75;
    int msgqid,pid,*pint,i;
    void pipeComm()
    {
    	pid_t pid;
    	int fd[2];
    	char buf[SIZE];
    	char msg[SIZE];
    	memset(buf, 0, SIZE);
    	memset(msg, 0, SIZE);
    	if (pipe(fd) < 0)
    	{
    		perror("pipe");
    		exit(1);
    	}
    	pid = fork();
    	if (pid == 0)//子
    	{
    		close(fd[0]);
    		sprintf(msg, "%d is sending a message to parent %d by PIPE
    ", getpid(), getppid());
    		write(fd[1], msg, strlen(msg));
    
    		exit(0);
    	}
    	else if (pid > 0)
    	{
    		close(fd[1]);
    
    		sleep(0);
    
    		read(fd[0], buf, SIZE);
    		printf("%s", buf);
    	}
    	else
    	{
    		perror("fork");
    		exit(1);
    	}
    }
    void client()
    {
       msgqid=msgget(SVKEY,0777);  //打开 75#消息队列
       pid=getpid();  //获取client进程标识符
       pint=(int *)msg.mtext;  //把正文的内容传给 pint,并强制转换类型
       *pint=pid;  //pint指针指向client进程标识符
       msg.mtype=1;  //消息类型为 1
       msgsnd(msgqid,&msg,sizeof(int),0);  //发送消息msg入msgqid消息队列
       msgrcv(msgqid,&msg,250,pid,0);  //从队列msgqid接收消息msg
       printf("client:receive reply from pid of  %d
    ",*pint);  //显示 server进程标识数
       exit(0);
    }
    void server()
    {
       msgqid=msgget(SVKEY,0777|IPC_CREAT);  //创建 75#消息队列
       msgrcv(msgqid,&msg,250,1,0);  //接收client进程标识数消息
       pint=(int *)msg.mtext;  //把正文的内容传给 pint,并强制转换类型
       pid=*pint;  //获得 cilent 进程标识数
       printf("server::serving for client  pid  of %d
    ",pid);
       msg.mtype=pid;  //消息类型为 client 进程标识数
       *pint=getpid();  //获取 server 进程标识数
       msgsnd(msgqid,&msg,sizeof(int),0);  //发送消息
       exit(0);
    }
    void msgQueueComm()
    {
       i=fork();  //创建进程 1
       if(!i)server();
      i=fork(); //创建进程 2
       if(!i) client();
       sleep(1);
    }
    void sharedMemComm(){
        int id;
        char *addr;
        char message[512];
        id=shmget(75,512,0777|IPC_CREAT);
        if(fork()==0){
            sprintf(message,"%d is sending message to parent",getpid());
            printf("%s
    ",message);
            addr=(char*)shmat(id,0,0);
            strcpy(addr,message);
            shmdt(addr);
         }else{
            //wait(0);
            addr=(char*)shmat(id,0,0);
            printf("%s
    ",addr);
            shmdt(addr);
            shmctl(id,IPC_RMID,0);
          }
    }
    int main()
    {
        cout<<"        进程通信程序        "<<endl;
        int choice;
        while(1)
        {
            cout<<"
    选择操作"<<endl;
            cout<<"1.管道通信"<<endl;
            cout<<"2.消息队列"<<endl;
            cout<<"3.共享储存区
    "<<endl;
            cin>>choice;
            switch (choice)
            {
            case 1:
                pipeComm();
                break;
            case 2:
                msgQueueComm();
                //wait(0);
                break;
            case 3:
                sharedMemComm();
                break;
            default:
                printf("INPUT ERROR!
    ");
                break;
            }
            sleep(1);
        }
        return 0;
    }
    
    
    
  • 相关阅读:
    jmeter_04_常用取样器
    jmeter_03_鉴权
    jmeter_02_目录文档说明
    jmeter_01_常用快捷键
    Web Api 与 Andriod 接口对接开发经验
    Eclipse自动生成作者、日期注释等功能设置
    c#解析XML到DATASET及dataset转为xml文件函数
    Jquery 仿 android Toast效果
    正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法
    异步网络加载开源框架AsyncHttpClient使用
  • 原文地址:https://www.cnblogs.com/gzr2018/p/11914270.html
Copyright © 2020-2023  润新知