操作系统-管道通信

编写程序,演示多进程并发执行和进程软中断、管道通信。

父进程使用系统调用pipe( )建立一个管道,然后使用系统调用fork()创建两个子进程,子进程1和子进程2;子进程1每隔1秒通过管道向子进程2发送数据:I send you x times. (x初值为1,每次发送后做加一操作)子进程2从管道读出信息,并显示在屏幕上。父进程用系统调用signal()捕捉来自键盘的中断信号(即按Ctrl+C键);当捕捉到中断信号后,父进程用系统调用Kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止:

`Child Process l is Killed by Parent!
Child Process 2 is Killed by Parent!`

父进程等待两个子进程终止后,释放管道并输出如下的信息后终止

Parent Process is Killed!
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define N 50
void stop1(),stop2();
int p1,p2;  // 子进程P1,P2
int fd[2];  //无名管道
char buf[N];//消息
void main()
{
	if(pipe(fd)<0)//创建管道
	{
		perror("Fail to pipe");//创建失败,输出提示
		exit(EXIT_FAILURE);
	}
	if(p1=fork())//创建子进程P1
	{
		if(p2=fork())//创建子进程P2
		{	
			signal(SIGINT,stop1);//等待接受SIGINT信号量
			wait(0);//等待P1进程关闭
			wait(0);//等待P2进程关闭
			printf("parent process is killed!\n");//输出父进程关闭
                        close(fd[0]);
                        close(fd[1]);
			exit(0);//退出父进程
	         }
			else
		{
			
			close(fd[1]);	//关闭写管道
			int n=0;
			signal(SIGINT,SIG_IGN);//忽略SIGINT信号
			signal(SIGUSR1,stop2);//接受kill信号,执行stop
			while(1)
			{
			n=read(fd[0],buf,sizeof(buf)+1);//读取管道中的信息
			buf[n]='\0';//添加结束标记
			printf("child %d receive:'%s.'\n",getpid(),buf);//输出读取的信息
		       }
		}
	}
	else
	{
		int i=0;
		close(fd[0]);//关闭读管道
		signal(SIGUSR1,stop2);//接受kill信号,执行stop
		signal(SIGINT,SIG_IGN);//忽略SIGINT信号
		while(1){
		sprintf(buf,"child %d send  %d time",getpid(),i);//写入写入次数到buf
		buf[strlen(buf)]='\0';//添加结束标记
		write(fd[1],buf,strlen(buf)+1);//将数据写入管道
		sleep(1);//等待1s
		i++;
	}
	}
}
void stop1()  // signal handler of parent process 
{
kill(p1,SIGUSR1);
kill(p2,SIGUSR1);
}
void stop2()  // signal handler of parent process 
{
        close(fd[0]);
        close(fd[1]);
//lockf(1,1,0); //锁定
        printf("child %d to exit!\n",getpid());
//lockf(1,0,0);//解锁
	exit(0);
}

实验结果

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
编写一个程序使用系统调用fork生成3个子进程并使用系统调用pipc创建一个管道,使得子进程之间可以通过管道进行通信。 首先,需要包含头文件<sys/types.h>、<sys/stat.h>、<fcntl.h>、<unistd.h>和<stdio.h>来使用相关的系统调用。 程序的主函数如下所示: ```c #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> int main() { pid_t pid[3]; int fd[2]; // 创建管道 if (pipe(fd) < 0) { perror("创建管道失败"); return 1; } // 创建子进程 for (int i = 0; i < 3; i++) { pid[i] = fork(); if (pid[i] < 0) { perror("创建子进程失败"); return 1; } else if (pid[i] == 0) { // 子进程从管道中读取数据 close(fd[1]); // 关闭写端 char buffer[256]; read(fd[0], buffer, sizeof(buffer)); printf("子进程%d收到的数据:%s\n", i + 1, buffer); return 0; } } // 父进程向管道中写入数据 close(fd[0]); // 关闭读端 char message[] = "Hello, child processes!"; write(fd[1], message, sizeof(message)); return 0; } ``` 在程序中,首先使用系统调用pipe(fd)创建一个管道,fd[0]表示读端,fd[1]表示写端。然后使用for循环创建3个子进程,并在子进程中使用read系统调用从管道中读取数据,并输出接收到的消息。最后,在父进程中使用write系统调用向管道中写入数据。 需要注意的是,在父进程中,需要通过close系统调用关闭不使用的文件描述符,这里关闭了管道的读端fd[0],以及在子进程中关闭了管道的写端fd[1]。 这样,运行程序后,父进程向管道中写入数据,然后每个子进程从管道中读取数据并输出,实现了子进程之间通过管道进行通信。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

醉后才知酒浓

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

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

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

打赏作者

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

抵扣说明:

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

余额充值