程序:进程间通信——无名管道实例
2017-03-19 00:12
274 查看
无名管道由pipe()函数创建:
int pipe(int filedis[2]);
当一个管道建立时,它会创建两个文件描述符:
filedis[0] 用于读管道, filedis[1] 用于写管道(不固定,也可倒过来)
通常先创建一个管道,再通过fork函数创建一个子进程,该子进程会继承父进程所创建的管道
必须在系统调用fork( )前调用pipe( ),否则子进程将不会继承文件描述符
以下程序为父进程传递字母给子进程,子进程将其转化为大写再传给父进程
#include<stdio.h>
#include<ctype.h>
#include<wait.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int fd1[2]; //创建管道文件描述符
int pid;
char str[20];
if (pipe(fd1) < 0) //创建管道
{
printf("create pipe error");
exit(-1);
}
int fd2[2];
if (pipe(fd2) < 0)
{
printf("create pipe error");
exit(-1);
}
if ((pid = fork()) <0) //创建子进程, 用fork创建,vfork创建的子进程运行结束再执行父进程
{
printf("create pid error");
}
else if (pid == 0) //pid=0 为子进程
{
close(fd1[1]); //关闭第一个通道的写端和第二通道的读端
close(fd2[0]);
sleep(2);
read(fd1[0],str,5); //从第一个通道读取内容
for (int i = 0; i < 5; i++)
{
if (str[i] > 'a' && str[i] < 'z')
{
str[i] = toupper(str[i]); 将读取的内容转化为大写
}
}
write(fd2[1],str,5); //再写进通道二
close(fd1[0]); //将两个通道关闭
close(fd2[1]);
exit(0);
}
else
{
close(fd1[0]); //在父进程关闭第一通道的读端和第二通道的写端
close(fd2[1]);
write(fd1[1],"hello",5); //在第一通道写
read(fd2[0],str,5); //在第二通道读
printf("%s",str);
waitpid(pid,NULL,0); //等到子进程结束
close(fd1[1]); //关闭通道
close(fd2[0]);
}
return 0;
}
无名管道由pipe()函数创建:
int pipe(int filedis[2]);
当一个管道建立时,它会创建两个文件描述符:
filedis[0] 用于读管道, filedis[1] 用于写管道(不固定,也可倒过来)
通常先创建一个管道,再通过fork函数创建一个子进程,该子进程会继承父进程所创建的管道
必须在系统调用fork( )前调用pipe( ),否则子进程将不会继承文件描述符
以下程序为父进程传递字母给子进程,子进程将其转化为大写再传给父进程
#include<stdio.h>
#include<ctype.h>
#include<wait.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int fd1[2]; //创建管道文件描述符
int pid;
char str[20];
if (pipe(fd1) < 0) //创建管道
{
printf("create pipe error");
exit(-1);
}
int fd2[2];
if (pipe(fd2) < 0)
{
printf("create pipe error");
exit(-1);
}
if ((pid = fork()) <0) //创建子进程, 用fork创建,vfork创建的子进程运行结束再执行父进程
{
printf("create pid error");
}
else if (pid == 0) //pid=0 为子进程
{
close(fd1[1]); //关闭第一个通道的写端和第二通道的读端
close(fd2[0]);
sleep(2);
read(fd1[0],str,5); //从第一个通道读取内容
for (int i = 0; i < 5; i++)
{
if (str[i] > 'a' && str[i] < 'z')
{
str[i] = toupper(str[i]); 将读取的内容转化为大写
}
}
write(fd2[1],str,5); //再写进通道二
close(fd1[0]); //将两个通道关闭
close(fd2[1]);
exit(0);
}
else
{
close(fd1[0]); //在父进程关闭第一通道的读端和第二通道的写端
close(fd2[1]);
write(fd1[1],"hello",5); //在第一通道写
read(fd2[0],str,5); //在第二通道读
printf("%s",str);
waitpid(pid,NULL,0); //等到子进程结束
close(fd1[1]); //关闭通道
close(fd2[0]);
}
return 0;
}
相关文章推荐
- 传统的进程间通信方式 1.无名管道(pipe) 2.有名管道(fifo) 3.信号(signal); 闹钟信号应用实例
- 程序:进程间通信——有名管道实例
- 进程间通信实例(IPC)之命名管道
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)
- Linux编程练习 --进程间通信1--无名管道
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL
- C语言无名管道实现父子进程间通信
- 以命名管道方式实现网络进程间通信的一个实例
- UNIX/Linux进程间通信IPC---管道--全总结(实例入门)
- 无名管道(pipe)使用实例
- Linux编程—进程间通信—无名管道(1)
- 进程间通信系列 之 命名管道FIFO及其应用实例
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL
- EXE程序间通信利器--命名管道(1):基础知识及管道实例
- 以命名管道方式实现网络进程间通信的一个实例
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server
- Linux进程间通信(二)---管道通信之无名管道及其基础实验