Android system Development (7)--standard I/O and file lock

Source: Internet
Author: User
Tags flock flock lock fread

First, common functions 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 (); Standard input 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), create a file file.in in a sibling directory, implement a copy of the file using the following code (the parameters of the function can be found using the man, or refer to the LIBC document)
#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 well aware of 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, and in most cases we can reach our requirements using standard I/O operations. Second, file lock if there is a file a, if process A is in action (modified), process B may be reading the file, which will cause problems (a bit like thread synchronization issues). There are two types of file-type records, the operation of files can be divided into exclusive and concurrent. Open the Linux kernel source code, you can see the kernel of the file lock is defined as follows
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;u nsigned 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_OP Erations *fl_ops;/* callbacks for filesystems */const struct lock_manager_operations *fl_lmops;/* Callbacks for Lockmanag  ERs */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;}; 
There are two kinds of locks in Linux, locking and the proposed lock, forced lock by the system kernel space support (and kernel operation related functions will be judged), it is recommended that the lock is actually an identity lock by the user space Support (manual judgment). You can use the man fcntl to view int fcntl (int fildes, in cmd, struct flock *arg);Required header File:<unistd.h><fcntl.h>
parameter two cmd:F_GETLK//Get lock F_SETLK//Set lock F_SETLKW//Set lock and wait to return parameter three: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) */
off_t L_len; /* Number of bytes to lock (length) */
pid_t L_pid; /* PID of process blocking our lock
(F_GETLK only) (process ID number with 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); C2/>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;}
Third, error handling system-level call function fails after setting the value of the external variable error to indicate the reason for the failure. You can then 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;}

Android system Development (7)--standard I/O and file lock

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.