实验五 进程通信-管道通信

1. 函数int pipe(int fd[2])创建一个管道,管道两端可分别用描述字fd[0]以及fd[1]来描述。需要注意的是,管道的两端是固定了任务的。即一端只能用于读,由描述字fd[0]表示,称其为管道读端;另一端则只能用于写,由描述字fd[1]来表示,称其为管道写端。下面给出的程序使用系统调用pipe()建立一条管道线,两个子进程p1和p2分别向管道各写一句话:child1 is sending a message!和child2 is sending a message!,父进程则从管道中读出来自子进程的信息,并显示在屏幕上。请读懂程序并调试运行。

#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include<stdlib.h>
int pid1, pid2;
int main() {
	int fd[2];
	int pid1, pid2;
	char OutPipe[100], InPipe[100];

	pipe(fd);

	while ((pid1 = fork()) == -1);
	if (pid1 == 0) {
		printf("child process1 %d\n", getpid());
		lockf(fd[1], 1, 0);
		sprintf(OutPipe, "child1 is sending a message!");
		write(fd[1], OutPipe, 50);
		sleep(5);
		lockf(fd[1], 0, 0);
		exit(0);
	}
	else {
		while ((pid2 = fork()) == -1);
		if (pid2 == 0) {
			printf("child process2 % d\n", getpid());
			lockf(fd[1], 1, 0);
			sprintf(OutPipe, "child2 is sending a message!");
			write(fd[1], OutPipe, 50);
			sleep(5);
			lockf(fd[1], 0, 0);
			exit(0);
		}
		else {
			printf("parent process %d\n", getpid());
			wait(0);
			read(fd[0], InPipe, 50);
			printf("%s\n", InPipe);
			wait(0);
			read(fd[0], InPipe, 50);
			printf("%s\n", InPipe);
			exit(0);
		}
	}
}

阅读并运行程序并回答以下问题:
问题1:该程序中使用的管道是有名管道还是无名管道?程序中红色部分的含义是什么?
无名管道。锁定管道的写入端,从当前位置开始锁定。
问题2:程序的含义是什么?运行结果是什么?为什么?
这段代码创建了两个子进程,并使用管道来实现进程间通信。
运行结果:
在这里插入图片描述
出现该运行结果可能的原因是:
• 在程序运行时,两个子进程之间没有竞争管道写入权的情况发生。
• 在程序运行时,第一个子进程比第二个子进程先写入管道。

2.使用管道通信时,可关闭某些不需要的读或写描述符,建立起单向的读或写管道,然后用read和write像操作文件一样去操作它。下面给出的程序中子进程通过管道向父进程发送数据,这里子进程只使用到管道的写端口fd[1]、父进程使用到了fd[0],因此可关闭子进程的fd[0]和父进程的fd[1]。请通过程序体会。

#include<sys/types.h> 
#include<unistd.h> 
#include<stdio.h> 
#include<stdlib.h> 
#include<errno.h> 
#include<memory.h>  
int main()
{
    char* msg = "I am child process!";
    /*子进程发送的数据*/
    pid_t pid;
    char buf[100];
    /*用于读取*/
    int pi;
    /*创建管道时的返回值*/
    int fd[2];
    /*创建管道的参数*/

    memset(buf, 0, sizeof(buf));
    /*设置buf数组全为0,需*/
    pi = pipe(fd);         					/*要引入#include<memory.h>*/
    if (pi < 0)
    {
        perror("pipe() error!");
        exit(0);
    }
    if ((pid = fork()) == 0)
        /*child process*/
    {
        close(fd[0]);
        /*关闭读管道*/
        if (write(fd[1], msg, 20) != -1)
            /*写入管道*/
            printf("child process write success!\n");
        close(fd[1]);
        /*关闭写管道*/
    }
    else if (pid > 0)					/*parent process*/
    {
        close(fd[1]);
        /*关闭写管道*/
        sleep(2);
        /*休眠一下等待数据写入*/
        if (read(fd[0], buf, 100) > 0)/*写入管道*/
            printf("Message from the pipe is:%s\n", buf);
        close(fd[0]);/*关闭读管道*/
        waitpid(pid, NULL, 0);
        /*待pid进程退出,此处pid为子进程*/
        exit(0);
    }
    else
    {
        perror("fork() error!");
        exit(0);
    }
}

在这里插入图片描述

3.普通管道只能用于一个进程家族之间的通信,如父子,兄弟之间,并且普通管道在于内存中,随着进程的结束而消失;而命名管道是有“名字”的管道,存在于磁盘上,作为一个特殊的设备文件而存在,不会随着进程结束而消失。有名管道可用于两个无关的进程之间的通信,实现函数是mkfifo()。下面的程序实例演示了mkfifo的使用。请先以超级用户身份登录系统,然后编辑/编译源程序(两个*.c程序),在图形终端上执行读程序readfifo.c,读程序执行后将陷入循环;切换到字符终端1(ctrl+alt+f1),以超级用户身份登录并执行写程序writefifo.c,然后回到图形终端,观察读程序的输出变化。

readfifo.c:

#include<sys/types.h> 
#include<unistd.h> 
#include<stdio.h> 
#include<stdlib.h> 
#include<errno.h> 
#include<fcntl.h> 
#include<sys/stat.h> 
#include<memory.h> 
#define FIFO "/home/ghj/myfifo" /*使用宏定义路径*/ 

int main()
{
    int fd;/*指向命名管道*/
    char buf[100];/*存储数据*/

    if (mkfifo(FIFO, O_CREAT | O_EXCL) < 0) /*创建管道*/
    {
        perror("Create error!\n");
        unlink(FIFO);/*清除管道*/
        exit(0);
    }
    fd = open(FIFO, O_RDONLY | O_NONBLOCK, 0);/*打开管道*/
    if (fd < 0) {
        perror("Create error!\n");
        unlink(FIFO);
        exit(0);
    }
    while (1) {
        memset(buf, 0, sizeof(buf));/*清空buf数组*/
        if (read(fd, buf, 100) > 0)/*读取管道*/
        {
            printf("Get message:%s\n", buf);
        }
        else {
            printf("Not accept any message!\n");
        }
        sleep(1);/*休眠*/
    }
}

writefifo.c:

#include<sys/types.h> 
#include<unistd.h> 
#include<stdio.h> 
#include<stdlib.h> 
#include<errno.h> 
#include<fcntl.h> 
#include<sys/stat.h> 
#define FIFO "/home/ghj/myfifo"/*宏定义命名管道路径*/ 

int main()
{
    char* msg = "Some message!";/*发送数据*/
    int fd;

    fd = open(FIFO, O_WRONLY | O_NONBLOCK, 0);/*打开*/
    if (write(fd, msg, 20) != -1)/*写信息*/
        printf("Message have been send to FIFO\n");
    exit(0);
}

本地运行结果出了点问题,这里就不展示运行结果图了。

4.编写两个程序client.c和server.c,分别用于消息的发送和接收。

client.c:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#define MSGKEY 75
struct msgform{
    long mtype;
    char mtext[1000];
}msg;
int msgqid;
void client(){
    int i;
    msgqid = msgget(MSGKEY, 0777);      /*打开75#消息队列*/
    for (i = 10; i >= 1; i--){
        msg.mtype = i;
        printf("(client)sent\n");
        msgsnd(msgqid, &msg, 1024, 0);    /*发送消息*/
    }
    exit(0);
}
int main(){
    client();

    return 0;
}

server.c:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#define MSGKEY 75
struct msgform{
    long mtype;
    char mtext[1000];
}msg;
int msgqid;
void server(){
    msgqid = msgget(MSGKEY, 0777 | IPC_CREAT);   /*创建75#消息队列*/
    do{
        msgrcv(msgqid, &msg, 1030, 0, 0);         /*接收消息*/
        printf("(server)received\n");
    } while (msg.mtype != 1);
    msgctl(msgqid, IPC_RMID, 0);     /*删除消息队列,归还资源*/
    exit(0);
}
int main(){
    server();
    
    return 0;
}

建议该题目的运行方法为:
将上述两个程序分别编译为server和client,并按以下方式执行:
./server & /*当在前台运行某个作业时,终端被该作业占据;而在后台运行作业时,它不会占据终端。可以使用&命令把作业放到后台执行。该命令的一般形式为:命令&*/
ipcs –q /* 输出有关信息队列(message queue)的信息*/
./client

阅读并运行程序并回答以下问题:
问题1:运行结果是什么?该程序为什么需要在后台运行server.c?若不如此会出现什么现象?为什么?
在这里插入图片描述
将服务器程序放在后台运行,会在客户端程序发送消息之前已经启动并创建了消息队列。如果不这样做,客户端程序可能会尝试打开一个不存在的消息队列,从而导致程序出错。
如果不将服务器程序放在后台运行,客户端程序可能会在服务器程序启动之前就尝试打开消息队列,导致错误。
问题2:两个程序的含义是什么?请解释其运行结果的含义?
server.c 和 client.c 两个程序分别实现了一个服务器和一个客户端,它们之间通过消息队列进行通信。
运行结果的含义是,服务器程序和客户端程序之间已经成功建立了连接,并且客户端程序已经发送了 10 条消息,服务器程序已经接收到了这些消息。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Re:从零开始的代码生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值