特点:
1、有名管道用于两个互不相干进程间的通信
2、创建有名管道根据文件名、权限指出
3、有名管道在文件系统中可见,但内容存放在内存中
4、有名管道用文件IO操作
5、有名管道遵循先进先出的原则
6、不支持lseek操作
我遇到的问题:
1、在指定目标文件权限时写成了竖线 0666。
2、在源文件、目标文件或有名管道进行容错处理时忘记 返回值的问题。
有名管道的应用——两个进程之间通信,复制文件的功能
read.c
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
ssize_t s;
char buf[32]={0};
mkfifo("./fifo", 0666);
if (mkfifo("./fifo", 0666) < 0)
{
if (errno == EEXIST)
{
printf("exiet\n");
}
else
{
perror("mkfifo err\n");
return -1;
}
}
printf("mkfifo success\n");
int fd=open("./fifo",O_WRONLY);
int fd1=open(argv[1],O_RDONLY);
if(fd<0)
{
perror("open fifo err\n");
return -1;
}
if(fd1<0)
{
perror("open src err\n");
return -1;
}
while(1)
{
s=read(fd1,buf,32);
if(s == 0)
break;
write(fd,buf,s);
}
return 0;
}
write.c
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
ssize_t s;
char buf[32]={0};
mkfifo("./fifo", 0666);
if (mkfifo("./fifo", 0666) < 0)
{
if (errno == EEXIST)
{
printf("exiet\n");
}
else
{
perror("mkfifo err\n");
return -1;
}
}
printf("mkfifo success\n");
int fd=open("./fifo",O_RDONLY);
int fd1=open(argv[1],O_WRONLY | O_CREAT | O_TRUNC,0666);
if(fd<0)
{
perror("open fifo err\n");
return -1;
}
if(fd1<0)
{
perror("open src err\n");
return -1;
}
while(1)
{
s=read(fd,buf,32);
if(s == 0)
break;
write(fd1,buf,s);
}
close(fd);
close(fd1);
return 0;
}