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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:33:48+00:00 2026-06-12T14:33:48+00:00

An exam question: A resource is shared between N threads such that access is

  • 0

An exam question:

A resource is shared between N threads such that access is on a first-come-first-served basis. Explain how you would implement a policy of first-come-first-served.

My answer would be along the lines of (Note: the course is Java-based):

Use a semaphore (binary) to control access to the shared resource. Then create a feeder class/thread to access the semaphore. The feeder thread uses a FIFO queue to implement first-come-first-served. When the semaphore is free it allows the thread a the head of the queue to access the shared resource.

I have just jotted out on paper the above approach and it seems to work.

The question is worth 8 marks in a 20 mark question so I doubt using new ReentrantLock(true) would suffice for full marks.

What do you guys think?

  • 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-12T14:33:49+00:00Added an answer on June 12, 2026 at 2:33 pm

    Your answer really doesn’t address the question. This is not just about locking but is also about queuing the threads waiting for the resource.

    • How do you queue the threads waiting for the resource?
    • If there are threads waiting for the resource, how do you wake them up so they consume the resource in order.
    • If there are threads waiting for the resource, how do you make sure they run in order and avoid the race condition around a new thread grabbing it.

    A possible solution would be something like the following code:

      lockResource(resource);
      try {
         ... consume the resource
      } finally {
         unlockResource(resource);
      }
    

    The lockResource would do something like:

    1. Acquire the lock on the resource.
    2. Retrieve the lock object associated with the resource that contains a long accessNum and a long runningNum.
    3. increment the accessNum value and store the results for the thread.
    4. check the long runningNum value to see if == to my accessNum.
    5. If it is == mine, unlock resource and continue running.
    6. If more than me in the queue, wait() on resource.

    When unlockResource(...) is called, the thread should:

    1. Lock resource.
    2. Set running++
    3. Call notifyAll()
    4. Unlock resource

    If a thread is waiting and gets awoken:

    1. Check to see if the accessNum == runningNum.
    2. If true then unlock resource and continue running
    3. If not then go back to wait()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this exam question that I have to tell how the beq instruction
The full question from our sample exam paper: Explain by highlighting the relevant parts
I have a nested polymorphic resource, question, which may be nested beneath an exam.
I've a question about that: @st = exam.students.find(:all) @st.each do |student| Return me an
i'm preparing for an exam and I'm having a question that I hope someone
I'm building a form to create an exam object that starts with 1 question,
I am studying for an exam and came across this question that seems a
This is a past exam question and I'm not sure how you would answer
yesterday I took a database exam and the question about normalization was strange. We
Question from an exam: Given a linked list with unknown size n , where

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.