Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8090999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:54:57+00:00 2026-06-05T19:54:57+00:00

i executed the following c code on my ubuntu machine…i have read about fcntl()’s

  • 0

i executed the following c code on my ubuntu machine…i have read about fcntl()’s use to lock a file and even reading will not be allowed if F_WRLCK opton is set…so i started this program and before relinquishing the lock by pressing enter i tried to open the file in two ways- by directly double clicking on file1.cpp and by running a different c program in a new terminal…both the time the file was opened…so how did fcntl() allow opening of these files when F_WRLCK is set…

int main(int argc, char *argv[])
{

struct flock fl = {F_WRLCK, SEEK_SET,   0,      0,     0 };
int fd;

fl.l_pid = getpid();

if ((fd = open("/home/file1.cpp", O_WRONLY)) == -1)
{
    perror("open");
    exit(1);
}


if (fcntl(fd, F_SETLKW, &fl) == -1) 
{
    perror("fcntl");
    exit(1);
}


printf("Press <RETURN> to release lock: ");
getchar();

fl.l_type = F_UNLCK;  /* set to unlock same region */

if (fcntl(fd, F_SETLK, &fl) == -1)
{
    perror("fcntl");
    exit(1);
}

printf("Unlocked.\n");

close(fd);

return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:55:01+00:00Added an answer on June 5, 2026 at 7:55 pm

    fcntl locks are purely advisory locks. Their only effect is to cause fcntl F_SETLK calls to block when the lock cannot be obtained. They have absolutely no effect on IO operations. It’s up to your program to obtain the locks it needs before performing IO when synchronization is necessary.

    This is completely analogous to using mutexes to protect objects in memory. The mutex will not prevent you from reading or writing memory addresses; it’s just a protocol you use to make sure your program only reads and writes and proper times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used following code to create a simple PDF file. It executes fine
i have executed the following code public class Activity_Threads_Handler extends Activity { private int
Please have a look a the following code sample, executed on a Windows-32 system
I have following code that is executed when doing a mouseenter on a div
I have 2 separate foreach loops being executed in the following code. At the
Assuming A.sql contains the following code, then second Select query won’t be executed due
I have tomcat 6.0 with 2 webapps. One of them executes the following code:
Say that I have the following code: ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(myRunnable); Now, if
I executed the following code: using System; using System.Collections.Generic; namespace TestReleaseAndDebug { public class
I have tried to use setjmp/longjmp in 64 bit Ubuntu, but it doesn't properly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.