有名,即文件系统中存在对应文件节点,每个文件节点都有一个inode号。
open: 普通文件
mkfifo: 创建管道
socket:创建套接字
mknod:字符设备文件
mkdir: 目录文件
mkfifo函数: 创建管道文件
函数形式:int mkfifo(const char * filename, mode_t mode);
参数:管道文件名;权限,文件权限仍然和umask有关
返回:成功返回0,失败返回-1
创建有名管道mkfifo,打开open,读read,写write,关闭close。
非父子进程间通信
第一个进程:
#include "unistd.h"
#include "stdio.h"
#include "sys/types.h"
#include "stdlib.h"
#include "fcntl.h"
int main()
{
int fd;
int i;
char process_inter=0;
fd=open("./myfifo",O_WRONLY);
if(fd <0)
{
printf("open myfifo failure\n");
return -1;
}
printf("open myfifo sucess\n");
for(i=0;i<5;i++)
{
printf("this is first process i=%d\n",i);
usleep(100);
}
process_inter=1;
sleep(5);
write(fd,&process_inter,1);
while(1);
return 0;
}
运行结果
open myfifo sucess
this is first process i=0
this is first process i=1
this is first process i=2
this is first process i=3
this is first process i=4
第二个进程:
#include "unistd.h"
#include "stdio.h"
#include "sys/types.h"
#include "stdlib.h"
#include "fcntl.h"
int main()
{
int fd;
int i;
int process_inter=0;
fd=open("./myfifo",O_RDONLY);
if(fd <0)
{
printf("open myfifo failure\n");
return -1;
}
printf("open myfifo sucess\n");
read(fd,&process_inter,1);
while(process_inter == 0);
for(i=0;i<5;i++)
{
printf("this is second process i=%d\n",i);
usleep(100);
}
while(1);
return 0;
}
运行结果
open myfifo sucess
this is second process i=0
this is second process i=1
this is second process i=2
this is second process i=3
this is second process i=4