posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          APUE - File I/O (5)

          Posted on 2007-08-22 15:46 ZelluX 閱讀(372) 評論(0)  編輯  收藏 所屬分類: LinuxC/C++
          1. dup和dup2函數
          #include <unistd.h>
          int dup(int filedes);
          int dup2(int filedes, int filedes2);
          // Both return: new file descriptor if OK, -1 on error

          dup返回的file descriptor(以下簡稱fd)為當前可用的最低號碼,dup2則指定目的fd,如果該fd已被打開,則首先關閉這個fd。
          dup后兩個fd指向相同的file table entry,這意味著它們共享同一個的file status flag, read, write, append, offset等。

          事實上,dup等價于
          fcntl(filedes, F_DUPFD, 0);

          dup2和也類似于
          close(filedes2);
          fcntl(filedes, F_DUPFD, filedes2);
          但這不是一個原子操作,而且errno也有一定的不同。
          主站蜘蛛池模板: 康平县| 金湖县| 屯门区| 仪陇县| 揭阳市| 宁阳县| 明溪县| 梧州市| 礼泉县| 同德县| 建宁县| 东明县| 江都市| 仁寿县| 大竹县| 夏邑县| 慈溪市| 板桥市| 溧水县| 铅山县| 淮安市| 灵璧县| 那曲县| 红原县| 洞头县| 民勤县| 筠连县| 桦南县| 深州市| 凤凰县| 宽城| 兰溪市| 雅安市| 高邑县| 会理县| 宣威市| 渭源县| 疏勒县| 渑池县| 台前县| 蒲城县|