如何获得在子进程中存储在变量中的变量外部子变量的值?

时间:2014-05-31 11:50:40

标签: c linux

我将数据存储在子进程中的变量中,当我在子进程外获取值时,它返回垃圾值。但是当我在子进程中获得变量值(我存储在子进程中的变量中)时,它给出了正确的值。 我的代码如下。提前谢谢。

#include <stdio.h>
#include <unistd.h>

void getValues();
void printValues();

int main()
{
int cpid, i = 0, j = 0, mat1[4], mat2[4], mat3[4], mat4[4], mat[4], sum1[4], sum2[4];
//get values in all matrixes.
for(i=0; i < 4; i++)
    {
        printf("\nEnter value %d in matrix 1: ", i+1);
        scanf("%d", &mat1[i]);
    }

    for(i=0; i < 4; i++)
    {
        printf("\nEnter value %d in matrix 2: ", i+1);
        scanf("%d", &mat2[i]);
    }

for(i=0; i < 4; i++)
    {
        printf("\nEnter value %d in matrix 3: ", i+1);
        scanf("%d", &mat3[i]);
    }

    for(i=0; i < 4; i++)
    {
        printf("\nEnter value %d in matrix 4: ", i+1);
        scanf("%d", &mat4[i]);
    }
 //print values of all matrixes.
printf("\nMatrix 1:\n");
    for(i=0; i < 4; i++)
    {
        printf("%d\t", mat1[i]);
        if(i == 1)
        {
            printf("\n");
        }
    }

    printf("\n\nMatrix 2:\n");
    for(i=0; i < 4; i++)
    {
        printf("%d\t", mat2[i]);
        if(i == 1)
        {
            printf("\n");
        }
    }

    printf("\n\nMatrix 3:\n");
    for(i=0; i < 4; i++)
    {
        printf("%d\t", mat3[i]);
        if(i == 1)
        {
            printf("\n");
        }
    }

    printf("\n\nMatrix 4:\n");
    for(i=0; i < 4; i++)
    {
        printf("%d\t", mat4[i]);
        if(i == 1)
        {
            printf("\n");
        }
    }

cpid = fork();

if(cpid < 0)
{
    printf("\n\nFaild!\n\n");
}
if(cpid == 0)
{
    for(i=0; i < 4; i++)
    {
        sum1[i] = mat1[i] + mat2[i];
    }
}
else
{
    for(i = 0; i <=10000; i++)
    {for(j = 0; j<=10000; j++){}}

for(i=0; i < 4; i++)
    {
        sum2[i] = mat3[i] + mat4[i];
    }

for(i=0; i < 4; i++)
    {
        mat[i] = sum1[i] + sum2[i];
    }

printf("\nSum of All Matrixes:\n");
    for(i=0; i < 4; i++)
    {
        printf("%d\t", mat[i]);
        if(i == 1)
        {
            printf("\n");
        }
    }

}
}

1 个答案:

答案 0 :(得分:-1)

您似乎对流程的工作方式感到困惑。当父母分叉孩子时,孩子有父母地址空间(堆和堆栈)的副本。但是,在那之后,它们作为单独的实体存在;对孩子的地址空间的任何更改都不会反映在父母的地址空间中,反之亦然。在进程之间共享资源的唯一方法是通过OS中的进程间通信API(由于持续系统中断导致的令人讨厌的性能损失)或共享内存段(通过mmap或shmem)。因此,您很可能想要使用线程而不是进程;可以在此处找到学习线程的绝佳资源:https://computing.llnl.gov/tutorials/pthreads/

关于如何完成指定查询的一个粗略示例如下所示(不确定您尝试使用实际代码实现了什么,但仍然如此。):

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

typedef struct {
    int size;
    int matrix[];
} MatrixStruct;

void *thread_matrix_calc(void *arg) {
    //Cast arg to the struct it points at
    MatrixStruct *matrix = (MatrixStruct *)arg;

    //Pull variables from that struct
    int matrix_size = matrix->size;
    int (*matrix_arry)[matrix_size] = matrix->matrix;

    /*
     * Code for child matrix manipulation goes here...
     */

     return NULL;
}

int main(void) {
    //Initialize all matrices and summations 
    MatrixStruct *child_matrix = malloc(sizeof(int) + (MATRIX_SIZE * sizeof(int));

    //Initialize and start child thread
    pthread_t child_thread;
    int child_thread_init = pthread_create(&child_thread, NULL, &thread_matrix_calc, (void *)child_matrix);

    //Ensure initialization was successful
    if(child_thread_init < 0) exit(EXIT_FAILURE);

    /*
     * Code for parent matrix manipulation goes here...
     */

    //Join child thread to ensure child has finished manipulating it's matrix.
    pthread_join(child_thread, NULL);

    /*
     * At this point both the parent and child manipulated matrices can be accessed 
     * in the remaining thread.
     */

    return 0;
}
  

注意:代码没有编译,仅作为POSIX线程的语义演示提供。