2016-07-04 10 views
0

Warum tritt dieser Fehler auf, wenn ich meinen Code ausführe? Fehler: RUN FINISHED; Segmentation fault: 11; real time: 3s; user: 0ms; system: 0mC-Programmierung Segmentierungsfehler: 11 Thread-Probleme

Ich erstelle 10 Threads, bei denen jeder Thread ein Ticketverkäufer ist. Es gibt ein Array 10by10, das die Sitze der Karten hält. Abhängig von der Art des Ticketverkäufers wird eine Person für diesen bestimmten Sitzplatz verkauft.

Ist das Problem mit den 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

Segmentierungsfehler greift auf schreibgeschützten Speicher zu, greift auf nicht zugreifbaren Speicher zu, greift auf Nullzeiger zu und so weiter –

+0

Warum haben Sie eine Menge Code eingefügt, der überhaupt nicht verwendet wird? – 2501

+0

Jeder Thread wird seine eigene Warteschlange haben, wo "Kunden" wir warten werden, um Tickets zu kaufen. Deshalb wird der Code im Moment überhaupt nicht verwendet. – Jwoozy

Antwort

0

Wie bereits von @ 2501 angenommen: Der Schuldige, der den Segfault verursacht hat, war die nicht initialisierte Variable i in main.

Ich habe mir die Freiheit genommen, ein minimales Beispiel für Ihre Pthread-Erstellung zu schreiben, mit der Zugabe von einigen Printf`s.

Es kompiliert ohne Warnungen mit

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); 
} 

Ich hoffe, Sie auf das aufbauen können.

0

Sie haben ein Daten Rennen, wenn Sie seller_type in den neu erstellten Thread übergeben, während zur gleichen Zeit die Variable in der Haupt ändern.

Der Typ der Funktion, die an pthread_create übergeben wird, muss sein: void*(*)(void*), nicht der Typ, den Sie verwenden.

Funktion pthread_join erfordert einen Typ pthread_t als das erste Argument, kein Zeiger auf diesen Typ, was Sie übergeben werden.

Die Variable i in der Hauptsache ist nicht initialisiert und wird verwendet, um das Array tids im ersten Anruf pthread_create zu indizieren.

Alle diese vier Probleme verursachen selbst undefiniertes Verhalten. Ich vermute, dass der letzte den Absturz verursacht.

+0

hi 2501, Ich korrigierte pthread_join und initialisierte i. Wie korrigiere ich die Funktion pthread_create, um den seller_type an den Verkaufsthread weiterleiten zu können? – Jwoozy

+0

@Jwoozy Weisen Sie etwas Speicher zu und übergeben Sie ihn dem Thread. – 2501

Verwandte Themen