Android系統開發(7)——標準I/O與檔案鎖,android檔案鎖

來源:互聯網
上載者:User

Android系統開發(7)——標準I/O與檔案鎖,android檔案鎖
一、常用函數fopen:FILE *fopen(const char *filename, const char *mode);fread:size_t  fread(void *ptz, size_t size, size_t nitems, FILE *stream);fwrite:size_t fwrite(const void *ptz, size_t size, size_t nitems, FILE *stream);fclose:int fclose(FILE *stream);fflush:int fflush(FILE *stream);fseek:int fseek(FILE *stream, long int offset, int where);fgetc,getc,getcharint fgetc(FILE *stream);int fgetc(FILE *stream);int getchar(); //標準輸入fputc,putc,putcharint fputc(int c, FILE *stream);int putc(int c, FILE *stream);fgets,getschar *fgets(char *s, int n, FILE *stream);fputs,putsint *fputs(char *s, FILE *stream);int *puts(char *s);在同級目錄下建立一個檔案file.in,用下面的代碼實現檔案拷貝(函數的參數可以使用man尋找,或者參考libc文檔)

#include <stdio.h>int main(){        char c;        FILE *pin, *pout;        //open file        pin = fopen("file.in", "r");        pout = fopen("file.out", "w+");        while(c = fgetc(pin) != EOF){                fputc(c, pout);        }        fclose(pin);        fclose(pout);        return 0;}
當我們很清楚我們的實體資源和不想讓一些緩衝來幹擾我們的時候(即時性要求高的地方)就可以使用底層的I/O操作,大部分情況下使用標準I/O操作就可以到達我們的要求。二、檔案鎖定假如有一個檔案a,如果進程A在操作(修改)的時候,進程B有可能正在讀檔案,這樣就會出現問題(有點像線程同步問題)。檔案的鎖定方式有檔案型的記錄型的兩種,對檔案的操作可分為獨佔和並發。開啟linux核心源碼,可以看到核心中對檔案鎖如下定義
struct file_lock {struct file_lock *fl_next;/* singly linked list for this inode  */struct list_head fl_link;/* doubly linked list of all locks */struct list_head fl_block;/* circular list of blocked processes */fl_owner_t fl_owner;unsigned char fl_flags;unsigned char fl_type;unsigned int fl_pid;struct pid *fl_nspid;wait_queue_head_t fl_wait;struct file *fl_file;loff_t fl_start;loff_t fl_end;struct fasync_struct *fl_fasync; /* for lease break notifications */unsigned long fl_break_time;/* for nonblocking lease breaks */const struct file_lock_operations *fl_ops;/* Callbacks for filesystems */const struct lock_manager_operations *fl_lmops;/* Callbacks for lockmanagers */union {struct nfs_lock_infonfs_fl;struct nfs4_lock_infonfs4_fl;struct {struct list_head link;/* link in AFS vnode's pending_locks list */int state;/* state of grant or error if -ve */} afs;} fl_u;};
在Linux中有強制鎖和建議鎖兩種鎖,強制鎖由系統核心空間支援(和核心操作相關的函數都會判斷),建議鎖其實就是一個標識鎖由使用者空間支援(手動判斷)。可以使用 man fcntl來查看 int fcntl(int fildes, in cmd, struct flock *arg);需要的標頭檔:<unistd.h><fcntl.h>
參數二cmd:F_GETLK  //得到鎖F_SETLK  //設定鎖F_SETLKW   //設定鎖並等待返回 參數三:           struct flock {
               ...
               short l_type;    /* Type of lock: F_RDLCK(共用鎖定),
                                   F_WRLCK(獨佔鎖), F_UNLCK (刪除鎖)*/
               short l_whence;  /* How to interpret l_start:
                                   SEEK_SET, SEEK_CUR, SEEK_END */
               off_t l_start;   /* Starting offset for lock (起點)*/
               off_t l_len;     /* Number of bytes to lock(長度) */
               pid_t l_pid;     /* PID of process blocking our lock
                                   (F_GETLK only)(擁有鎖的進程ID號) */
               ...
           };
#include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <sys/types.h>#include <unistd.h>#include <fcntl.h>int main(){        //open file        int fd = open("hello", O_RDWRIO_CREAT, 0666);        if(fd > 0){                //lock file                struct flock lock;                lock.l_type = F_WRLCK;                lock.l_whence = SEEK_SET;                lock.l_start = 0;                lock.l_len = 0;                lock.l_pid = getpid();                int rd = fcntl(fd, F_SETLK, &lock);                printf("return value of lock:%d\n", rd);                while(1){                        rd++;                }        }        return 0;}
三、錯誤處理系統級調用函數失敗之後會設定外部變數error的值來指明失敗原因。然後可以使用perror將最新的error輸出。
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int main(){        int fd = open("helloworld", O_RDONL, 0666);        if(fd < 0){                perror("open error");        }        return 0;}


STM32 103系列只有100個引腳 為何書上說:有7個I/O口 每個i/o口有16個引腳?

stm32 103系列有很多不同配置
u系列:36
c系列:48
r系列:64
v系列:100
z系列:144
你指的應該是z系列,有144腳。
根據晶片名字就可以知道,比如stm32f103zet6,103後面的z表示有144腳。具體的選型手冊可以去www.st.com上下載。
 
跪 :使用單片機I/O口,完成一個6位7段數位管動態顯示程式,顯示數字001632,要用組合語言

MOV 31H,#00H
MOV 32H,#00H
MOV 33H,#01H
MOV 34H,#06H
MOV 35H,#03H
MOV 36H,#02H
ACALL D0
JMP $-2
D0: MOV R0,#31H
MOV R2,#11111110B
MOV DPTR,#TAB
D1: ORL P1,#00111111B
MOV A,@R0
MOVC A,@A+DPTR
MOV R3,#8
D2: RLC A
MOV P3.2,C
SETB P3.3
CLR P3.3
DJNZ R3,D2
INC R0
MOV A,R2
ANL P1,A
D3: MOV R3,#250
DJNZ R3,$
MOV A,R2
RL A
MOV R2,A
JB ACC.6,D1
RET
TAB: DB 3FH,06H,5BH,4FH
DB 66H,6DH,7DH,07H
DB 7FH,6FH,77H,7CH
DB 39H,5EH,79H,71H
DB 40H
END



 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.