2016-07-04 100 views
0

为什么我运行我的代码时发生此错误? 错误:RUN FINISHED; Segmentation fault: 11; real time: 3s; user: 0ms; system: 0mC程序设计分割故障:11线程问题

我创建了10个线程,其中每个线程都是卖家。有一个10by10阵列可以容纳票的座位。根据售票员的类型,一个人将被卖给特定的座位。

问题与pthreads?

/* 
* 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); 
} 
+0

分段错误正在访问只读内存,访问不可访问的内存,访问空指针等 –

+0

为什么包含一堆完全没有使用的代码? – 2501

+0

每个线程都将拥有自己的队列,我们​​将等待购买门票的“客户”。这就是为什么目前代码没有被使用。 – Jwoozy

回答

0

正如@ 2501所假定的那样:导致段错误的罪魁祸首是main中未初始化的变量i

我冒昧地为你的pthread创建编写了一个小例子,并增加了一些printf的功能。

它编译没有警告与

gcc -W -Wall threadtest.c -o threadtest -pthread

#include <stdlib.h> 
#include <stdio.h> 
#include <pthread.h> 

struct seller_type { 
    char st; 
    int tid; 
}; 

void *sell(void *arg) 
{ 
    struct seller_type *seller_type1 = arg; 
    int i; 
    i = 0; 
    printf("thread #%d: seller_type1->st = %c\n", seller_type1->tid, 
    seller_type1->st); 
    // no semicolon after while() 
    while (i == 0) { 
    switch (seller_type1->st) { 
    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; 
    } 
    } 
    printf("thread #%d: Work done\n", seller_type1->tid); 
    return NULL; 
} 

int main() 
{ 
    int i = 0; 
    struct seller_type *seller_type1; 

    pthread_t tids[10]; 

    seller_type1 = calloc(10, sizeof(struct seller_type)); 
    // All error handling ommitted! Yes, ALL! 

    seller_type1[0].st = 'H'; 
    seller_type1[0].tid = 0; 
    pthread_create(&tids[0], NULL, sell, &seller_type1[0]); 

    for (i = 1; i < 4; i++) { 
    seller_type1[i].st = 'M'; 
    seller_type1[i].tid = i; 
    pthread_create(&tids[i], NULL, sell, &seller_type1[i]); 
    } 

    for (i = 4; i < 10; i++) { 
    seller_type1[i].st = 'L'; 
    seller_type1[i].tid = i; 
    pthread_create(&tids[i], NULL, sell, &seller_type1[i]); 
    } 
    puts("All threads created"); 

    // wait for all seller threads to exit 
    for (i = 0; i < 10; i++) { 
    pthread_join(tids[i], NULL); 
    printf("Thread %d joined\n", i); 
    } 
    puts("All threads joined"); 

    exit(EXIT_SUCCESS); 
} 

我希望你能建立在那。

0

你有一个数据的比赛,当你通过seller_type到新创建的线程,而在同一时间修改的主要变量。

传递给pthread_create的函数的类型必须是:void*(*)(void*),而不是您正在使用的类型。

函数pthread_join需要pthread_t作为第一个参数,而不是指向该类型的指针,这是您传递给它的指针。

主体中的变量i未初始化,用于在第一个pthread_create调用中为数组tids编制索引。

所有这四个问题都会导致自定义行为。我怀疑最后一个导致崩溃。

+0

hi 2501, 我更正了pthread_join,并初始化了i。如何纠正pthread_create函数以将seller_type传递给销售线程? – Jwoozy

+0

@Jwoozy分配一些内存并将其传递给线程。 – 2501