Unix学习笔记----编程实例-------Link函数的实例使用:

         Link函数的实例使用:

 

分析:

/*

         我们首先要解决几个问题:

         1.什么是一个文件的硬链接??

         2.什么是软连接??? 硬链接和软链接的区别是什么???

         3.link()函数的实质是不是创建一个硬链接??

         ::::::::::::::::::::

        

         硬链接只能引用同一文件系统中的文件它引用的是文件在文件系统中的物理索引(也称为 inode)。

                   当您移动或删除原始文件时,硬链接不会被破坏,因为它所引用的是文件的物理数据而不是文件在文件结构中的位置。

                   硬链接实际上是为文件建一个别名,链接文件和原文件实际上是同一个文件。可以通过ls -i来查看一下,这两个文件的inode号是同一个,说明它们是同一个文件

                  

         而软链接建立的是一个指向,即链接文件内的内容是指向原文件的指针,它们是两个文件。

        

         (个人总结)

                   :::

                            硬链接就是建立一个新的文件名,只不过这个文件名指向的iNode就是原文件的inode.而不是新创建一个inode,(只要inode相同,他们就是同一个文件)

                            而软链接是建立一个新的文件,它会创建一个新的inode节点,该inode节点的内容是指向原文件的指针。

                            因此:

                            软链接其实就是快捷方式。

                             

                             

                            硬链接就是在本文件系统中新增加一个文件名,但该文件名还是对应一个已经存在的文件的inode节点。

                           

                           

                           

        

         link()函数的原型如下:

        

         ::::::::::::::::::::::::::::::::

                   link- make a new name for a file

 

SYNOPSIS

      #include <unistd.h>

 

      int link(const char *oldpath, const char *newpath);

 

DESCRIPTION

      link() creates a new link (also known as a hard link) to an existing file.

 

      If newpath exists it will not be overwritten.

 

      This new name may be used exactly as the old one for any operation; bothnames refer to the same file (and so have the same permissions and ownership)and it is impossi-

      ble to tell which name was the "original".

 

RETURN VALUE

      On success, zero is returned.  Onerror, -1 is returned, and errno is set appropriately.

 

           

           

        

        

         //从以上,我们可以看出,link()函数的实质就是建立一个硬链接。

         即:把原文件的inode节点,copy到新的文件名的inode结构体中。

        

        

        

        

*/

 

 

 

 

 

 

/*

         事后分析:

         发现:

                   对open()函数的使用存在欠缺:

                   例子:

                   int  fd_t=open("/home/code/file1/my.txt",O_RDWR);

                  

 

*/

 

 

 

源代码如下:

#include<unistd.h>

#include<stdio.h>

#include<errno.h>

#include<fcntl.h>

#include<sys/types.h>

#include<sys/stat.h>

 

int main()

{

         intfd_t_1;

         intfd_t_2;

         charbuf[50];

                   buf[49]='\n';

 

        

//create a new link of my.txt

         intlink_t=link("/home/code/file1/my.txt","/home/code/file2/thsamemy.txt");

         if(link_t<0)

         {

           perror("link error");

           _exit(0);

 

         }

  else

         printf("theold file content is : \n");

         if(fd_t_1=open("/home/code/file1/my.txt",O_RDWR)>0)

          {

                   if(read(fd_t_1,&buf,49)>0)

                            write(1,&buf,50);

 

          }

 

         printf("nowwe will to print the new link' content...\n");

         if(fd_t_2=open("/home/code/file2/thesamemy.txt",O_RDWR)>0)

         {

                   printf("thenew file decripto is :%d \n",fd_t_2);

 

                   if(read(fd_t_2,&buf,49)>0)

                       write(1,&buf,50);

         }

 

return 0;

 

          

        

}

 

 

运行结果图:

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值