环境:Vmware Workstation;CentOS-6.4-x86_64
说明:
1、创建管道的时候,使用的函数:int mkfifo(const char *pathname, mode_t mode);
2、删除管道的时候,使用的函数:int unlink(const char *path);
3、这两个函数的返回值,如果是-1,说明失败。
步骤:
1、编写makefile文件:
.SUFFIXES:.c .o
CC=gcc
SRCS=main.c
OBJS=$(SRCS:.c=.o)
EXEC=main
start: $(OBJS)
$(CC) -o $(EXEC) $(OBJS)
@echo "-----------------------------OK-----------------------"
.c.o:
$(CC) -Wall -o $@ -c $<
clean:
rm -rf $(EXEC) $(OBJS)2、编写源文件main.c:
#include #include #include #include #include #include int main(int argc, char *args[])
{
// 定义管道的名称
char *fifoname = "fifo";
// 创建一个有名管道,第一个参数是管道的名字,第二个参数是管道的权限
int mk_fifo = mkfifo(fifoname, 0666);
// 判断管道是否创建成功
if (mk_fifo == -1)
{
printf("mkfifo %s failed : %s\n", fifoname, strerror(errno));
return -1;
}
else
{
printf("mkfifo %s success : %s\n", fifoname, strerror(errno));
}
// 删除一个有名管道,参数是管道的名称
int del_fifo = unlink(fifoname);
// 判断是否成功删除管道
if (del_fifo == -1)
{
printf("unlink %s failed : %s\n", fifoname, strerror(errno));
return -1;
}
else
{
printf("unlink %s success : %s\n", fifoname, strerror(errno));
}
return 0;
}
3、编译并执行程序:
[negivup@negivup mycode]$ make
gcc -Wall -o main.o -c main.c
gcc -o main main.o
-----------------------------OK-----------------------
[negivup@negivup mycode]$ main
mkfifo fifo success : Success
unlink fifo success : Success
PS:根据传智播客视频学习整理得出。