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 6793019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:03:02+00:00 2026-05-26T18:03:02+00:00

Hey guys i was thinking on how we can make a file read by

  • 0

Hey guys i was thinking on how we can make a file read by n readers and only one writer in which each operation has a different offset and size. With this motive i would like to have:-

1) Maximum concurrency
2) Preventing readers from reading regions of file where writer is writing to any offset in that region
3) however multiple readers can read overlapping area of file whenever writer does not have any overlap with the readers
4) Lastly readers should also not starve the writer

How can we achieve this situation using n+1 semaphore. Is there any example solution which i can refer. Thanks

Here is my psuedo code:-

lock
lock lockarray[m+1]
lockentry = set Lock Entry[Item number] 
writer code:
wait(mutex[item number]);
readcount[item number]--;
if read count[item number]==0
signal (wrt[item number]);
signal (mutex[item number])
}

lock
lock lockarray[m+1]
lockentry = set Lock Entry[Item number] 
Reader code:
wait(mutex[item number]);
read count[item number]++;
if read count[item number]==1
wait (wrt[item number]); 
wait(mutex[item number]);
signal mutex[item number];
  • 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-05-26T18:03:03+00:00Added an answer on May 26, 2026 at 6:03 pm

    If you want to do it on a file you can define a certain granularity and declare locks based on that granularity. For example for large files you can use a size of 4k (size of a page on a system) and say that each r/w lock has a granularity of 4k on the file and the number of locks is simply size of file / 4k. You can use any arbitrary size that you want and of course there’s a tradeoff between more fine grain locking (more memory to keep track of region locks) and achieving the best performance.

    You can try something like

    #define MYGRANULARITY 4096
    lock filelocks[file_size / MYGRANULARITY];
    
    /* Code to do read or write */
    int lock_idx = location_to_rw / MYGRANULARITY;
    lock(filelocks[lock_idx]);
    /* do work with it */
    unlock(filelocks[lock_idx]);
    

    To lock on a granularity lock all you have to do is find which lock you need to grab and do the normal R/W lock behavior.

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

Sidebar

Related Questions

Hey guys! I have this little problem: I have one ViewController which adds 2
Hey guys, I can only apologise for asking such a noob question, but this
Hey guys i wrote a quick test. I want delete to call deleteMe which
Hey Guys, here is my code for this, the only help i get from
Hey guys currently I am thinking of serving images using an image handler script.
hey guys how to make these php html links codes possible. i have tried
Hey guys, I want to make a function loop over all elements that have
hey guys, how can I just achieve this simple layout? I'm hoping the answer
Hey guys, I'm at a deadlock here after thinking about this for too long.
Hey guys & girls, Im wondering how I can find the object count of

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.