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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:47:26+00:00 2026-06-13T15:47:26+00:00

Im trying to complete what I thought would be simple task but after several

  • 0

Im trying to complete what I thought would be simple task but after several days have reached a breaking point.

I am trying to simulate a database using multiple readers and a single writer. When I run my program it deadlocks.

I was trying to base it on this algorithm:

 1 semaphore accessMutex;     // Initialized to 1
 2 semaphore readersMutex;    // Initialized to 1
 3 semaphore orderMutex;      // Initialized to 1
 4 
 5 unsigned int readers = 0;  // Number of readers accessing the resource
 6 
 7 void reader()
 8 {
 9   P(orderMutex);           // Remember our order of arrival
10 
11   P(readersMutex);         // We will manipulate the readers counter
12   if (readers == 0)        // If there are currently no readers (we came first)...
13     P(accessMutex);        // ...requests exclusive access to the resource for readers
14   readers++;               // Note that there is now one more reader
15   V(orderMutex);           // Release order of arrival semaphore (we have been served)
16   V(readersMutex);         // We are done accessing the number of readers for now
17 
18   ReadResource();          // Here the reader can read the resource at will
19 
20   P(readersMutex);         // We will manipulate the readers counter
21   readers--;               // We are leaving, there is one less reader
22   if (readers == 0)        // If there are no more readers currently reading...
23     V(accessMutex);        // ...release exclusive access to the resource
24   V(readersMutex);         // We are done accessing the number of readers for now
25 }
26 
27 void writer()
28 {
29   P(orderMutex);           // Remember our order of arrival
30   P(accessMutex);          // Request exclusive access to the resource
31   V(orderMutex);           // Release order of arrival semaphore (we have been served)
32 
33   WriteResource();         // Here the writer can modify the resource at will
34 
35   V(accessMutex);          // Release exclusive access to the resource
36 }

However I am trying to implement it using only pthreads rather than semaphores. As you can expect here is what the mess turned out to be:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAX 5 //size of buffer

pthread_mutex_t mutex; //pthread_mutex type
pthread_cond_t condw, condr; //reader/writer cond var
int buffer = 0, rc = 0;

pthread_mutex_t db; //pthread_mutex type

//reader-writer header:
void* writer(void* ptr);
void* reader(void* ptr);

void use_data();
void write_database();
void readdb();

int main(int argc, char** argv)
{
  pthread_t read,write;
  //allow signal back and forth
  pthread_mutex_init(&mutex,0); //init mutex
  pthread_cond_init(&condw, 0); //init writer
  pthread_cond_init(&condr,0); //init reader

    pthread_mutex_init(&db,0); //init db

  //this calls the void* reader function
  pthread_create(&write,0,writer,0); //create thread
  pthread_create(&read,0,reader,0); //create thread

  //let them join
  pthread_join(read,0);
  pthread_join(write,0);

  //destroy them
  pthread_cond_destroy(&condw);
  pthread_cond_destroy(&condr);
  pthread_mutex_destroy(&db);
  pthread_mutex_destroy(&mutex);

  return 0;
}//end main

void* reader(void* arg)
{
  while(1) //if there is a reader lock the db 
  {
    pthread_mutex_lock(&mutex);
    rc++;
    if(rc==1)
    {
      pthread_mutex_lock(& db);
    }
    pthread_mutex_unlock(&mutex);
    readdb();
    pthread_mutex_lock(&mutex);
    rc--;
    if(rc==0) 
    {
      pthread_mutex_unlock(&db);
    }
    pthread_mutex_unlock(&mutex);
    use_data();
  }
}

void* writer(void* arg)
{
  while(1) //unlock the db
  {
    pthread_mutex_lock(&db);
    write_database();
    pthread_mutex_unlock(&db);
  }
}

void use_data(){}
void write_database(){}
void readdb(){}

Any help with a working solution and explanation where we went wrong is appreciated as it would help me and my colleagues out. Regards.

  • 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-13T15:47:28+00:00Added an answer on June 13, 2026 at 3:47 pm

    The problem is that your source algorithm relies on the fact that one thread locks the accessMutex lock, and another thread then may unlock it. This is allowable with semaphore-based mutexes, but it is not allowed for pthreads mutexes.

    There are semaphores in pthreads, provided by the sem_init(), sem_post() and sem_wait() functions. You could use these to write a direct implementation of the source algorithm, and it should work correctly.

    Alternately, pthreads also provides a native read-writer lock type – see the functions pthread_rwlock_init(), pthread_rwlock_rdlock(), pthread_rwlock_wrlock() and pthread_rwlock_unlock(). You could use this for a very simple implementation, but obviously this is missing the point if it’s supposed to be a learning exercise.

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

Sidebar

Related Questions

I trying to complete a RPG game for a project, but have no idea
I've been trying for a while to get something I thought would be simple
I thought this would be really simple but its proving to be very difficult.
I am beginner trying to complete a simple JSON problem on SingPath which asks
Im trying to get some auto complete to work. I have this URL i
I am trying to have a textBox auto complete with values from a database.
I'm trying to create an auto-complete function for the ICSharpCode.TextEditor. But the fileTabs_KeyDown doesn't
I have a task that might take a while to complete. I have tried
Basically I have this code. I am trying to simply update a div but
I have a problem that I thought was easily solved, but is turning out

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.