1. 程式人生 > >Linux-C標準輸入輸出

Linux-C標準輸入輸出

STDIO

  #include <stdio.h>
  #include <unistd.h>
 
  int main(){
          int     n;
          char    buf[1024];
 
          n = read(STDIN_FILENO, buf, 1024);
          //printf("%d %s\n", n, buf);
          write(STDOUT_FILENO, buf, n);
 
          return 0;
  }

標準輸出連線到終端裝置(互動方式)是行緩衝,否則是全緩衝。

檔案讀寫標誌


檔案屬性 Struct

 27 struct  stat
 28 {
 29   dev_t     st_dev;        //檔案的裝置編號
 30   ino_t     st_ino;        //節點 
 31   mode_t    st_mode;        
 32   nlink_t   st_nlink;
 33   uid_t     st_uid;
 34   gid_t     st_gid;
 35   dev_t     st_rdev;
 36   off_t     st_size;        //檔案位元組數(檔案大小)
 37 #if defined(__rtems__)
 38   struct timespec st_atim;
 39   struct timespec st_mtim;
 40   struct timespec st_ctim;
 41   blksize_t     st_blksize;
 42   blkcnt_t  st_blocks;
 43 #else
 44   /* SysV/sco doesn't have the rest... But Solaris, eabi does.  */
 45 #if defined(__svr4__) && !defined(__PPC__) && !defined(__sun__)
 46   time_t    st_atime;
 47   time_t    st_mtime;
 48   time_t    st_ctime;
 49 #else
 50   time_t    st_atime;
 51   long      st_spare1;
 52   time_t    st_mtime;
 53   long      st_spare2;
 54   time_t    st_ctime;
 55   long      st_spare3;
 56   blksize_t st_blksize;
 57   blkcnt_t  st_blocks;
 58   long  st_spare4[2];
 59 #endif
 60 #endif
 61 };

exec函式族

exec後新程序保持原程序以下特徵      環境變數(使用了execle、execve函式則不繼承環境變數);
     程序ID和父程序ID;
     實際使用者ID和實際組ID;
     附加組ID;
     程序組ID;
     會話ID;
     控制終端;
     當前工作目錄;
     根目錄;
     檔案許可權遮蔽字;
     檔案鎖;
     程序訊號遮蔽;
     未決訊號;
     資源限制;
     tms_utime、tms_stime、tms_cutime以及tms_ustime值。
對開啟檔案的處理與每個描述符的exec關閉標誌值有關,程序中每個檔案描述符有一個exec關閉標誌(FD_CLOEXEC),若此標誌設定,則在執行exec時關閉該描述符,否則該描述符仍開啟。除非特地用fcntl設定了該標誌,否則系統的預設操作是在exec後仍保持這種描述符開啟,利用這一點
可以實現I/O重定向