2015-04-04 52 views
0

我正在尝试编写一个使用3个线程和共享内存的程序。共享内存是一个有101个值的数组。第一个值共享内存[0](初始化为0)是状态值,它决定了哪个操作应该发生。三个线程都做多线程程序中的段错误

  1. 第一个应该用100个随机值填充共享内存数组。并将状态值设置为1.

  2. 第二个应打印100个随机值(从索引1到100)的乘积。并将状态值设置为2.第三个应打印100个随机变量的平均值。

  3. 第三个应打印100个随机变量的平均值。并将状态值设置为0.以便线程1用不同的随机变量填充共享内存。

这是我的代码

#include <stdio.h> 
#include <sys/types.h> 
#include <sys/ipc.h> 
#include <sys/shm.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <time.h> 
#include <pthread.h> 

unsigned int product=0; 
float avg=0; 
int* shared_memory; 
int status=0; 

void productAllThread(); 
void averageAllThread(); 
void *parentProcess(); 
void *prodAll(); 
void *avgAll(); 
void initializeArray(); 

int main(int argc, const char * argv[]) 
{ 

    time_t t; 
    key_t key = 9876; 

    // Create shared memory area 
    int shm_id = shmget(key, sizeof(int)*101, IPC_CREAT | 0666); 

    // initialize the random variable 
    srand((unsigned) time(&t)); 

    // Create shared memory 
    shared_memory=shmat(shm_id, NULL, 0); 

    //create threads 
    pthread_t tid1, tid2, tid3; 
    pthread_attr_t attr; 
    pthread_attr_init(&attr); 

    pthread_create(&tid1, &attr, parentProcess, NULL); 
    pthread_create(&tid2, &attr, prodAll, NULL); 
    pthread_create(&tid3, &attr, avgAll, NULL); 

    pthread_join(tid1, NULL); 
    pthread_join(tid2, NULL); 
    pthread_join(tid3, NULL); 


    return 0; 
} 

void initializeArray() { 
    shared_memory[0]=0; 
    status=shared_memory[0]; 
    int i= 0; 
    printf("Initial Array:{"); 
    for(i=1; i<100; i++) 
    { 
     shared_memory[i]=rand()% 50; 
     printf("%d,", shared_memory[i]); 
    } 
    printf("}\n"); 
} 

void *parentProcess() 
{ 
    while(1) 
    { 
     status=shared_memory[0]; 
     if(status==0) { 
      // initialize array 
      initializeArray(); 
      shared_memory[0]=1; 
     } else { 
      sleep(10); 
     } 
    } 
} 

void averageAllThread() { 
    while(1) { 
     status=shared_memory[0]; 
     if(status==2) 
     { 
      avgAll(); 
      wait(NULL); 
      printf("Avg:%.2f\n", avg); 
      shared_memory[0]=0; 
     } else { 
      sleep(5); 
     } 
    } 
} 
void productAllThread() { 
    while(1){ 
     status=shared_memory[10]; 
     if (status==1) 
     { 
      prodAll(); 
      wait(NULL); 
      printf("Sum:%d\n",product); 
      shared_memory[0]=2; 
     } else { 
      sleep(5); 
     } 

    } 
} 

void *prodAll() 
{ 
    while(1){ 
     int i=1; 
     product=0; 
     for(i=1; i<100; i++) 
     { 
      product=product+shared_memory[i]; 
     } 
    } 
} 

void *avgAll() 
{ 
    while(1){ 
     int i=0; 
     avg=0; 
     for(i=1; i<100; i++) 
     { 
      avg=avg+shared_memory[i]; 
     } 
     avg=avg/100; 
    } 
} 

当我在终端运行它,它给了我这个错误

“段错误:11”

什么可能导致这种类型的的错误?如果这个错误得到解决,程序能够正常工作来完成我想要做的工作吗?

+0

'pthread_t tid1'未初始化 – baf 2015-04-04 08:55:56

+0

初始化更正。但我仍然有同样的问题 – 2015-04-04 09:01:53

+0

你有四个线程,顺便说一句。 – 2015-04-04 09:05:33

回答

0

我发现在你的程序了几个问题:

  1. 您所呼叫的错功能,启动线程:

    pthread_create(&tid1, &attr, parentProcess, NULL); 
    pthread_create(&tid2, &attr, prodAll, NULL); 
    pthread_create(&tid3, &attr, avgAll, NULL); 
    

    应该是:

    pthread_create(&tid1, &attr, parentProcess, NULL); 
    pthread_create(&tid2, &attr, productAllThread, NULL); 
    pthread_create(&tid3, &attr, averageAllThread, NULL); 
    
  2. 你有几个电话wait()像这样:

    wait(NULL); 
    

    您应该删除所有这些。

  3. 应删除avgAll()prodAll()中的while循环,因为这些函数的调用者中已经存在while循环。

  4. 电话srand()应该从parentProcess()作出,否则它可能不会影响该线程中的rand()调用。