gpt4 book ai didi

c - 创建用户输入的多个线程时出现段错误

转载 作者:行者123 更新时间:2023-11-30 17:13:36 25 4
gpt4 key购买 nike

我正在尝试使用 pthreads 和 vector 形式的缓冲区来解决生产者-消费者问题。我希望能够输入生产者和消费者的线程数量。一旦我输入这两个值,我就会遇到段错误。我正在使用 gcc 和 -lpthread 编译代码,但没有收到编译错误。如何修复此错误?

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

#define MAX 1000//00 /* Numbers to produce */
#define SIZE 20 /* Size of Buffer */

typedef struct {
int id;
} parm;

pthread_mutex_t the_mutex;
pthread_cond_t condc, condp;
int buffer[SIZE];
int c = 0;

/*
@Function: printState
@In: integer i
@Out: none

@Description: Used to show the state of the buffer on a given state
*/
void printState(int i){
int j;

puts("Showing the state of the buffer: ");
printf("[ ");
for (j = 0; j < SIZE; j++){
printf("%d ",buffer[j]);
}
printf("]\n");

}

/*
@Function: producer
@In: void *ptr
@Out: none

@Description: Call a producer on the process
*/

void* producer(void *ptr){
int i;

for (i = 1; i <= MAX; i++){
printf("calling producer\n");// on position %d.\n",c+1);
pthread_mutex_lock(&the_mutex); /* protect the buffer */

if(c == SIZE){ /* If the buffer is full, wait */
puts("The buffer is full. Waiting.");
pthread_cond_wait(&condp, &the_mutex);
}

buffer[c] = 1;
c++;

printf("There are %d occupied positions on the buffer.\n", c);
pthread_cond_signal(&condc); /* Wake up the consumer */
pthread_mutex_unlock(&the_mutex); /* Release the buffer */

//if(i == MAX/2){
// printState(i);
//}

}
pthread_exit(0);
}

/*
@Function: consumer
@In: void *ptr
@Out: none

@Description: Call a consumer on the process
*/
void* consumer(void *ptr) {
int i, j;

for (i = 1; i <= MAX; i++){
printf("calling consumer\n");// on position %d\n", c+1);
pthread_mutex_lock(&the_mutex); /* protect the buffer */
if (c == 0){ /* If there is nothing in the buffer, wait */
puts("Buffer is empty. Waiting.");
pthread_cond_wait(&condc, &the_mutex);
}
buffer[c] = 0;
c--;
printf("There are %d occupied positions on the buffer.\n", c);

pthread_cond_signal(&condp); /* wake up consumer */
pthread_mutex_unlock(&the_mutex); /* release the buffer */

//if(i == MAX){
// printState(i);
//}

}
pthread_exit(0);
}

/*
@Function: main
@In: integer argc and character **argv
@Out: none

@Description: Main function of the algorithm
*/
int main(int argc, char **argv){
pthread_t *pro_threads, *con_threads;
pthread_attr_t pro_pthread_custom_attr, con_pthread_custom_attr;
int i, M, N;
parm *p_pro, *p_con;

puts("Please, enter the number of producer threads:");
scanf("%d",&N);

puts("Please, enter the number of consumer threads:");
scanf("%d",&M);

for(i=0;i<SIZE;i++){
buffer[i] = 0;
}

// Allocate space for the threads

pro_threads=(pthread_t *)malloc(N*sizeof(*pro_threads));
pthread_attr_init(&pro_pthread_custom_attr);
con_threads=(pthread_t *)malloc(M*sizeof(*con_threads));
pthread_attr_init(&con_pthread_custom_attr);

// Initialize the mutex and condition variables

pthread_mutex_init(&the_mutex, NULL); /* Initialize the mutex */
pthread_cond_init(&condc, NULL); /* Initialize the consumer condition variable */
pthread_cond_init(&condp, NULL); /* Initialize the producer condition variable */

// Create the threads

for (i=0; i<N; i++){
p_pro[i].id=i;
pthread_create(&pro_threads[i], &pro_pthread_custom_attr, producer, (void *)(p_pro+i));
}

for (i=0; i<M; i++){
p_con[i].id=i;
pthread_create(&con_threads[i], &con_pthread_custom_attr, consumer, (void *)(p_con+i));
}

// Wait for the threads to finish.
// Otherwise main might run to the end
// and kill the entire process when it exits.

for (i=0; i<N; i++){

pthread_join(pro_threads[i], NULL);
}

for (i=0; i<M; i++){

pthread_join(con_threads[i], NULL);
}

// Cleanup -- would happen automatically at the end of program

pthread_mutex_destroy(&the_mutex); /* Free up the_mutex */
pthread_cond_destroy(&condc); /* Free up the consumer condition variable */
pthread_cond_destroy(&condp); /* Free up the producer condition variable */
free(p_pro);
free(p_con);
return 0;
}

最佳答案

这是大学类(class)还是其他类(class)?

如果您要求编译器 (gcc) 启用警告,该问题就会被报告。无论是谁在“教”你,都应该告诉你这一点。

meh.c: In function ‘printState’: meh.c:25:21: warning: unused parameter ‘i’ [-Wunused-parameter] void printState(int i){ ^ meh.c: In function ‘producer’: meh.c:47:22: warning: unused parameter ‘ptr’ [-Wunused-parameter] void* producer(void ptr){ ^ meh.c: In function ‘consumer’: meh.c:85:12: warning: unused variable ‘j’ [-Wunused-variable] int i, j; ^ meh.c:84:22: warning: unused parameter ‘ptr’ [-Wunused-parameter] void consumer(void *ptr) { ^ meh.c: In function ‘main’: meh.c:118:14: warning: unused parameter ‘argc’ [-Wunused-parameter] int main(int argc, char **argv){ ^ meh.c:118:27: warning: unused parameter ‘argv’ [-Wunused-parameter] int main(int argc, char **argv){ ^ meh.c:150:14: warning: ‘p_pro’ may be used uninitialized in this function [-Wmaybe-uninitialized] p_pro[i].id=i; ^ meh.c:155:14: warning: ‘p_con’ may be used uninitialized in this function [-Wmaybe-uninitialized] p_con[i].id=i;

但是,即使使用标准方法(例如到处放置 printfs 来缩小崩溃站点的范围),也可以轻松诊断问题。

因此,我很困惑到底出了什么问题。

代码有一些微不足道的错误,即使修复了段错误,它也无法工作。我将省略它们,因为它们处理的是一般问题。

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

#define MAX 1000//00 /* Numbers to produce */
#define SIZE 20 /* Size of Buffer */

typedef struct {
int id;
} parm;

pthread_mutex_t the_mutex;
pthread_cond_t condc, condp;
int buffer[SIZE];
int c = 0;

它已经是 0。对于全局变量来说,这是一个糟糕的非描述性名称。 //-------------------------------------------------------- --------------------------------------------

/* @函数:打印状态 @In:整数i @Out:无

    @Description: Used to show the state of the buffer on a given state
*/
void printState(int i){

未使用错误命名的参数。

    int j;

习惯用法是使用“i”作为循环索引。

    puts("Showing the state of the buffer: ");
printf("[ ");
for (j = 0; j < SIZE; j++){
printf("%d ",buffer[j]);
}
printf("]\n");

}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/*
@Function: producer
@In: void *ptr
@Out: none

@Description: Call a producer on the process
*/

void* producer(void *ptr){

“*”的位置不一致。将其与类型名称放在一起很糟糕。

    int i;

for (i = 1; i <= MAX; i++){

'i' 不在循环内使用,因此实际值并不重要。习惯用法是从 0 到 < MAX。

        printf("calling producer\n");// on position %d.\n",c+1);
pthread_mutex_lock(&the_mutex); /* protect the buffer */

if(c == SIZE){ /* If the buffer is full, wait */
puts("The buffer is full. Waiting.");
pthread_cond_wait(&condp, &the_mutex);
}

buffer[c] = 1;
c++;

printf("There are %d occupied positions on the buffer.\n", c);
pthread_cond_signal(&condc); /* Wake up the consumer */
pthread_mutex_unlock(&the_mutex); /* Release the buffer */

//if(i == MAX/2){
// printState(i);
//}

}
pthread_exit(0);
}
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
/*
@Function: consumer
@In: void *ptr
@Out: none

@Description: Call a consumer on the process
*/
void* consumer(void *ptr) {
int i, j;

for (i = 1; i <= MAX; i++){
printf("calling consumer\n");// on position %d\n", c+1);
pthread_mutex_lock(&the_mutex); /* protect the buffer */
if (c == 0){ /* If there is nothing in the buffer, wait */
puts("Buffer is empty. Waiting.");
pthread_cond_wait(&condc, &the_mutex);
}
buffer[c] = 0;
c--;
printf("There are %d occupied positions on the buffer.\n", c);

pthread_cond_signal(&condp); /* wake up consumer */
pthread_mutex_unlock(&the_mutex); /* release the buffer */

//if(i == MAX){
// printState(i);
//}

}
pthread_exit(0);
}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/*
@Function: main
@In: integer argc and character **argv
@Out: none

@Description: Main function of the algorithm
*/
int main(int argc, char **argv){
pthread_t *pro_threads, *con_threads;
pthread_attr_t pro_pthread_custom_attr, con_pthread_custom_attr;
int i, M, N;

全大写的名称通常与宏一起使用。可怕的非描述性名称。

    parm *p_pro, *p_con;

puts("Please, enter the number of producer threads:");
scanf("%d",&N);

puts("Please, enter the number of consumer threads:");
scanf("%d",&M);

我不知道谁以及为什么建议初学者使用这个。请改用 argv。

    for(i=0;i<SIZE;i++){
buffer[i] = 0;
}

该缓冲区已归零。与之前使用的间距不一致。

    // Allocate space for the threads

pro_threads=(pthread_t *)malloc(N*sizeof(*pro_threads));

强制转换 malloc 是非常有害的。

    pthread_attr_init(&pro_pthread_custom_attr);
con_threads=(pthread_t *)malloc(M*sizeof(*con_threads));
pthread_attr_init(&con_pthread_custom_attr);

// Initialize the mutex and condition variables

pthread_mutex_init(&the_mutex, NULL); /* Initialize the mutex */
pthread_cond_init(&condc, NULL); /* Initialize the consumer condition variable */
pthread_cond_init(&condp, NULL); /* Initialize the producer condition variable */

// Create the threads

for (i=0; i<N; i++){
p_pro[i].id=i;

p_pro 未初始化。

        pthread_create(&pro_threads[i], &pro_pthread_custom_attr, producer, (void *)(p_pro+i));

缺少错误检查。 p_pro 使用不一致。

    }

for (i=0; i<M; i++){
p_con[i].id=i;
pthread_create(&con_threads[i], &con_pthread_custom_attr, consumer, (void *)(p_con+i));
}

// Wait for the threads to finish.
// Otherwise main might run to the end
// and kill the entire process when it exits.

for (i=0; i<N; i++){

pthread_join(pro_threads[i], NULL);
}

for (i=0; i<M; i++){

pthread_join(con_threads[i], NULL);
}

// Cleanup -- would happen automatically at the end of program

pthread_mutex_destroy(&the_mutex); /* Free up the_mutex */
pthread_cond_destroy(&condc); /* Free up the consumer condition variable */
pthread_cond_destroy(&condp); /* Free up the producer condition variable */
free(p_pro);
free(p_con);
return 0;
}

关于c - 创建用户输入的多个线程时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30716876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com