unix学习笔记-------利用dup()函数来达到共享同一个文件表项的作用

来源:互联网 发布:sftp端口号 编辑:程序博客网 时间:2024/06/03 15:09

dup函数

 

源代码如下:

 

/*

/*

         dup()函数的使用:

         1.dup()函数的作用是什么???

            利用dup()函数来达到共享同一个文件表项的作用。

             (个人):

                     因为一个文件描述符指向一个打开文件文件对象,所以,如果俩个文件描述符的内容是相同的话,那么这两个文件描述符指向的是同一个文件表项。

                     即:指向的是同一个文件。

                     所以:dup()的作用就是: 把一个文件描述符的内容复制到另一个新的文件描述符上去。

                    

           

         2.函数原型:

                            /*

                                     DUP(2)                     Linux Programmer鈥檚Manual                    DUP(2)

 

NAME

      dup, dup2, dup3 - duplicate a file descriptor

                                                        /*

                                                                 remark:  duplicate  报错

 英 [ˈdjuːplɪkeɪt]   美 ['duplɪket]  跟读 口语练习

vt. 复制;使加倍

n. 副本;复制品

                                                       

                                                        */

                                                       

SYNOPSIS

      #include <unistd.h>

 

      int dup(int oldfd);

      int dup2(int oldfd, int newfd);

 

      #define _GNU_SOURCE

      #include <unistd.h>

 

      int dup3(int oldfd, int newfd, int flags);

 

DESCRIPTION

      These system calls create a copy of the file descriptor oldfd.

 

      dup() uses the lowest-numbered unused descriptor for the new descriptor.

 

      dup2() makes newfd be the copy of oldfd, closing newfd first ifnecessary, but note the following:

 

      *  If oldfd is not a valid filedescriptor, then the call fails, and newfd is not closed.

 

      *  If oldfd is a valid filedescriptor, and newfd has the same value as oldfd, then dup2() does nothing,and returns newfd.

 

      After  a  successful return  from one of these systemcalls, the old and new file descriptors may be used interchangeably.  They refer to the same open file description

      (see open(2)) and thus share file offset and file status flags; forexample, if the file offset is modified by using lseek(2) on one of thedescriptors,  the  offset is

      also changed for the other.

 

      The  two descriptors do not sharefile descriptor flags (the close-on-exec flag). The close-on-exec flag (FD_CLOEXEC; see fcntl(2)) for the duplicatedescriptor is off.

 

      dup3() is the same as dup2(), except that:

 

      *  The caller can force theclose-on-exec flag to be set for the new file descriptor by specifyingO_CLOEXEC in flags.  See the descriptionof the same flag  in  open(2)

         for reasons why this may be useful.

 

      *  If oldfd equals newfd, thendup3() fails with the error EINVAL.

 

RETURN VALUE

      On success, these system calls return the new descriptor.  On error, -1 is returned, and errno is setappropriately

                           

                            */

*/

*/

#include<unistd.h>

#include<stdio.h>

#include<sys/types.h>

#include<fcntl.h>

 

int main()

{

         //usethe open () function

         intfd_t_1;

         intfd_t_new;

         charbuf[50];

      fd_t_1 =open("/home/code/file1/my.txt",O_RDWR,775);

     if(fd_t_1<0)

         {

                   printf("openerror\n");

                   _exit(0);

        

         }

else

         printf("thefile descriptor is : %d \n",fd_t_1);

//call the dup()

 

fd_t_new =dup(fd_t_1);

printf("the new file descriptor is :%d\n",fd_t_new);

//the new descriptor is for

 

 

int rd_t=read(fd_t_new,&buf,20);

if(rd_t <0)

{

         perror("readerror \n");

         exit(0);

 

}

printf("the contents of reading is :\n");

printf("%s\n",buf);

return 0;

 

        

 

}

 

 

 

 

 

 

 

 

 

 

 

运行截图:

 

 

 

 

0 0
原创粉丝点击