C Pthreads的问题;改变价值等

C Pthreads的问题;改变价值等,c,pthreads,C,Pthreads,我知道标题很模糊,我就是不知道该怎么称呼它 卧铺 #ifndef SLEEPER_H #define SLEEPER_H #include <unistd.h> int rideTime(int, int); void walkAroundTime(int); #endif \ifndef轨枕\u H #定义枕木 #包括 int rideTime(int,int); 无效漫游时间(int); #恩迪夫 卧铺 #include <stdio.h> #include

我知道标题很模糊,我就是不知道该怎么称呼它

卧铺

#ifndef SLEEPER_H
#define SLEEPER_H
#include <unistd.h>

int rideTime(int, int);
void walkAroundTime(int);

#endif
\ifndef轨枕\u H
#定义枕木
#包括
int rideTime(int,int);
无效漫游时间(int);
#恩迪夫
卧铺

#include <stdio.h>
#include <stdlib.h>
#include "sleeper.h"

int rideTime(int id, int car) {

int seconds = ( rand() % 5) + 1 ;
printf ("Person %d is riding car %d for  %d seconds.\n", id, car, seconds);
sleep (seconds); 

return car;

}

void walkAroundTime(int id) {

int seconds = (rand() % 10) + 1 ;
printf ("Person %d is walking around for  %d seconds.\n", id, seconds);
sleep (seconds);

}
#包括
#包括
#包括“轨枕.h”
int rideTime(int id,int car){
整数秒=(rand()%5)+1;
printf(“人员%d驾驶车辆%d达%d秒。\n”,id,车辆,秒);
睡眠(秒);
返回车;
}
无效漫游时间(int-id){
整数秒=(rand()%10)+1;
printf(“人员%d正在走动%d秒。\n”,id,秒);
睡眠(秒);
}
汽车保险杠

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include "sleeper.h"

void* person(void*);
int getInLine(int);
void returnCar(int);

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t car_availability = PTHREAD_COND_INITIALIZER;
int available_cars;
int num_cars;
int* cars;

int main(int argc, char* argv[]) {
   num_cars = atoi(argv[1]);
   int num_people = atoi(argv[2]);
   int sim_time = atoi(argv[3]);

   available_cars = num_cars;
   cars = calloc(num_cars, sizeof(int));
   pthread_t* threads = calloc(num_people, sizeof(pthread_t));

    srand((unsigned)time (NULL));

   for (int i = 0; i < num_cars; i++) {
      cars[i] = 1;
   }

   for (int i = 0; i < num_people; i++) {
        int* id = malloc(sizeof(*id));
      *id = i + 1;
      pthread_create(&threads[i], NULL, person, (void*)(id));
   }

    //printf("MAIN: Sleeping for %d seconds\n", sim_time);
   sleep(sim_time);
    //printf("Done sleeping.\n");

   for (int i = 0; i < num_people; i++) {
      pthread_cancel(threads[i]);
   }

   printf("Simulation complete.\n");

   pthread_mutex_destroy(&lock);
   free(cars);
   free(threads);

   exit(EXIT_SUCCESS);
}

void* person(void* arg) {
    int car = 0;
   int id = *((int *) arg);
   while (1) {
      walkAroundTime(id);
      car = getInLine(id);
      rideTime(id, car);
      returnCar(car);
   }
}

int getInLine(int id) {
    int car = 0;

    printf("Person %d is waiting for a car.\n", id);
    pthread_mutex_lock(&lock);

    if (available_cars == 0) {
        pthread_cond_wait(&car_availability,&lock);
    }

    for (int i = 0; i < num_cars; i++) {
        if (cars[i] == 1) {
            car = i + 1;
            cars[i] = 0;
            break;
        }
    } 
    available_cars--;

    return car;
}

void returnCar(int carID) {
    available_cars++;
    cars[carID] = 1;
    printf("Car %d has been returned.\n", carID);
   pthread_cond_broadcast(&car_availability);
    pthread_mutex_unlock(&lock);
}
#包括
#包括
#包括
#包括
#包括“轨枕.h”
无效*人(无效*);
int getInLine(int);
无效返回车(int);
pthread\u mutex\u t lock=pthread\u mutex\u初始值设定项;
pthread_cond_t car_availability=pthread_cond_初始值设定项;
国际可用汽车;
国际汽车;
国际*汽车;
int main(int argc,char*argv[]){
num_cars=atoi(argv[1]);
int num_people=atoi(argv[2]);
int sim_time=atoi(argv[3]);
可用车数=车数;
cars=calloc(num_cars,sizeof(int));
pthread_t*threads=calloc(num_people,sizeof(pthread_t));
srand((无符号)时间(NULL));
对于(int i=0;i
所以。。。我的程序有两个问题。最重要的是,它只适用于1辆车。如果有多辆车,其中一辆被偷了,其他所有人仍然必须等待归还


第二个不太重要的问题是,当我只使用1辆车启动代码时,车的id从0开始,然后在1和0之间交替,而它应该是1。看看我的代码,我不清楚这些问题是从哪里产生的。。。事实证明,gdb是毫无用处的。

问题是,一旦你获得了锁,你就要把锁握得太久,以获得汽车。我以这种方式修改了您的函数,并在注释中注明了更改:

int getInLine(int id) {
    int car = 0;

    printf("Person %d is waiting for a car.\n", id);
    pthread_mutex_lock(&lock);

    // Use a while loop because pthread_cond_wait() is subject to
    // spurious awakenings.
    while (available_cars == 0) {
        pthread_cond_wait(&car_availability,&lock);
    }

    for (int i = 0; i < num_cars; i++) {
        if (cars[i] == 1) {
            car = i + 1;
            cars[i] = 0;
            break;
        }
    } 
    available_cars--;
    // Unlock the mutex here now that we are done acquiring the car.
    // If we don't unlock the mutex here, no one else can acquire a car.
    pthread_mutex_unlock(&lock);

    return car;
}

void returnCar(int carID) {
    // Lock the mutex here before returning the car.
    pthread_mutex_lock(&lock);
    available_cars++;
    // Need -1 here because carID is one bigger than the index.
    cars[carID-1] = 1;
    printf("Car %d has been returned.\n", carID);
    pthread_cond_broadcast(&car_availability);
    pthread_mutex_unlock(&lock);
}
intgetinline(intid){
int car=0;
printf(“人员%d正在等待车辆。\n”,id);
pthread_mutex_lock(&lock);
//使用while循环,因为pthread_cond_wait()受
//虚假的觉醒。
while(可用车==0){
pthread_cond_wait(&car_可用性,&lock);
}
对于(int i=0;i

顺便说一下,我注意到您使用
rand()
生成随机数。但是,如果多个线程同时调用
rand()
,您将得到相同的随机数(至少在我运行您的程序时是这样)。您应该改为使用
rand\u r()
,并为每个线程创建不同的种子。

在sleeper.h中,为什么包含unistd.h文件?文件中的两个原型不使用unistd.h中定义的任何项。通常,“包含在头文件中”仅适用于头文件后面使用的类型。如果某个源文件中需要unistd.h的任何内容,那么该源文件就是include应该位于的位置。您没有检查pthreads函数是否成功。如果您故意不检查错误,您就没有道义上的权利期望您的程序工作。参数“argc”未使用,因此编译器将发出警告。也就是说,这段代码没有干净地编译。此外,该代码假定在命令行上输入了正确的参数(以及参数数量)。在获取任何参数之前,应执行argc检查,以确保命令行正确。然后在每次调用atoi()之后,都应该检查转换后的值,以确保其可用/有效。从malloc和calloc返回的值应该始终被检查(!=NULL),以确保操作成功“这只是一个家庭作业,不会对评分进行错误检查,所以我对它很懒惰”,现在你已经学会了你的错误。错误检查不仅仅是为了得到分数,它是