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

The Archive Base Latest Questions

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

The task is to have 5 threads present at the same time, and the

  • 0

The task is to have 5 threads present at the same time, and the user assigns each a burst time. Then a round robin algorithm with a quantum of 2 is used to schedule the threads. For example, if I run the program with

$ ./m 1 2 3 4 5

The output should be

A 1
B 2
C 2
D 2
E 2
C 1
D 2
E 2
E 1

But for now my output shows only

A 1
B 2
C 2

Since the program errs where one thread does not end for the time being, I think the problem is that this thread cannot unlock to let the next thread grab the lock. My sleep() does not work, either. But I have no idea how to modify my code in order to fix them. My code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
double times[5];
char process[] = {'A', 'B', 'C', 'D', 'E'};
int turn = 0;

void StartNext(int tid)     //choose the next thread to run
{
    int i;
    for(i = (tid + 1) % 5; times[i] == 0; i = (i + 1) % 5)
        if(i == tid)    //if every thread has finished
            return;
    turn = i;
}

void *Run(void *tid)    //the thread function
{
    int i = (int)tid;
    while(times[i] != 0)
    {
        while(turn != i);   //busy waiting till it is its turn
        if(times[i] > 2)
        {
            printf("%c 2\n", process[i]);
            sleep(2);   //sleep is to simulate the actual running time
            times[i] -= 2;
        }
        else if(times[i] > 0 && times[i] <= 2)      //this thread will have finished after this turn
        {
            printf("%c %lf\n", process[i], times[i]);
            sleep(times[i]);
            times[i] = 0;
        }
        StartNext(i);   //choose the next thread to run
    }
    pthread_exit(0);
}

int main(int argc, char **argv)
{
    pthread_t threads[5];
    int i, status;

    if(argc == 6)
    {
        for(i = 0; i < 5; i++)
            times[i] = atof(argv[i + 1]);   //input the burst time of each thread
        for(i = 0; i < 5; i++)
        {
            status = pthread_create(&threads[i], NULL, Run, (void *)i);    //Create threads
            if(status != 0)
            {
                printf("While creating thread %d, pthread_create returned error code %d\n", i, status);
                exit(-1);
            }
            pthread_join(threads[i], 0);    //Join threads
        }
    }
    return 0;
}

The program is directly runnable. Could anyone help me figure it out? Thanks!

  • 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-31T18:53:14+00:00Added an answer on May 31, 2026 at 6:53 pm

    Some things I’ve figured out reading your code:

    1. At the beginning of the Run function, you convert tid (which is a pointer to void) directly to int. Shouldn’t you dereference it?

    1. It is better to make int turn volatile, so that the compiler won’t make any assumptions about its value not changing.

    2. When you call the function sleep the second time, you pass a parameter that has type double (times[i]), and you should pass an unsigned int parameter. A direct cast like (unsigned int) times[i] should solve that.

    3. You’re doing the pthread_join before creating the other threads. When you create thread 3, and it enters its busy waiting state, the other threads won’t be created. Try putting the joins after the for block.

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

Sidebar

Related Questions

I have a task in which I have to read data using different threads.
I have an application that performs a write to a database each time a
I have a task that takes quite a long time. So I would like
TASK : I have an existing xml document (UTF-8) which uses xml namespaces and
Task: I have a camera mounted on the end of our assembly line, which
I have a slow custom data source in a SSIS Dataa Flow Task.I have
I am new this sharepoint development and i have task in hand to do
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have a task and I want to generate some code using the CodeDom.
I have a Task object that has a collection of Label objects ... in

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.