C function parameters are represented by both parameters and input parameters.

Source: Internet
Author: User

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;
}

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.