2013-10-09 3 views
0

이 세그먼트 오류 오류를 해결하려고하지만 이해가되지 않습니다. 내 buffer.c에서 그것은 deposit() 메서드에서 세그먼트 오류 오류를 던지고있다, 나는 그것이 일어나고 있다고 생각하는 의견을 나타냅니다.다중 스레드를 사용하는 세그먼트 오류

buffer.c에

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "buffer.h" 
#include "st.h" 
#include "semaphore.h" 

void c_deposit(buffer *buffer, char c); 
int c_remove(buffer *buffer); 

buffer *init_buffer(int size); 

buffer *init_buffer(int size) 
{ 

    buffer *new_Buffer; 
    new_Buffer=malloc((sizeof(buffer))); 

    semaphore *sem; 
    sem=malloc(sizeof(semaphore)); 
    new_Buffer->emptyBuffer=sem; 
    createSem(new_Buffer->emptyBuffer, size); 


    semaphore *sem2; 
    sem2=malloc(sizeof(semaphore)); 

    new_Buffer->fullBuffer=sem2; 
    createSem(new_Buffer->fullBuffer, 0); 

    char *array; 
    array=malloc(sizeof(char)*size); 
    new_Buffer->chars=array; 

    new_Buffer->size=size; 

    new_Buffer->nextIn=0; 
    new_Buffer->nextOut=0; 

    return new_Buffer; 
} 

void c_deposit(buffer *buffer, char c) 
{ 
    down(buffer->emptyBuffer); 
    printf("we're before assigning a char to chars, here's buffer->nextIn: %d\n",buffer->nextIn); 
    //nextIn will print here 
    buffer->chars[buffer->nextIn]=c; 
    //"c" is also not being assigned to chars 
    printf("buffer size is equal to: %d\n", buffer->size); 

    printf("nextIn is: %d",buffer->nextIn); 
    //^line above is never reached. I'm assuming it's faulting on nextIn 

    buffer->nextIn=(buffer->nextIn+1)%(buffer->size); 
    printf("we're right before up()"); 
    up(buffer->fullBuffer); 
    printf("we made it to the end\n"); 
} 
int c_remove(buffer *buffer) 
{ 
    int c; 
    down(buffer->fullBuffer); 
    c=buffer->chars[buffer->nextOut]; 
    buffer->nextOut=(buffer->nextOut+1)%buffer->size; 
    up(buffer->emptyBuffer); 
    return c; 
} 

아래 코드는 여기에 return 전에뿐만 아니라

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include "st.h" 
#include "buffer.h" 
#include "semaphore.h" 

#define MAX_CHARS 81 
#define BUFF_SIZE 12 
#define NULL_CHAR 
typedef struct { 
}ThreadInit; 

static buffer *buffer1; 
static buffer *buffer2; 
static buffer *buffer3; 

void *Thread1(); 
void *Thread2(); 
void *Thread3(); 
void *Thread4(); 

int main(int argc, char const *argv[]) 
{ 
    buffer1=init_buffer(BUFF_SIZE); 
    buffer2=init_buffer(BUFF_SIZE); 
    buffer3=init_buffer(BUFF_SIZE); 
    if (st_thread_create(Thread1(), NULL, 0, 0) == NULL) { 
     perror("st_thread_create failed for thread 1"); 
     exit(EXIT_FAILURE); 
    } 

    if (st_thread_create(Thread2(), NULL, 0, 0) == NULL) { 
     perror("st_thread_create failed for thread 2"); 
     exit(EXIT_FAILURE); 
    } 
    if (st_thread_create(Thread3(), NULL, 0, 0) == NULL) { 
     perror("st_thread_create failed for thread 3"); 
     exit(EXIT_FAILURE); 
    } 
    if (st_thread_create(Thread4(), NULL, 0, 0) == NULL) { 
     perror("st_thread_create failed for thread 4"); 
     exit(EXIT_FAILURE); 
    } 


    return 0; 
} 
void *Thread1() 
{ 
    int c; 
    while (1) 
    { 
     c=fgetc(stdin); 
    printf("We got a character from the the input, it's %c\n",c); 
     c_deposit(buffer1,c); 
    printf("We deposited a char\n"); 
     if(c==EOF) 
     { 
      break; 
     } 
    } 
    st_thread_exit(NULL); 
    return NULL; 
} 
void *Thread2(void *state) 
{ 
    printf("We made it to Thread2\n"); 
    int c; 
    while(1) 
    { 

     c=c_remove(buffer1); 
     if(c==EOF) 
     { 
      break; 
     } 
     if(c=='\n') 
     { 
      c=' '; 
     } 
     c_deposit(buffer2,c); 
    } 
    st_thread_exit(NULL); 
    return NULL; 

} 
void *Thread3(void *state) 
{ 
    int c; 
    while(1) 
    { 
     c=c_remove(buffer2); 
     if(c==EOF) 
     { 
      break; 
     } 
     if(c=='*' && c_remove(buffer2)=='*') 
     { 
      c_remove(buffer2); 
      c='^'; 
      c_deposit(buffer3,c); 
     } 
     else 
     { 
      c_deposit(buffer3,c); 
     } 
    } 
    st_thread_exit(NULL); 
    return NULL; 
} 
void *Thread4(void *state) 
{ 
    int counter=0; 
    int c; 
    char output[MAX_CHARS]; 
    output[MAX_CHARS-1]='\0'; 
    while(1) 
    { 
     c=c_remove(buffer3); 
     if(c==EOF) 
     { 
      break; 
     } 
     else 
     { 
      output[counter]=c; 
      if(counter==80) 
      { 
       printf("%s\n",output); 
       counter=-1; 
       memset(output,'\0',BUFF_SIZE); 
      } 
      counter++; 
     } 
    } 
    st_thread_exit(NULL); 
    return NULL; 
} 
+0

'buffer.h'와'semaphore.h '에 관련된 정의와 헤더, 특히'down()'과'up (')', 그들은 분명히 당신이 스레드 동기화를 위해 사용하고있는 메커니즘이기 때문입니다. . 디버거에서 실행하십시오. 'gdb'는 행복하게 프로세스를 멈추고 디버그 정보로 컴파일하면 오류가있는 곳을 정확하게 알려줍니다. 내가 추측 할 수 있듯이, 카운터에 대한 보호 장치가 없으며, 특히 'buffer-> nextIn = (buffer-> nextIn + 1) % (buffer-> size) '. – WhozCraig

+0

읽기/쓰기를 동기화하지 않기 때문에 bufferX에 경쟁 조건이있는 것으로 보입니다. –

+0

그들은 세마포어이기 때문에 deposit/remove에서 down()과 up()을 통해 동기화됩니다 – sreya

답변

0
내가 메인의 시작 부분에 st_int() 메인의 끝으로 다음 st_thread_exit(NULL); (실종됐다

내 main.c에의). st_int()을 사용하면 스레드 라이브러리가 초기화되지 않고 exit()없이 스레드가 어떤 이유로 실행되지 않습니다. 논리의 나머지 부분은 정확하다

관련 문제