APUE學習筆記 2

2021-07-08 18:45:07 字數 1044 閱讀 7068

檔案i / o

5個函式:open、read、write、lseek 以及close

lseek

off_t lseek(int filedes, off_t offset, int whence);

• 若whence是seek_set(定義在sys/types.h中),則將該檔案的位移量設定為距檔案開始處offset 個位元組。

• 若whence是seek_cur,則將該檔案的位移量設定為其當前值加offset, offset可為正或負。

• 若whence是seek_end,則將該檔案的位移量設定為檔案長度加offset, offset可為正或負

read

ssize_t read(int filedes, void *buff, size_t nbytes);

write

ssize_t write(int filedes, const void * buff, size_t nbytes);

#include "apue.h"

#include

#include

#define buffsize 4096

int main(int argc, char*argv)

; lseek(filefd, 0, seek_set);

ret = read(filefd, buf, buffsize);

printf("buf = %s \nret = %d \n", buf, ret);

return

0;}

root@ubuntu:# gcc -i ../apue.3e/include test1210.c 210 -static ../apue.3e/lib/libapue.a

root@ubuntu:# ./test1210

hello!

buf = hello c!hello c!hello c!hello c!hello c!hello c!hello c!hello c!hello c!hello c!

ret = 80

APUE學習筆記 執行緒

採用多執行緒模式可以採用同步程式設計,而非非同步程式設計,可以簡化程式設計 多個程序間可以很方便的共享資料 可以通過pthread self獲得自身的執行緒id。執行緒id只在程序內部唯一。新建立執行緒不能保證那個執行緒先執行,新縣城可以訪問程序的位址空間,繼承執行緒的浮點環境和訊號遮蔽字。如果任意...

APUE學習筆記 umask

umask的作用是為程序設定檔案模式建立遮蔽字,也就是說linux中每建立乙個檔案,則檔案許可權取決於umask設定的遮蔽狀態。1 檔案許可權 理解umask之前,先看下檔案許可權。所有檔案型別 目錄檔案 字元特別檔案等 都有訪問許可權,很多人認為只有普通檔案有訪問許可權,這是一種誤解。每個檔案的9...

APUE讀書筆記(2)

chapter4 file directory struct stat 檔案型別 用提供的巨集來判斷,如if s isdir buf.st mode uid和gid 檔案將其所有者的使用者id和組id記錄在stat結構的st uid和st gid 實際使用者id和實際組id用於標識使用者究竟是誰。這...