FCNTL system Call
Example 1: Status flags for a file
Myfcntl_access.c
#include <stdio.h>#include<unistd.h>#include<fcntl.h>#include<sys/types.h>#include<sys/stat.h>#include<stdlib.h>#include<errno.h>#defineERROR (flag)if(flag) {printf ("%d:", __line__); Fflush (stdout); Perror ("Error"); Exit (errno); }intMainintargcChar*argv[]) { intret; intAccess_mode; intFD; FD= Open (argv[1], O_RDWR); ERROR (FD== -1); RET= Fcntl (FD, F_SETFL, O_append |O_nonblock); ERROR (ret<0); RET= Fcntl (FD, F_GETFL,0); ERROR (ret<0); Access_mode= RET &O_accmode; if(Access_mode = =o_rdonly) {printf ("%s:read only", argv[1]); } Else if(Access_mode = =o_wronly) {printf ("%s:write only", argv[1]); } Else if(Access_mode = =o_rdwr) {printf ("%s:read + Write", argv[1]); } if(Ret &o_append) {printf (", append"); } if(Ret &O_nonblock) {printf (", Nonblock"); } printf ("\ n"); return 0;}
Compile the link execution, the properties of the resulting output file are as follows:
Example 2: Setting a read-write lock on a file descriptor
Myfcntl_lock.c
#include <stdio.h>#include<unistd.h>#include<fcntl.h>#include<sys/types.h>#include<sys/stat.h>#include<stdlib.h>#include<errno.h>#include<string.h>#defineERROR (flag)if(flag) {printf ("%d:", __line__); Fflush (stdout); Perror ("Error"); Exit (errno); }intLock_set (intFdstructFlock *Lock){ if(Fcntl (FD, F_SETLK,Lock) ==0) { if(Lock->l_type = =F_rdlck) {printf ("Set read lock, pid:%d\n", Getpid ()); } Else if(Lock->l_type = =F_wrlck) {printf ("Set write lock, pid:%d\n", Getpid ()); } Else if(Lock->l_type = =F_unlck) {printf ("release lock, pid:%d\n", Getpid ()); } } Else{perror ("Lock Operation Fail\n"); return-1; } return 0;}intLock_test (intFdstructFlock *Lock){ if(Fcntl (FD, F_GETLK,Lock) ==0) { if(Lock->l_type = =F_unlck) {printf ("lock can be set in fd\n"); return 0; } Else { if(Lock->l_type = =F_rdlck) {printf ("can ' t set lock, read lock have been set by:%d\n", Lock-l_pid); } Else if(Lock->l_type = =F_wrlck) {printf ("can ' t set lock, write lock have been set by:%d\n", Lock-l_pid); } return-2; } } Else{perror ("get incompatible locks fail"); return-1; }}intMainintargcChar*argv[1]){ intFD; intret; structFlockLock; Charread_buf[ +]; FD= Open (argv[1], O_creat | O_trunc |O_rdwr, S_irwxu); ERROR (FD== -1); RET= Write (FD,"=====hello 123456789=====", -); ERROR (ret!= -); memset (&Lock,0,sizeof(structflock)); Lock. l_whence =Seek_set; Lock. L_start =0; Lock. L_len =0; Lock. L_type =F_rdlck; if(Lock_test (FD, &Lock) ==0) { Lock. L_type =F_rdlck; Lock_set (FD,&Lock); } lseek (FD,0, Seek_set); RET= Read (FD, READ_BUF, -); ERROR (ret<0); Read_buf[ret]=' /'; printf ("%s\n", READ_BUF); GetChar (); Lock. L_type =F_wrlck; if(Lock_test (FD, &Lock) ==0) { Lock. L_type =F_wrlck; Lock_set (FD,&Lock); } getchar ();
Lock. L_type =F_unlck; Lock_set (FD,&Lock); Close (FD); return 0;}
The compile link runs with the following output:
Result 1: Execute only on one terminal, description of open file multibyte read lock, write lock and unlock result such as:
Result 2: Two terminals execute the same command at the same time, and then press ENTER again, the open file description multibyte read lock, write lock and unlock, the output results are as follows:
Understand the following:
1. This file read/write lock is not mandatory (provides a mechanism for implementing policies by the interface user).
2. Different processes can read locks on the same open file descriptor at the same time
3. File descriptor for read lock, no more write lock
Result 3: Two terminals execute the same command at the same time, one of the first consecutive press ENTER, so that it describes the file multibyte write lock, as follows:
Understand the following:
No read lock or write lock for files that have been write-locked
FCNTL system Call