麻烦在Ubuntu WSL中使用GCC编译.C和标头文件

发布于 2025-01-22 03:21:47 字数 4261 浏览 3 评论 0原文

我正在尝试使用多线程编译一些C代码,由于某些原因,当我尝试运行时,我会在Ubuntu WSL终端中遇到细分故障:

gcc -o mashu concurrent_list.c concurrent_list.h

我要运行的文件如下:

concurrent_list.c:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "concurrent_list.h"

struct node {
  int value;
  node* next;
  pthread_mutex_t* lock;
  // add more fields
};

struct list {
  // add fields
  node* head;
  pthread_mutex_t* lock;
};

void print_node(node* node)
{
  // DO NOT DELETE
  if(node)
  {
    printf("%d ", node->value);
  }
}

list* create_list()
{
  // add code here
  list* l = malloc(sizeof(list));
  if(l == NULL){
    printf("malloc error");
  }
  l->head = NULL;
  l->head->next = NULL;
  if(pthread_mutex_init(l->lock, NULL) != 0){
     printf("mutex init failed\n");
  }
  if(pthread_mutex_init(l->head->lock, NULL) != 0){
    printf("mutex init failed\n");
  }
  return l;
}

void delete_list(list* list)
{
  // add code here
  pthread_mutex_lock(list->lock);
  node* head = list->head;
  node* next = head->next;
  while(next->next != NULL){
    free(head);
    next = next->next;
    head = next;
  }
  pthread_mutex_unlock(list->lock);
  free(list);
}

void insert_value(list* list, int value)
{
  // add code here

  // if the list is empty
  pthread_mutex_lock(list->lock);
  if(list->head == NULL){
    list->head->value = value;
    pthread_mutex_unlock(list->lock);
  }
  else{
    // init newnode
    node* newNode = malloc(sizeof(node));
    if(!newNode){
      printf("malloc failed\n");
    }
    newNode->value = value;
    newNode->next = NULL;
    if(pthread_mutex_init(newNode->lock, NULL) != 0){
      printf("mutex init failed\n");
    }

    node* curr = list->head;
    // lock the list and the first node 
    pthread_mutex_lock(curr->lock);
    if(curr->next == NULL){ // first and only node at the start of a list
      if(curr->value > value){  // insert the newnode at the beggining
        list->head = newNode;
        newNode->next = curr;
      }else{
        curr->next = newNode;
      }
      pthread_mutex_unlock(list->lock);
      pthread_mutex_unlock(curr->lock);
      // finished the insert
    }
    else{
      node* prev = curr;
      curr = curr->next;
      pthread_mutex_unlock(list->lock);
      pthread_mutex_lock(curr->lock);
      while(curr->value < value && curr->next != NULL){
        pthread_mutex_unlock(prev->lock);
        prev = curr;
        curr = curr->next;
        pthread_mutex_lock(curr->lock);
      }
      if(curr->next == NULL){
        curr->next = newNode;
      }else{
        prev->next = newNode;
        newNode->next = curr;
      }
      pthread_mutex_unlock(prev->lock);
      pthread_mutex_unlock(curr->lock);
    }
  }
}

void remove_value(list* list, int value)
{
  // add code here
  
  
  
}

void print_list(list* list)
{
  // add code here
  node* curr = list->head;
  pthread_mutex_lock(list->lock);
  if(curr != NULL){
    pthread_mutex_unlock(list->lock);
    while(curr != NULL){
      pthread_mutex_lock(curr->lock);
      print_node(curr);
      curr = curr->next;
      pthread_mutex_unlock(curr->lock);
    }
  }
  printf("\n"); // DO NOT DELETE
}

void count_list(list* list, int (*predicate)(int))
{
  int count = 0; // DO NOT DELETE

  // add code here

  printf("%d items were counted\n", count); // DO NOT DELETE
}

int main(){
  list* l = create_list();
  printf("1\n");
  insert_value(l,6);
  printf("2\n");
  insert_value(l,12);
  insert_value(l,3);
  insert_value(l,19);
  insert_value(l,8);
  printf("3\n");
  print_list(l);
  printf("4\n");
  delete_list(l);
}

concurrent_list.h:

typedef struct node node;
typedef struct list list;

list* create_list();
void delete_list(list* list);
void print_list(list* list);
void insert_value(list* list, int value);
void remove_value(list* list, int value);
void count_list(list* list, int (*predicate)(int));

编译时抛出的错误是:

分割故障

是我访问非法内存,无法正确编译还是使用MUTEX线程错误?

任何帮助都将被应用。

I am trying to compile some c code with multithreading and for some reason I'm getting a segmentation fault in the Ubuntu WSL terminal when I try to run:

gcc -o mashu concurrent_list.c concurrent_list.h

The files I am trying to run are the following:

concurrent_list.c:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "concurrent_list.h"

struct node {
  int value;
  node* next;
  pthread_mutex_t* lock;
  // add more fields
};

struct list {
  // add fields
  node* head;
  pthread_mutex_t* lock;
};

void print_node(node* node)
{
  // DO NOT DELETE
  if(node)
  {
    printf("%d ", node->value);
  }
}

list* create_list()
{
  // add code here
  list* l = malloc(sizeof(list));
  if(l == NULL){
    printf("malloc error");
  }
  l->head = NULL;
  l->head->next = NULL;
  if(pthread_mutex_init(l->lock, NULL) != 0){
     printf("mutex init failed\n");
  }
  if(pthread_mutex_init(l->head->lock, NULL) != 0){
    printf("mutex init failed\n");
  }
  return l;
}

void delete_list(list* list)
{
  // add code here
  pthread_mutex_lock(list->lock);
  node* head = list->head;
  node* next = head->next;
  while(next->next != NULL){
    free(head);
    next = next->next;
    head = next;
  }
  pthread_mutex_unlock(list->lock);
  free(list);
}

void insert_value(list* list, int value)
{
  // add code here

  // if the list is empty
  pthread_mutex_lock(list->lock);
  if(list->head == NULL){
    list->head->value = value;
    pthread_mutex_unlock(list->lock);
  }
  else{
    // init newnode
    node* newNode = malloc(sizeof(node));
    if(!newNode){
      printf("malloc failed\n");
    }
    newNode->value = value;
    newNode->next = NULL;
    if(pthread_mutex_init(newNode->lock, NULL) != 0){
      printf("mutex init failed\n");
    }

    node* curr = list->head;
    // lock the list and the first node 
    pthread_mutex_lock(curr->lock);
    if(curr->next == NULL){ // first and only node at the start of a list
      if(curr->value > value){  // insert the newnode at the beggining
        list->head = newNode;
        newNode->next = curr;
      }else{
        curr->next = newNode;
      }
      pthread_mutex_unlock(list->lock);
      pthread_mutex_unlock(curr->lock);
      // finished the insert
    }
    else{
      node* prev = curr;
      curr = curr->next;
      pthread_mutex_unlock(list->lock);
      pthread_mutex_lock(curr->lock);
      while(curr->value < value && curr->next != NULL){
        pthread_mutex_unlock(prev->lock);
        prev = curr;
        curr = curr->next;
        pthread_mutex_lock(curr->lock);
      }
      if(curr->next == NULL){
        curr->next = newNode;
      }else{
        prev->next = newNode;
        newNode->next = curr;
      }
      pthread_mutex_unlock(prev->lock);
      pthread_mutex_unlock(curr->lock);
    }
  }
}

void remove_value(list* list, int value)
{
  // add code here
  
  
  
}

void print_list(list* list)
{
  // add code here
  node* curr = list->head;
  pthread_mutex_lock(list->lock);
  if(curr != NULL){
    pthread_mutex_unlock(list->lock);
    while(curr != NULL){
      pthread_mutex_lock(curr->lock);
      print_node(curr);
      curr = curr->next;
      pthread_mutex_unlock(curr->lock);
    }
  }
  printf("\n"); // DO NOT DELETE
}

void count_list(list* list, int (*predicate)(int))
{
  int count = 0; // DO NOT DELETE

  // add code here

  printf("%d items were counted\n", count); // DO NOT DELETE
}

int main(){
  list* l = create_list();
  printf("1\n");
  insert_value(l,6);
  printf("2\n");
  insert_value(l,12);
  insert_value(l,3);
  insert_value(l,19);
  insert_value(l,8);
  printf("3\n");
  print_list(l);
  printf("4\n");
  delete_list(l);
}

concurrent_list.h:

typedef struct node node;
typedef struct list list;

list* create_list();
void delete_list(list* list);
void print_list(list* list);
void insert_value(list* list, int value);
void remove_value(list* list, int value);
void count_list(list* list, int (*predicate)(int));

The thrown error when compiling is:

Segmentation fault

Am I accessing illegal memory, not compiling correctly or am I using mutex threads wrong?

Any help would be appriciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌化 2025-01-29 03:21:47

create_list()

  1. l-&gt; head = null如果malloc失败,则会segfault。您可能还需要返回null;除了打印外。
  2. l-&gt; head-&gt; next = null;将始终在设置l-&gt; head null
  3. pthread_mutex_init 时(l-&gt; head-&gt; lock,null)将始终以l-&gt; headnull

insert_value()

  1. list-&gt; head-&gt; value = value;如果list> list-&gt; headnull。您甚至确保与IF语句有关。

当您在修改中锁定列表时(至少要为头更改),我消除了节点锁定。在print_list()中in Inlined print_node()作为前者所需的呼叫者,以锁定风险的锁。固定create_list()每上文。简化delete_list()。修复(上述)和简化insert_value()。删除的死代码remove_value()

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

typedef struct node node;
struct node {
    int value;
    node *next;
};
typedef struct list list;
struct list {
    node *head;
    pthread_mutex_t *lock;
};

list* create_list();
void delete_list(list* list);
list *insert_value(list* list, int value);
void print_list(list* list);

list* create_list() {
    list* l = malloc(sizeof(*l));
    if(!l) {
        printf("malloc error");
        return NULL;
    }
    l->head = NULL;
    if(pthread_mutex_init(l->lock, NULL) != 0) {
        printf("mutex init failed\n");
    }
    return l;
}

void delete_list(list *l) {
    pthread_mutex_lock(l->lock);
    while(l->head) {
        node *n = l->head;
        l->head = l->head->next;
        free(n);
    }
    free(l);
    pthread_mutex_unlock(l->lock);
}


list *insert_value(list *l, int value) {
    node* newNode = malloc(sizeof(node));
    if(!newNode){
        printf("malloc failed\n");
        return NULL;
    }
    newNode->value = value;

    // head
    pthread_mutex_lock(l->lock);
    if(!l->head || value < l->head->value){
        newNode->next = l->head;
        l->head = newNode;
        pthread_mutex_unlock(l->lock);
        return l;
    }
    // non-head
    node *n = l->head;
    for(; n->next && value >= n->next->value; n = n->next);
    newNode->next = n->next;
    n->next = newNode;
    pthread_mutex_unlock(l->lock);
    return l;
}

void print_list(list *l) {
    pthread_mutex_lock(l->lock);
    for(node *n = l->head; n; n = n->next) {
        printf("%d ", n->value);
    }
    pthread_mutex_unlock(l->lock);
    printf("\n");
}

int main(){
    list* l = create_list();
    printf("1\n");
    insert_value(l, 6);
    printf("2\n");
    insert_value(l,12);
    insert_value(l,3);
    insert_value(l,19);
    insert_value(l,8);
    printf("3\n");
    print_list(l);
    printf("4\n");
    delete_list(l);
}

输出为:

1
2
3
3 6 8 12 19 
4

create_list():

  1. l->head = NULL will segfault if malloc failed. You probably want to return NULL; in addition to the print.
  2. l->head->next = NULL; will always segfault as you set l->head to NULL
  3. pthread_mutex_init(l->head->lock, NULL) will always segfault as l->head is NULL.

insert_value():

  1. list->head->value = value; will segfault if list->head is NULL. You even ensure that it is with the if statement.

As you lock the list on modification (and you need to do that at least for the head changes) I eliminated the node lock. Inlined print_node() in print_list() as the former required caller to take a lock which is risky. Fixed create_list() per above. Simplified delete_list(). Fixed (per above) and simplified insert_value(). Removed dead code remove_value():

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

typedef struct node node;
struct node {
    int value;
    node *next;
};
typedef struct list list;
struct list {
    node *head;
    pthread_mutex_t *lock;
};

list* create_list();
void delete_list(list* list);
list *insert_value(list* list, int value);
void print_list(list* list);

list* create_list() {
    list* l = malloc(sizeof(*l));
    if(!l) {
        printf("malloc error");
        return NULL;
    }
    l->head = NULL;
    if(pthread_mutex_init(l->lock, NULL) != 0) {
        printf("mutex init failed\n");
    }
    return l;
}

void delete_list(list *l) {
    pthread_mutex_lock(l->lock);
    while(l->head) {
        node *n = l->head;
        l->head = l->head->next;
        free(n);
    }
    free(l);
    pthread_mutex_unlock(l->lock);
}


list *insert_value(list *l, int value) {
    node* newNode = malloc(sizeof(node));
    if(!newNode){
        printf("malloc failed\n");
        return NULL;
    }
    newNode->value = value;

    // head
    pthread_mutex_lock(l->lock);
    if(!l->head || value < l->head->value){
        newNode->next = l->head;
        l->head = newNode;
        pthread_mutex_unlock(l->lock);
        return l;
    }
    // non-head
    node *n = l->head;
    for(; n->next && value >= n->next->value; n = n->next);
    newNode->next = n->next;
    n->next = newNode;
    pthread_mutex_unlock(l->lock);
    return l;
}

void print_list(list *l) {
    pthread_mutex_lock(l->lock);
    for(node *n = l->head; n; n = n->next) {
        printf("%d ", n->value);
    }
    pthread_mutex_unlock(l->lock);
    printf("\n");
}

int main(){
    list* l = create_list();
    printf("1\n");
    insert_value(l, 6);
    printf("2\n");
    insert_value(l,12);
    insert_value(l,3);
    insert_value(l,19);
    insert_value(l,8);
    printf("3\n");
    print_list(l);
    printf("4\n");
    delete_list(l);
}

and the output is:

1
2
3
3 6 8 12 19 
4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文