分享

linux下unlink函数的使用

 wusiqi111 2017-10-27 发布于四川

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

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


NAME     (名字)

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

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


SYNOPSIS  (大纲)

       #include <unistd.h>    (需要包含的头文件)


       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_fifo
unlink(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条评论

    发表

    请遵守用户 评论公约

    类似文章 更多