Android system development (7)-standard I/O and file lock, android file lock

Source: Internet
Author: User
Tags flock flock lock

Android system development (7)-standard I/O and file lock, android file lock
I. Common functionsFopen: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 getchar (); // standard inputFputc, 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); create a file FILE in the same directory. in, use the following code to copy the file (you can use man to find the function parameters, or refer to the libc Documentation)

#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;}
When we are clear about our physical resources and do not want some caching to interfere with us (where real-time requirements are high), we can use the underlying I/O operations, in most cases, standard I/O operations can meet our requirements. Ii. File Locking. Assume that there is a file A. If process a is operating (modifying), process B may be reading the file, this will cause problems (a bit like thread synchronization problems ). There are two types of file locking methods: file-type record-type, file operations can be divided into exclusive and concurrent. Open the Linux kernel source code and you can see the following definition of the file lock in the kernel:
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;};
In Linux, there are two types of locks: Strong locks and recommended locks. Forced locks are supported by the system kernel space (which can be determined by functions related to kernel operations ), the recommended lock is actually an identity lock supported by the user space (manually determined ). You can use man fcntl to view Int fcntl (int fildes, in cmd, struct flock * arg );Required header file: <unistd. h> <fcntl. h>
Parameter 2 cmd:F_GETLK // get the lock F_SETLK // set the lock F_SETLKW // set the lock and wait for return Parameter 3:Struct flock {
...
Short l_type;/* Type of lock: F_RDLCK (shared lock ),
F_WRLCK (exclusive lock), F_UNLCK (delete lock )*/
Short l_whence;/* How to interpret l_start:
SEEK_SET, SEEK_CUR, SEEK_END */
Off_t l_start;/* Starting offset for lock (start point )*/
Off_t l_len;/* Number of bytes to lock (length )*/
Pid_t l_pid;/* PID of process blocking our lock
(F_GETLK only) (ID of the process that owns the lock )*/
...
};
#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;}
3. After the system-level function call fails, the error value of the external variable is set to indicate the cause of the failure. Then, you can use perror to output the latest 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;}


Why does the STM32 103 series have only 100 pins? Why is there 7 I/O Ports and each I/o port has 16 pins?

The stm32 103 series has many different configurations.
U series: 36
C series: 48
R series: 64
V: 100
Z-Series: 144
You should be referring to the z series, with 144 feet.
According to the chip name, for example, stm32f103zet6 and z after 103 indicate 144 feet. You can download the specific selection manual from www.st.com.

Kneeling: Use the I/O port of the single-chip microcomputer to complete a 6-digit, 7-segment digital tube dynamic display program. The display number is 001632, and the assembly language is used.

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




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.