簡體   English   中英

具有多個線程的分段錯誤(核心轉儲)

[英]Segmentation fault (core dumped) with multiple threads

我們必須制作一個模擬劇院座位預訂系統功能的程序。 我們有N_cust客戶在電話中心打電話, N_tel人接聽電話。 每次通話從t_seathigh持續到t_seatlow秒。 使用信用卡付款成功,概率為P_cardsuccess 程序獲得的兩個參數是客戶端數量和rand_r的種子。每個客戶端都有一個線程,客戶端必須等待一個人在電話上通話。 我的問題是程序運行但是在AwesomeThreadFunction函數的第一個循環中出現了分段錯誤或卡在無限循環中。

我想也許我還沒有正確處理變量telefoners ,因為程序有時會卡在函數的第一個循環中,但我不知道究竟是怎么回事。 我也不知道為什么會出現這種分段錯誤。 我的程序究竟在哪里嘗試訪問未分配的內存。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include "p3170013-p3170115-p3170097-res1.h"
#include <unistd.h>
#define N_seat 250
#define N_tel 8
#define N_seatlow 1
#define N_seathigh 5
#define t_seatlow 5
#define t_seathigh 10
#define P_cardsuccess 0.9
#define C_seat 20.0
#define BILLION 1E9

pthread_mutex_t lock_phone, lock_bank_account,lock_number_of_transfer,lock_wait_time, lock_service_time, lock_plan, lock_screen;
int plan[N_seat];
int phone_count=0,bank_account=0,tid=0,seats=0,telefoners=N_tel;
unsigned int seed;
float avg_wait_time=0,avg_service_time;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void* AwesomeThreadFunction(void* vargc){
tid=*(int*)vargc;
struct timespec waitStart, waitEnd;
clock_gettime(CLOCK_REALTIME, &waitStart);
avg_wait_time-=(waitStart.tv_sec+waitStart.tv_nsec/BILLION);
pthread_mutex_lock(&lock_screen);
printf("%d\n",telefoners);
pthread_mutex_unlock(&lock_screen);
pthread_mutex_lock(&lock_phone);
while (telefoners == 0) {
    pthread_cond_wait(&cond, &lock_phone);
}
telefoners--;
pthread_mutex_unlock(&lock_phone);
pthread_mutex_lock(&lock_screen);
printf("coooooooool\n");
pthread_mutex_unlock(&lock_screen);
clock_gettime(CLOCK_REALTIME, &waitEnd);
avg_wait_time+=(waitEnd.tv_sec+waitEnd.tv_nsec/BILLION);
struct timespec talkStart, talkEnd;
clock_gettime(CLOCK_REALTIME, &talkStart);
int how_many_seats=rand_r(&seed);
seed=how_many_seats;
how_many_seats=how_many_seats%(N_seathigh-N_seatlow)+N_seatlow;
int how_many_seconds=rand_r(&seed);
seed=how_many_seconds;
how_many_seconds=how_many_seconds%(t_seathigh-t_seatlow)+t_seatlow;
sleep(how_many_seconds);
if(seats==N_seat){
    pthread_mutex_lock(&lock_screen);
    printf("%d Reservation cancelled because the theater is full\n",*(int*)vargc);
    pthread_mutex_unlock(&lock_screen);
}else if(seats+how_many_seats>N_seat){
    pthread_mutex_lock(&lock_screen);
    printf("%d Reservation cancelled because there are not enough seats available\n",*(int*)vargc);
    pthread_mutex_unlock(&lock_screen);
}else{       
    int c=0,i=0;
    pthread_mutex_lock(&lock_number_of_transfer);
    pthread_mutex_unlock(&lock_number_of_transfer);
    pthread_mutex_lock(&lock_plan);
    while(c<how_many_seats){
        if(!plan[i]){
            plan[i]=tid;
            c++;
        }
        i++;
    }
    seats+=how_many_seats;
    pthread_mutex_unlock(&lock_plan);
    int card_success=rand_r(&seed);
    seed=card_success;
    float tempp=(float)card_success/(float)RAND_MAX;
    card_success=(tempp<=P_cardsuccess);
    if(!card_success){
        pthread_mutex_lock(&lock_screen);
        printf("%d Reservation cancelled because the transaction with the credit card was not accepted\n",*(int*)vargc);
        pthread_mutex_unlock(&lock_screen);
        pthread_mutex_lock(&lock_plan);
        c=0;i=0;
        while(c<how_many_seats){
            if(plan[i]==tid){
                plan[i]=0;
                c++;
            }
            pthread_mutex_lock(&lock_screen);
            pthread_mutex_unlock(&lock_screen);
            i++;
        }
        seats-=how_many_seats;
        pthread_mutex_unlock(&lock_plan);
        pthread_mutex_lock(&lock_number_of_transfer);
        pthread_mutex_unlock(&lock_number_of_transfer);
    }else{
        pthread_mutex_lock(&lock_screen);
        printf("%d Reservation completed successfully.The number of the transaction is %d, your seats are ",*(int*)vargc,*(int*)vargc);
        pthread_mutex_unlock(&lock_screen);
        c=0;i=0;
        pthread_mutex_lock(&lock_plan);
        while(c<how_many_seats){
            if(plan[i]==*(int*)vargc){
                pthread_mutex_lock(&lock_screen);
                printf("%d ",i);
                pthread_mutex_unlock(&lock_screen);
                c++;
            }               
            i++;
        }
        pthread_mutex_unlock(&lock_plan);
        pthread_mutex_lock(&lock_screen);
        printf("and the cost of the transaction is %.2f euros\n",how_many_seats*C_seat);
        pthread_mutex_unlock(&lock_screen);
        pthread_mutex_lock(&lock_bank_account);
        bank_account+=how_many_seats*C_seat;
        pthread_mutex_unlock(&lock_bank_account);
    }
}
//we assume that the client is fully served when we have also printed out his/her result of the try to book seats
clock_gettime(CLOCK_REALTIME, &talkEnd);
double cow = ( talkEnd.tv_sec - talkStart.tv_sec ) + ( talkEnd.tv_nsec - talkStart.tv_nsec ) / BILLION;
pthread_mutex_lock(&lock_service_time);
avg_service_time+=cow;
pthread_mutex_unlock(&lock_service_time);
pthread_mutex_lock(&lock_phone);
telefoners++;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&lock_phone);
pthread_exit(0);
}

int main(int argc,char* argv[]){
int i;
for(i=0;i<N_seat;i++){
    plan[i]    = 0;
}
//if user did not give the correct number of arguments
if(argc!=3){
    printf("Wrong number of arguments\n");
    return -1;
}

int N_cust=atoi(argv[1]),tel_available=N_tel,err;
i=0;
seed=atoi(argv[2]);
pthread_t *threads=(pthread_t*)malloc(N_cust*sizeof(pthread_t));
int threadid[N_cust];

//if we can't init one of the mutexes
pthread_mutex_init(&lock_phone, NULL);
pthread_mutex_init(&lock_bank_account, NULL);
pthread_mutex_init(&lock_number_of_transfer, NULL);
pthread_mutex_init(&lock_wait_time, NULL);
pthread_mutex_init(&lock_service_time, NULL);
pthread_mutex_init(&lock_plan, NULL);
pthread_mutex_init(&lock_screen, NULL);

//creating the threads
while(i<N_cust){
threadid[i]=i+1;
    err = pthread_create(&(threads[i]), NULL, AwesomeThreadFunction, (void*)&threadid[i]); //func name
    if (err){
        printf("Thread can't be created :[%s]\n", strerror(err));
    }
    i++;
}

//join the threads
void *status;
for (i = 0; i < N_cust; i++) {

    rc = pthread_join(threads[i], &status);

    if (rc != 0) {
        printf("ERROR: return code from pthread_join() is %d\n", rc);
        exit(-1);       
    }

    printf("Main: Thread %lu finished with status %d.\n", threads[i], *(int *)status);
}

//final output
for(i=0;i<N_seat;++i){
    if(plan[i]){
        printf("Seat %d / client %d\n",i+1,plan[i]);
    }
}
printf("Total revenue from sales:\t%d\n",bank_account);
printf("Average    waiting time:\t%f\n",(float)avg_wait_time/(float)N_cust);
printf("Average service time:\t%f\n",(float)avg_service_time/(float)N_cust);

free(threads);
//Destroy mutexes
pthread_mutex_destroy(&lock_phone);
pthread_mutex_destroy(&lock_bank_account);
pthread_mutex_destroy(&lock_number_of_transfer);
pthread_mutex_destroy(&lock_wait_time);
pthread_mutex_destroy(&lock_service_time);
pthread_mutex_destroy(&lock_plan);
pthread_mutex_destroy(&lock_screen);
pthread_cond_destroy(&cond);
return 0;
}

導致段錯誤的直接問題在於(省略了無關的細節):

    if(seats+how_many_seats>N_seat) {
        ....
    } else {       
        int c=0,i=0;
        pthread_mutex_lock(&lock_plan);
        while(c<how_many_seats) {
            if(!plan[i]){
                plan[i]=tid;
                c++;
            }
            i++;
        }
        seats+=how_many_seats;
        pthread_mutex_unlock(&lock_plan);

代碼確定它是否能滿足請求,並愉快地繼續預訂座位。 只有這樣它才會鎖定計划。 同時,在測試seats+how_many_seats>N_seat和鎖定計划之間,另一個線程做同樣的seats+how_many_seats>N_seat並修改計划。 之后,可用座位數少於第一個線程所需的數量,而while(c<how_many_seats)循環訪問plan越界。

我沒有檢查其余的; 我期待其他類似的問題。 非易失性全局變量非常可疑。 在任何情況下,幫自己一個忙,並使用更多的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM