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; }
================================================================================================================================================================================================================================================================================================
NFS是Network File System的缩写,即网络文件系统。它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录。NFS客户端(一般为应用服务器,例如web)可以通过挂载(mount)的方式将NFS服务端共享的数据目录挂载到NFS客户端本地系统中(就是某一个挂载点下)。从NFS客户端的机器本地看,NFS服务端共享的目录就好像是客户自己的磁盘分区或者目录一样,而实际上确是远端的NFS服务端的目录。
NFS网络文件系统类似windows系统的网络共享、安全功能、网络驱动器映射,这也和linux系统里的samba服务类似。应用于互联网中小型集群架构后端作为数据共享,如果是大型网站,那么有可能还会用到更复杂的分布式文件系统,例如Moosefs(mfs)、glusterfs、FastDFS。