linux命令unlink,linux下unlink函数的使用

在linux中有句话很实用,“有问题找男人”,这就是man的作用,man里面共有7个章节,这次经过学习,把对unlink的学习经验分享出来。

在linux下有很多很实用的函数,但对于一个具体的linux函数,我们在使用它的时候先是只需要知道这个函数需要包含哪个头文件、函数原型和它的参数、返回值等。

NAME     (名字)

unlink - delete a name and possibly the file it refers to

(unlink— 删去unlink指定名字的文件)SYNOPSIS  (大纲)

#include    (需要包含的头文件)

int unlink(const char *pathname);   (函数原型:有一个int型的返回值,有一个char * 的指针)

DESCRIPTION(描述:主要意思是从文件系统中删除一个指定名字的文件,并清空这个文件使用的可用的系统资源,如空间、进程等)       unlink() deletes a name from the file system.  If that name was       the last link to a file and no processes have the file open the       file  is  deleted  and the space it was using is made available       for reuse.       If the name was the last link to a file but any processes still       have  the file open the file will remain in existence until the       last file descriptor referring to it is closed.

RETURN VALUE(返回值:成功返回0;失败返回-1,其错误存在全局变量errno中,可用perror查看)       On success, zero is returned.  On error, -1  is  returned,  and       errno is set appropriately.

#define mplayer_cmd_fifo "/tmp/mplayer_cmd_fifo"        //定义命令命名管道路径#define mplayer_data_fifo "/tmp/mplayer_dat_fifo" //定义数据命名管道路径

int ret;//删除"/tmp/mplayer_cmd_fifo"下后残留的mplayer_cmd_fifounlink(mplayer_cmd_fifo);

//删除"/tmp/mplayer_dat_fifo"下残留的mplayer_dat_fifo      unlink(mplayer_data_fifo);    ret = mkfifo(mplayer_cmd_fifo, 0777);      if(ret < 0){perror("mkfifo cmd");exit(-1);}    ret = mkfifo(mplayer_data_fifo, 0777);if(ret < 0){perror("mkfifo data");exit(-1);}    bzero(player, sizeof(PLAYER));

这段程序是一个mplayer项目中进程间通信的一部分,创建两个命名管道,但在创建之前要先删除之前运行mplayer遗留下的管道文件,要不然会导致进程与mplayer通信失败,更深层次可以参阅下面这两篇文章,原理我想是一样的,虽然它们说的是ftok,希望对大家有所帮助,也谨记下我自己的学习历程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值