C function parameters are represented by both parameters and input parameters.
// Use fcntl to lock the file# Include "stdio. h"
# Include "unistd. h"
# Include "fcntl. h"
Int main ()
{
Int fd;
Struct flock lk;
Int r;
Fd = open ("a.txt", O_RDWR );
If (fd =-1)
{
Fd = open ("a.txt", O_RDWR | O_CREAT | O_EXCL, 0666 );
If (fd =-1)
{
Perror ("File Open Error ");
Exit (2 );
}
}
Lk. l_type = F_WRLCK;
Lk. l_whence = SEEK_SET;
Lk. l_start = 5;
Lk. l_len = 10;
R = fcntl (fd, F_SETLK, & lk); // lk here is the input parameter
If (r = 0)
{
Printf ("Lock Sussess! \ N ");
}
Else
{
Printf ("Lock Failed! \ N ");
}
While (1); // The program cannot be exited
Return 0;
} // Use fcntl to read the file lock
# Include "stdio. h"
# Include "unistd. h"
# Include "fcntl. h"
Int main ()
{
Int fd;
Struct flock lk = {0 };
Int r;
Fd = open ("a.txt", O_RDWR );
If (fd =-1)
{
Perror ("Error ");
Exit (0 );
}
R = fcntl (fd, F_GETLK, & lk); // lk is an outgoing parameter here
If (r = 0)
{
Printf ("Get Lock Success! \ N ");
}
If (lk. l_type = F_WRLCK)
{
Printf ("Write Lock! \ N ");
}
Printf ("start: % d, len: % d \ n", lk. l_start, lk. l_len );
Return 0;
}