【问题标题】:Segmentation fault using multiple threads使用多个线程的分段错误
【发布时间】:2013-10-09 05:27:43
【问题描述】:

我正在尝试解决此分段错误错误,但我不明白。在我的buffer.c 中,它在deposit() 方法中引发了分段错误错误,我有 cmets 来指示我认为它发生的位置。代码如下

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

这也是我的 main.c

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

【问题讨论】:

  • 包括buffer.hst.hsemaphore.h的相关定义和标题,特别是down()up()的缺失代码,因为它们显然是你的机制用于线程同步。 .并在调试器下运行它。 gdb 会很高兴地停止该过程并告诉您确切地如果您使用调试信息进行编译,则故障几乎完全在哪里。如果我猜的话,我会说你在柜台上没有任何保护,并且随之而来的是竞争条件,特别是像buffer-&gt;nextIn=(buffer-&gt;nextIn+1)%(buffer-&gt;size)这样的事情。
  • 您似乎对 bufferX 有竞争条件,因为您没有同步读取/写入它。
  • 它们是通过 down() 和 up() 在存款/删除中同步的,因为它们是信号量

标签: c multithreading segmentation-fault


【解决方案1】:

我在 main 的开头缺少 st_int(),然后在 main 的结尾缺少 st_thread_exit(NULL);(在 return 之前)。使用st_int() 时,线程库不会被初始化,如果没有exit(),无论出于何种原因,线程都不会运行。其余逻辑正确

【讨论】:

    猜你喜欢
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 2016-02-09
    相关资源
    最近更新 更多