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

  • SEARCH
  • Home
  • 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 8186621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:16:23+00:00 2026-06-07T02:16:23+00:00

I read a book about files in Linux, and it gives the next example:

  • 0

I read a book about files in Linux, and it gives the next example:

int main(char ** argv, int argc) {
  int stat;
  int fd = open("dugma1.txt", O_WRONLY, 0666);

  if (fork() == 0) {
    int fd2 = open("dugma1.txt", O_WRONLY, 0666);
    sleep(10);
    if (lockf(fd2, F_TLOCK, 17) >= 0) {
      write(fd2, "I was here second", 17);
    }
  } //if
  else {
    lockf(fd, F_TLOCK, 16);
    write(fd, "I was here first", 16);
    wait(&stat);    
  }
}

It says that the output will be:I was here first, and the reason: We don’t close the file. But I didn’t understand this explantion. We first write: I was here first, but why after the sleep(10) we will not go to this part of the code:

if (lockf(fd2, F_TLOCK, 17) >= 0) {
   write(fd2, "I was here second", 17);
}

F_TLOCK is a non-blocking, and for that we will succsses writing “I was here second”.

Thanks

  • 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-07T02:16:25+00:00Added an answer on June 7, 2026 at 2:16 am

    The parent executes lockf(fd, F_TLOCK, 16) on the opened file, locking the first 16 bytes. Then it writes the text inside and waits for the child to exit. It does not close the file and hence the lock remains. If there was a close(fd); in the parent’s code after the write(), the lock would have been released but there isn’t.

    The child first sleeps for a while and when it tries to lock the first 17 bytes of the file but that fails since the parent still has the lock. That’s why lockf(fd2, F_TLOCK, 17) fails with EAGAIN – the operation should be repeated later on. Errors are signalled by a return value of -1 which makes the conditional in the child code not to execute.

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

Sidebar

Related Questions

I read a book about the observer pattern. It gives the following example: Class
I've read this book section about git branches. I have create a branch called
I could find the answer if I read a complete chapter/book about multithreading, but
I am little bit confused about this statement that I read in a book
I just read about the breadth-first search algorithm in the Introduction to Algorithms book
Is there any book or links that I can read some tutorial about the
I read a book that write the next program in C, and convet the
I've read in Stoyan Stefanov book about single var pattern. Also it's good by
I read about Drivers that uses JDBC API in a book. Not clear about
I am using 1.3. I read about cake logging activites in book.cakephp But in

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.