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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:50:31+00:00 2026-05-13T00:50:31+00:00

Quite a specific question, but I was wondering if anyone has had any problems

  • 0

Quite a specific question, but I was wondering if anyone has had any problems getting the first signal to be caught in a consumer-producer relationship with multiple consumers (HTTP web server)

for(i = 0; i < num_threads; i++) {
    pthread_cond_init(&condVars[i], NULL);
    if(strcmp(policy,"FIFO") == 0)
        pthread_create(&threadArr[i], NULL, workerFIFO, &condVars[i]);
    else
        pthread_create(&threadArr[i], NULL, workerSFF, &condVars[i]);

}


listenfd = Open_listenfd(port);
    // Producer
while (1) {
    clientlen = sizeof(clientaddr);
    connfd = Accept(listenfd, (SA *)&clientaddr, (socklen_t *) &clientlen);

    pthread_mutex_lock(&mutex);

    while(numRequests == num_buffers)
        pthread_cond_wait(&empty, &mutex);

    if(strcmp(policy,"FIFO") == 0)
        putFIFO(connfd);
    else
        putSFF(connfd);


    numRequests++;
    pthread_cond_signal(&condVars[nextWorker]);

    nextWorker = (nextWorker + 1) % num_threads;
    pthread_mutex_unlock(&mutex);
    printf("%s\n", "Look not dead!!!");

}

    // Consumer
    while(1){
    pthread_mutex_lock(&mutex);
    while(numRequests == 0) {
        printf("%u\n", condVar);
        pthread_cond_wait(condVar, &mutex);
        printf("%s", "caught it");
    }

    printf("%s, %u\n", "Its Workingzz! ZOMGZ!!!", condVar);


    int connfd = buffer[nextOutFIFO];
    nextOutFIFO = (nextOutFIFO + 1) % num_buffers;
    numRequests--;
    pthread_cond_signal(&empty);
    pthread_mutex_unlock(&mutex);
    requestHandle(connfd);

    Close(connfd);
      }
  • 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-13T00:50:31+00:00Added an answer on May 13, 2026 at 12:50 am

    I don’t believe the previous answer solves the problem, because both operations are being performed inside the critical section.

    I would recommend using a counting semaphore instead of an integer for numRequests. The producer performs a sem_post() and the consumers perform a sem_wait(). The first consumer to return from the wait will decrement the semaphore and can then lock the mutex only when modifying shared variables, minimizing the code inside the critical section, and imposing a sense of order that is not provided by mutexing alone.

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

Sidebar

Related Questions

This is quite a specific question, and I have had no luck on the
I am planning to make Q&A system (quite specific, has nothing to do with
My specific question is about using URLByAppendingPathComponent but the principle will be applicable to
This is not properly a question but something more like a thought I had
Yeah quite a specific question. I'm making a bot for Google Wave and I'm
I apologize if this has been asked before, but I haven't quite found the
I am quite sure that this is somehow a basic/stupid question but I just
I know how that question looks, but I'm quite serious. I am trying to
I have quite an specific question about image recognition. I have to write a
Looking at the related questions, I don't think this specific question has been asked,

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.