创建用户输入的多个线程时出现分段错误
Segmentation fault while creating a number of threads entered by the user
我正在尝试使用 pthreads 和向量形式的缓冲区来解决生产者-消费者问题。我希望能够输入我将拥有的生产者和消费者的线程数量。输入这两个值后,我立即收到分段错误。我正在使用 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;
}
这是大学课程还是什么?
如果您要求它启用警告,编译器 (gcc) 就会报告该问题。谁是 "teaching" 你应该告诉你的。
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。全局变量的可怕的非描述性名称。
//----------------------------------------------------------------------------
-
/*
@Function: 打印状态
@In: 整数 i
@Out: none
@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;
}
我正在尝试使用 pthreads 和向量形式的缓冲区来解决生产者-消费者问题。我希望能够输入我将拥有的生产者和消费者的线程数量。输入这两个值后,我立即收到分段错误。我正在使用 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;
}
这是大学课程还是什么?
如果您要求它启用警告,编译器 (gcc) 就会报告该问题。谁是 "teaching" 你应该告诉你的。
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。全局变量的可怕的非描述性名称。
//----------------------------------------------------------------------------
-
/* @Function: 打印状态 @In: 整数 i @Out: none
@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;
}