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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:53:40+00:00 2026-06-10T08:53:40+00:00

I wrote the code for sleeping barber problem and it seems to be looking

  • 0

I wrote the code for sleeping barber problem and it seems to be looking weird…
the code is as follows..

#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>

#define MAX_C 10
int a[MAX_C], head=0, tail=0, tmp, tb, tc, count=1;
pthread_mutex_t B;
double time_slot[]={0.125,0.5,0.75,1.00,1.25,1.50,1.75,2.00};

void wait(int a)
{
    clock_t g=clock();
    while(((float)(clock()-g))/CLOCKS_PER_SEC != time_slot[a]);
}

void barber()
{
    printf("barber started\n");
    while(1) {
        tmp=0;
        while(head==tail) {
            printf("b\n");
        }
        tb=rand()%8;
        printf("tail:%d\n", tail);
        tail=(tail+1)%MAX_C;
        wait(tb);
    }
}

void customer()
{       
    printf("customers started\n");
    while(1) {
        wait(rand()%8);
        while((head+1)%MAX_C == tail) {
            printf("c\n");
        }
        a[head]=count-1;
        printf("head:%d\n", head);
        head=(head+1)%MAX_C;
    }
}

int main(int argc, char* argv[])
{
    pthread_t b,c;

    pthread_mutex_init(&B, NULL);
    pthread_create(&c, NULL, (void*)&customer, NULL);
    pthread_create(&b, NULL, (void*)&barber, NULL);

    pthread_join(b, NULL);
    pthread_join(c, NULL);  
    exit(0);
}

The problem is that when the buffer is full … the barber is waiting for customers… but the customer is not executing at all!!( it is neither waiting nor filling the buffer)… coz the customers while loop was not executing…

  • 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-10T08:53:41+00:00Added an answer on June 10, 2026 at 8:53 am

    As Daneil Fischer said … there was a mistake with while(((float)(clock()-g))/CLOCKS_PER_SEC != time_slot[a]); i should replace it with

    while(((float)(clock()-g))/CLOCKS_PER_SEC <= time_slot[a]);

    But its still weird that this ‘missing’ of the clock value is occuring only after the whole buffer is filled….

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

Sidebar

Related Questions

i have wrote code for quicksort with linked list ,here is code #include<stdio.h> #include<iostream>
Since virturl directory name is not fixed, I wrote code below to include .css
I always run in circles with this problem. i wrote code that uses the
i have problem in javascript email validation, I wrote code something like, //emp.php <form
I wrote code to get name of printer which is installed in my system.but
I wrote code first without using functions to prototype, and of course, it worked
I recently wrote code that didnt work as i would expect, it was: message
i added sharekitkit my project. And wrote code like here : SHKItem *item =
I wrote some code in VHDL that is expected to look at a rotory
I wrote this code for zoom in/out . it works but even with one

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.