最后一个子进程没有返回值

时间:2015-04-19 15:52:27

标签: c parallel-processing child-process

我尝试使用进程实现并行加法器。我的问题是,在创建的子进程中,最后一个子进程不返回值。否则会发生此错误。整数是一个奇数,创建的子进程数是偶数

这是我的代码:

#include <sys/ipc.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <errno.h>
#include <semaphore.h>
#include <unistd.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#define BUFFER_SIZE 200
#define BUFFER_SUB 2
typedef struct
{

        int buff[BUFFER_SIZE];
        sem_t mutex, empty, full;
}shared_data;

typedef struct
{
        int subs[BUFFER_SUB];
        sem_t mutex,empty,full;
}sub_totals;

int main(int argc, char** argv)
{

        char *file_name = argv[1];
        FILE *fp;
        int number,i;
    int j=0;
        int num_values;
        int k = atoi(argv[2]);


        int segment_id;
        size_t segment_size = sizeof(shared_data);
        segment_id = shmget(IPC_PRIVATE,segment_size,IPC_CREAT|0666);
        shared_data *shared_memory = shmat(segment_id,NULL,0);

        int segment_id1;
        size_t segment_size1 = sizeof(sub_totals);
        segment_id1 = shmget(IPC_PRIVATE,segment_size1,IPC_CREAT|0666);
        sub_totals *subtotal = shmat(segment_id1,NULL,0);

        sem_init(&shared_memory->mutex,1,1);
        sem_init(&shared_memory->empty,1,BUFFER_SIZE);
        sem_init(&shared_memory->full,1,0);

        sem_init(&subtotal->mutex,1,1);
        sem_init(&subtotal->empty,1,BUFFER_SUB);
        sem_init(&subtotal->full,1,0);
        fp = fopen(file_name, "r");

        while(fscanf(fp,"%d,",&number)!=EOF)
        {
                sem_wait(&shared_memory->empty);
                sem_wait(&shared_memory->mutex);
                shared_memory->buff[j] = number;
                sem_post(&shared_memory->mutex);
                sem_post(&shared_memory->full);
                j++;
                num_values=j;
        }
        int p, z;
        int processes = k;
        int temp_sub;
        int count = 0;
        int x;
        int b;
        int tot=0;
    b = (num_values/processes);
    double f = (num_values/(double)processes);

    printf("%.11f\n", f);

    if((num_values % 2) == 0)
    {
        b = b;
        printf("%i\n", b);
    }

    else
    {   
        //for(z = 1; z <= processes - 1; z++)
        //{
            b = (int)ceil(f + 0.5f);
            //b = b - 1;
            printf("%i\n", b);
        //}

        b = b - 1;
        printf("%i\n", b);
    }


        for(i = 0; i < processes; i++)
        {
                int id;
                temp_sub=0;
                if((id=fork())==0)
                {
                                for(x=(count*b);x<((count*b)+b);x++)
                                {
                                        sem_wait(&shared_memory->full);
                                        sem_wait(&shared_memory->mutex);
                                        int num = shared_memory->buff[x];
                                        sem_post(&shared_memory->mutex);
                                        sem_post(&shared_memory->empty);
                                        temp_sub = temp_sub + num;
                                }

                                        int pid = getpid();
                                        sem_wait(&subtotal->empty);
                                        sem_wait(&subtotal->mutex);
                                        subtotal->subs[0] = pid;
                                        subtotal->subs[1] = temp_sub;
                                        sem_post(&subtotal->mutex);
                                        sem_post(&subtotal->full);

                        exit(0);
                }
                count++;
                if(id>0)
                {
                        int status;
                        wait(&status);
                        sem_wait(&subtotal->full);
                        sem_wait(&subtotal->mutex);
                        int pid = subtotal->subs[0];
                        int sub = subtotal->subs[1];
                        sem_post(&subtotal->mutex);
                        sem_post(&subtotal->empty);
                        printf("Sub total of process %d is %d\n",pid,sub);
                        tot = tot+sub;
                }

        }
printf("Total: %d\n",tot);
}

将带有整数的文件名解析为第一个参数,将子进程的数量解析为命令行中的第二个参数。

该文件包含1,1,2,3,4,4,5,6,10,11,14,17,25,16,5个整数。解析的子进程数为4时,应返回以下输出:

Sub-total produced by Processor with ID x1: 7
Sub-total produced by Processor with ID x2: 19
Sub-total produced by Processor with ID x3: 52
Sub-total produced by Processor with ID x4: 46
Total: 124

不计算最终过程值和总计。请帮忙。提前谢谢。

0 个答案:

没有答案