文件

lsof命令是什么?

可以列出被进程所打开的文件的信息。被打开的文件可以是

1.普通的文件,2.目录  3.网络文件系统的文件,4.字符设备文件  5.(函数)共享库  6.管道,命名管道 7.符号链接

8.底层的socket字流,网络socket,unix域名socket

9.在linux里面,大部分的东西都是被当做文件的…..还有其他很多

================================================================================================================================================================================================================================================================================================

有名管道(FIFO)
有名管道是持久稳定的。
它们存在于文件系统中。
FIFO比无名管道作用更大,因为他们能让无关联的进程之间交换数据。
管道文件一般用于交换数据。
复制代码
shell命令创建管道
一个shell命令可以建立有名管道
--mkfifo [option] name
--mkfifo创建一个名为name的有名管道
--mkfifo fifo1     创建一个有名管道fifo1
--mkfifo -m 666 fifo2    创建一个带权限的管道文件
--cat < fifo1      通过cat命令从fifo1中读取数据。
--ls > fifo1       将ls命令输出的结果写入fifo1中
复制代码
shell命令删除管道
--"rm name"
复制代码
代码创建管道fifo
mkfifo()函数是C语言库函数
int mkfifo(const char * pathname,mode_t mode);
参数pathname是文件名称,
参数mode是文件读写权限(读写权限是一位八进制数,例如0777(0表示八进制)表示当前用户,组用户,其他用户都拥有对该文件的可读,可写,可执行权限)
函数执行成功返回0,否则返回-1,并设置变量errno。
复制代码
代码删除管道
int unlink(const char *pathname);
unlink()函数是系统函数
函数执行成功返回0,否则返回-1,并设置变量errno。

 

复制代码
//代码创建有名管道
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int arg, char * args[])
{
    if(arg<2)
    {
        printf("请输入一个参数!\n");
        return -1;
    }
    int no=0;
    //创建有名管道
    no=mkfifo(args[1],0444);
    if(no==-1)
    {
        printf("创建管道失败!\n");
        return -1;
    }
    return 0;
}
复制代码

 

复制代码
//代码删除有名管道
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

int main(int arg, char * args[])
{
    if(arg<2)
    {
        printf("请输入一个参数!\n");
        return -1;
    }
    int no=0;
    //删除有名管道
    no=unlink(args[1]);
    if(no==-1)
    {
        printf("删除管道失败!\n");
        return -1;
    }
    return 0;
}
复制代码

 

复制代码
打开和关闭FIFO
int open(const char * pathname,int flags);
int close(int fd);
Linux的一切都是文件这一抽象概念的优势,打开和关闭FIFO和打开关闭一个普通文件操作是一样的。
FIFO的两端使用前都必须打开
open中如果参数flags为O_RDONLY将阻塞open调用,一直到另一个进程为写入数据打开FIFO为止。
相同的,O_WRONLY也导致阻塞一直到为读出数据打开FIFO为止。
复制代码

 

复制代码
//有名管道读文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int arg, char *args[])
{
    if (arg < 2)
    {
        printf("请输入一个参数!\n");
        return -1;
    }
    int fd = 0;
    char buf[100] = { 0 };
    //打开管道文件
    fd = open(args[1], O_RDONLY);
    if (fd == -1)
    {
        printf("open the file failed ! error message : %s\n", strerror(errno));
        return -1;
    }
    while (read(fd, buf, sizeof(buf)) > 0)
    {
        printf("%s", buf);
        memset(buf, 0, sizeof(buf));
    }
    //close the file stream
    close(fd);
    return 0;
}
复制代码

 

复制代码
//管道写文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int arg,char * args[])
{
    if(arg<2)
    {
        printf("请输入一个参数!\n");
        return -1;
    }
    int fd=0;
    char buf[100]={0};
    fd=open(args[1],O_WRONLY);
    if(fd==-1)
    {
        printf("open the file failed ! error message :%s\n",strerror(errno));
        return -1;
    }
    while(1)
    {
        //从键盘上读取数据
        read(STDIN_FILENO,buf,sizeof(buf));
        if(buf[0]=='0')
        {
            break;
        }
        //写入管道文件中
        write(fd,buf,strlen(buf));
        memset(buf,0,sizeof(buf));
    }
    //close the file stream
    close(fd);
    return 0;
}
复制代码


================================================================================================================================================================================================================================================================================================

字符设备是指在I/O传输过程中以字符为单位进行传输的设备,例如键盘,打印机等。在UNIX系统中,字符设备以特别文件方式在文件目录树中占据位置并拥有相应的结点。
字符设备可以使用与普通文件相同的文件操作命令对字符设备文件进行操作,例如打开、关闭、读、写等
================================================================================================================================================================================================================================================================================================

NFS是Network File System的缩写,即网络文件系统。它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录。NFS客户端(一般为应用服务器,例如web)可以通过挂载(mount)的方式将NFS服务端共享的数据目录挂载到NFS客户端本地系统中(就是某一个挂载点下)。从NFS客户端的机器本地看,NFS服务端共享的目录就好像是客户自己的磁盘分区或者目录一样,而实际上确是远端的NFS服务端的目录。

NFS网络文件系统类似windows系统的网络共享、安全功能、网络驱动器映射,这也和linux系统里的samba服务类似。应用于互联网中小型集群架构后端作为数据共享,如果是大型网站,那么有可能还会用到更复杂的分布式文件系统,例如Moosefs(mfs)、glusterfs、FastDFS。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值