简体   繁体   English

用pthread模拟循环

[英]Simulating round robin with pthreads

The task is to have 5 threads present at the same time, and the user assigns each a burst time. 任务是同时存在5个线程,并且用户为每个线程分配一个突发时间。 Then a round robin algorithm with a quantum of 2 is used to schedule the threads. 然后使用量子为2的循环算法来调度线程。 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. 我的sleep()也不起作用。 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! 谢谢!

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. 1.在Run函数的开始,将tid(这是指向void的指针)直接转换为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. 最好使int变为volatile,这样编译器就不会对其值保持不变做出任何假设。

  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. 第二次调用该函数sleep时,您传递的参数类型为double(times [i]),并且应传递一个无符号的int参数。 A direct cast like (unsigned int) times[i] should solve that. 直接转换为(unsigned int) times[i]应该可以解决这个问题。

  3. You're doing the pthread_join before creating the other threads. 创建其他线程之前,您正在执行pthread_join。 When you create thread 3, and it enters its busy waiting state, the other threads won't be created. 创建线程3并进入繁忙的等待状态时,将不会创建其他线程。 Try putting the joins after the for block. 尝试将联接放在for块之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM