fwrite flock異常情況測試

2021-10-21 18:34:59 字數 2748 閱讀 4385

更名操作 ==》兩個程序都開啟檔案之後,乙個鎖住(iotest),乙個等待鎖(iotest2),此時修改檔案iotest.log的名字

更名操作 ==》iotest程序開啟檔案並鎖住檔案,iotest2不開啟檔案,此時修改檔案iotest.log的名字

刪除操作 ==》兩個程序都開啟檔案,乙個程序鎖住(iotest),乙個程序等待鎖(iotest2),此時刪除檔案

刪除操作 ==》單獨起乙個程序,strace跟蹤情況

只要是檔案被程序開啟了,那之後無論是檔案更名,還是程序加鎖、解鎖,程序最終指向的都是此檔案,內容也一定是寫入此檔案,並寫入成功的

如果檔案被程序開啟之後,後來又被刪除掉了,那無論程序加鎖、解鎖、還是寫資料,都不會報錯,當然雖然不報錯,但寫入的資料寫到**就不得而知了

/****** iotest ******/

#include #include #include #include #include #include #define iotest_dir "/tmp/"

#define iotest_wstr "this is iotest\n"

static void iotest_print(char *pstr)

int main()

; char *pstr;

snprintf(fpath, sizeof(fpath), "%siotest.log", iotest_dir);

iotest_print("即將開啟/建立檔案, 繼續請按任意鍵...");

fp = fopen(fpath, "at+");

if (fp == null)

re_lock:

iotest_print("即將鎖住此檔案, 繼續請按任意鍵...");

r = flock(fileno(fp), lock_ex);

if (r)

fclose(fp);

fp = null;

return -1;

}iotest_print("即將寫入資料至緩衝區, 繼續請按任意鍵...");

fwrite(iotest_wstr, sizeof(iotest_wstr), 1, fp);

iotest_print("即將把資料從緩衝區刷入磁碟, 繼續請按任意鍵...");

fflush(fp);

re_input:

iotest_print("即將解鎖, 繼續請按任意鍵...");

flock(fileno(fp), lock_un);

iotest_print("解鎖完成, 即將關閉描述符, 繼續請按任意鍵...");

fclose(fp);

printf("檔案描述符已關閉, 操作完成, 正在退出...\n");

return 0;

}

/****** iotest2 ******/

#include #include #include #include #include #include #define iotest2_dir "/tmp/"

#define iotest2_wstr "this is iotest2\n"

static void iotest2_print(char *pstr)

int main()

; char *pstr;

snprintf(fpath, sizeof(fpath), "%siotest.log", iotest2_dir);

iotest2_print("即將開啟/建立檔案, 繼續請按任意鍵...");

fp = fopen(fpath, "at+");

if (fp == null)

re_lock:

iotest2_print("即將鎖住此檔案, 繼續請按任意鍵...");

r = flock(fileno(fp), lock_ex);

if (r)

fclose(fp);

fp = null;

return -1;

}iotest2_print("即將寫入資料至緩衝區, 繼續請按任意鍵...");

fwrite(iotest2_wstr, sizeof(iotest2_wstr), 1, fp);

iotest2_print("即將把資料從緩衝區刷入磁碟, 繼續請按任意鍵...");

fflush(fp);

re_input:

iotest2_print("即將解鎖, 繼續請按任意鍵...");

flock(fileno(fp), lock_un);

iotest2_print("解鎖完成, 即將關閉描述符, 繼續請按任意鍵...");

fclose(fp);

printf("檔案描述符已關閉, 操作完成, 正在退出...\n");

return 0;

}

浮點異常情況(VX FP TASK)

總結 在生成任務時,如果在任務中使用浮點計算,一定需要將任務的標誌vx fp task設定,否則將會出錯,主要是在其他任務中出現0 0錯誤 產生原因 當沒有設定浮點計算標識vx fp task時,在進入任務時沒有儲存浮點暫存器值,這樣在任務中進行浮點運算將破壞了浮點暫存器的內容,而在任務退出後,浮點...

python 異常情況處理

def ceshi number input 請輸入乙個數字 number int number try 裡面是有可能有異常的 try result 10 number except 是異常捕獲,多個except也只能執行乙個 except valueerror print 請輸入整數 except...

lftp連線異常情況分析過程

問題現象 通過rpm安裝好lftp後,執行lftp huangmr huangmr 192.168.107.132無法連線 lftp huangmr 192.168.107.132 ls ls at 0 delaying before reconnect 175 或者是一直處於connecting....