首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C编程分段错误: 11个线程问题

C编程分段错误: 11个线程问题
EN

Stack Overflow用户
提问于 2016-07-05 04:54:36
回答 0查看 958关注 0票数 0

当我运行我的代码时为什么会发生这个错误?错误:RUN FINISHED; Segmentation fault: 11; real time: 3s; user: 0ms; system: 0m

我创建了10个线程,每个线程都是一个售票者。有一个10x10的数组,用来存放门票的座位。根据售票者的类型,一个人将被卖给那个特定的座位。

问题出在pthread上吗?

代码语言:javascript
复制
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   ticketsellers.c
 * Author: iantheflyinghawaiian
 *
 * Created on July 4, 2016, 11:27 AM
 */

#include <stdio.h>
#include <pthread.h>
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// seller thread to serve one time slice (1 minute)

int theatre[10][10] ;

struct node
{
    int info;
    struct node *ptr;
}*front,*rear,*temp,*front1;

int count = 0;

/* Create an empty queue */
void create()
{
    front = rear = NULL;
}

/* Returns queue size */
void queuesize()
{
    printf("\n Queue size : %d", count);
}

/* Enqueing the queue */
void enq(int data)
{
    if (rear == NULL)
    {
        rear = (struct node *)malloc(1*sizeof(struct node));
        rear->ptr = NULL;
        rear->info = data;
        front = rear;
    }
    else
    {
        temp=(struct node *)malloc(1*sizeof(struct node));
        rear->ptr = temp;
        temp->info = data;
        temp->ptr = NULL;

        rear = temp;
    }
    count++;
}

/* Displaying the queue elements */
void display()
{
    front1 = front;

    if ((front1 == NULL) && (rear == NULL))
    {
        printf("Queue is empty");
        return;
    }
    while (front1 != rear)
    {
        printf("%d ", front1->info);
        front1 = front1->ptr;
    }
    if (front1 == rear)
        printf("%d", front1->info);
}

/* Dequeing the queue */
void deq()
{
    front1 = front;

    if (front1 == NULL)
    {
        printf("\n Error: Trying to display elements from empty queue");
        return;
    }
    else
        if (front1->ptr != NULL)
        {
            front1 = front1->ptr;
            printf("\n Dequed value : %d", front->info);
            free(front);
            front = front1;
        }
        else
        {
            printf("\n Dequed value : %d", front->info);
            free(front);
            front = NULL;
            rear = NULL;
        }
        count--;
}

/* Returns the front element of queue */
int frontelement()
{
    if ((front != NULL) && (rear != NULL))
        return(front->info);
    else
        return 0;
}

/* Display if queue is empty or not */
void empty()
{
     if ((front == NULL) && (rear == NULL))
        printf("\n Queue empty");
    else
       printf("Queue not empty");
}



//Ticket Seller
void * sell(char *seller_type)
{
    char seller_type1;
    seller_type1 = *seller_type;
    int i;
    i = 0;
 while (i == 0);
 {
 pthread_mutex_lock(&mutex);
 pthread_cond_wait(&cond, &mutex);
 pthread_mutex_unlock(&mutex);
 // Serve any buyer available in this seller queue that is ready
 // now to buy ticket till done with all relevant buyers in their queue
 //………………
 // Case statements for seller_types
    switch(seller_type1)
    {
        case 'H' :
            printf("Seller type: H\n");
            i = 1;
            break;
        case 'M' :
            printf("Seller type: M\n");
            i = 1;
            break;
        case 'L' :
            printf("Seller type: L\n");
            i = 1;
            break;
    }
 }
 return NULL; // thread exits
}
void wakeup_all_seller_threads()
{
 pthread_mutex_lock(&mutex);
 pthread_cond_broadcast(&cond);
 pthread_mutex_unlock(&mutex);
}
int main()
{
 int i, N;
 pthread_t tids[10];
 printf("Enter N value of Customers: ");
 scanf("%d", &N);
 printf("Number of Customers: %d", N);

 char seller_type;
 // Create necessary data structures for the simulator.
 // Create buyers list for each seller ticket queue based on the
 // N value within an hour and have them in the seller queue.
 // Create 10 threads representing the 10 sellers.
 seller_type = 'H';
 pthread_create(&tids[i], NULL, sell, &seller_type);
 seller_type = 'M';

 for (i = 1; i < 4; i++)
 pthread_create(&tids[i], NULL, sell, &seller_type);
 seller_type = 'L';
 for (i = 4; i < 10; i++)
 pthread_create(&tids[i], NULL, sell, &seller_type);
 // wakeup all seller threads
 wakeup_all_seller_threads();

 // wait for all seller threads to exit
 for (i = 0 ; i < 10 ; i++)
    pthread_join(&tids[i], NULL);
 // Printout simulation results
 //…………
 exit(0);
}
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38192108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档