实验五 进程间通信实验(二)
实验目的:
1. 通过基础实验,基本掌握无名管道、有名管道的程序设计。
2. 通过编写程序,使读者掌握信号处理程序设计方法。
实验内容:
1. 无名管道程序设计:在父进程中创建一个无名管道,并创建子进程;在父进程中写该管
道,并用子进程将内容读出。
2. 有名管道程序设计:创建两个进程,在 A 进程中创建一个有名管道,并向其写入数据,
通过 B 进程从有名管道中读出数据。
3. 信号处理程序设计:在进程中为 SIGBUS 注册处理函数,并向该进程发送 SIGBUS 信号
来触发信号处理函数。
实验过程:
(一)无名管道程序设计
实验代码:
/* pipe.c */
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DATA_LEN 256
#define DELAY_TIME 1
int main()
{
pid_t pid;
int pipe_fd[2];
char buf[MAX_DATA_LEN];
const char data[] = "Pipe Test Program";
int real_read, real_write;
memset((void*)buf, 0, sizeof(buf));
/* 创建管道 */
if (pipe(pipe_fd) < 0)
{