试图找出为什么子进程比应有的更早终止

发布于 2024-11-19 13:43:42 字数 5467 浏览 2 评论 0原文

我正在尝试编写一个程序,其中父进程将分叉 n 个子进程(子进程的节点数),并且子进程必须执行每个子进程都相同的特定任务。

在我的代码中放置几个​​ printf 之后,我意识到子进程中的 for 循环中存在一个错误,即 for 没有在正确的位置终止,例如,如果代码读取为 for(p =0;p<=10; p++) printf 显示该进程计数不超过 p=6!

我对此真的很陌生,尽管我搜索了网络,但没有找到任何有用的东西!有人可以帮我吗?

#include "ranlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


#define node_number 1 
#define seed1 123455L
#define seed2 54321L
#define floor_rates {{0,1,1},{1,0,1}}
// the floor_rates should be intialized at the beginning and they specify the amount
// of flow rate to a specific destination from a specific node, for example
// floor_rates[k][i] is the amount of data node i has for destination k.
#define numofflows 2

float **diagonalcreation(int matsize, float *matrix)//, float **diagonal)
{
int i;
int j;
int k;
float **diagonal;
diagonal=malloc(sizeof(float *)*matsize);
for(i=0;i<matsize;i++)
{
    diagonal[i]=malloc((sizeof(float))*matsize);
}
for(i=0; i<matsize;i++)
{
    for(j=0;j<matsize; j++)
    {
        if(i==j)
        {
            diagonal[i][j]=matrix[i];
        }
        else
        {
            diagonal[i][j]=0;
        }
    }
}

for( k=0;k<matsize; k++)
{
    printf("behnaz hastam\n");
    for(j=0; j<matsize; j++)
    {
        printf("%f\t",diagonal[k][j]);
    }
}
return diagonal;
}


int main()
{
    float c_mean[node_number][node_number];
    pid_t pid[node_number];
    int check;
    check=0;
     //   int index;
     // index=0;
    float c_var[node_number][node_number];
    struct nodes{ 
        float *a;
        float *b;
        float *r;
        int length_r;
        float *s;
        int length_s;
        float **A;
        float **B;
        //right now we have not implemented the DIV protocol but when we do there is a need   for a different variable named seq_number_rec which is the last sequence number recieved.
        //last_seq_number_sent;
        //ack
        float **lambdaprimey_x_x;
        float **lambdax_y_x;
        float lambdax_x_x[numofflows];
        float **t;
        float **ttemp;
        float **tprime;
        float **lambdacomputeprime;
        float lambdacompute[numofflows];
        int *neighbors;
        int length_neighbors;


    } node[node_number];

    int i;
    int j;
    //int numofflows;
    /* srand((unsigned)time(0));
    seed1=random();//12345L;
    seed2=random();//54321L;*/
    setall(seed1,seed2);
    //signal(SIGCHLD, SIG_IGN);
       for(i=0;i<=node_number-1;i++)
    {
        for(j=0; j<=i-1; j++)
        {

            c_mean[i][j]=genchi(1.0);
             if (c_mean[i][j]>1)
             {
             c_mean[i][j]=1.0/c_mean[i][j];
             }
             if(i==j)
             {
             c_mean[i][j]=0;
             }
        }

    }
     for(i=0;i<=node_number-1;i++)
     {
     for(j=i; j<=node_number-1; j++)
     {
     c_mean[i][j]=c_mean[j][i];

     }
     }
    //we are assuming that the links are bimodal with a certain probability to be up or  down.
    for(i=0;i<=node_number-1;i++)
    {
        for(j=0;j<=node_number-1; j++)
        {
            c_var[i][j]=c_mean[i][j]*(1-c_mean[i][j]);
        }
    }
    // pid[0]=fork();

    for(i=0;i<node_number;i++)
    {
        pid[i]=fork();
        if(pid[i]==0)
        {
            int *temp_n=node[i].neighbors;
            float *temp_a=node[i].a;
            float *temp_b=node[i].b;
            float *temp_r=node[i].r;
            float *temp_s=node[i].s;
            //pid[i]=fork();

            int p;
            //The first step is to do the initialization in each process.
            for(p=0; p<=10; p++)
            {
                if(i==0){
                    printf("%d %d %d\n\n\n",p,i, node_number);}
                node[i].length_neighbors=0;

                if(c_mean[i][p]!=0)
                {
                    *temp_n=p;
                    temp_n++;
                    node[i].length_neighbors++;
                    *temp_a=c_var[i][p];
                    temp_a++;
                    *temp_b=c_var[p][p];
                    temp_b++;
                    *temp_r=c_mean[i][p];
                    temp_r++;
                    *temp_s=c_mean[p][i];
                    temp_s++;
                    free(&temp_n);
                    free(&temp_a);
                    free(&temp_b);
                    free(&temp_s);
                    free(&temp_r);
                }

            }


              node[i].A=diagonalcreation(node[i].length_neighbors,node[i].a);//, float    **diagonal)

    /*        for( k=0;k<node[i].neighbors; k++)
            {
                printf("\n");
                for(j=0; j<node[i].neighbors; j++)
                {
                    printf("%f\t",node[i].A[k][j]);
                }
            }
            */
            free(node[i].A);
            printf("behnaaaz");
            exit(0);

        }
        else if(pid[i]<0)
        {
            printf("error_pid");
            break;
        }

    }
    for(i=0;i<node_number;i++)
    {
        if(pid[i]!=0)
        {
            check++;
        }
    }
    if(check==node_number)
    {
        for(i=0;i<node_number;i++)
        {
            wait(NULL);
            printf("waitover");
        }
    }


    return(0);
}

I am trying to write a program where a parent process would fork n children(node_number of children) and where the children have to carry out a specific task which is the same for every child.

After placing a couple of printf's in my code I realized that there is an error in the for loop in the child processes which is that the for does not terminate at the right place, for example if the code reads, for(p=0;p<=10; p++) the printf shows that the process does not count up to more than p=6!

I am really new at this and even though I searched the web I didn't find anything useful! Could someone help me please?

#include "ranlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


#define node_number 1 
#define seed1 123455L
#define seed2 54321L
#define floor_rates {{0,1,1},{1,0,1}}
// the floor_rates should be intialized at the beginning and they specify the amount
// of flow rate to a specific destination from a specific node, for example
// floor_rates[k][i] is the amount of data node i has for destination k.
#define numofflows 2

float **diagonalcreation(int matsize, float *matrix)//, float **diagonal)
{
int i;
int j;
int k;
float **diagonal;
diagonal=malloc(sizeof(float *)*matsize);
for(i=0;i<matsize;i++)
{
    diagonal[i]=malloc((sizeof(float))*matsize);
}
for(i=0; i<matsize;i++)
{
    for(j=0;j<matsize; j++)
    {
        if(i==j)
        {
            diagonal[i][j]=matrix[i];
        }
        else
        {
            diagonal[i][j]=0;
        }
    }
}

for( k=0;k<matsize; k++)
{
    printf("behnaz hastam\n");
    for(j=0; j<matsize; j++)
    {
        printf("%f\t",diagonal[k][j]);
    }
}
return diagonal;
}


int main()
{
    float c_mean[node_number][node_number];
    pid_t pid[node_number];
    int check;
    check=0;
     //   int index;
     // index=0;
    float c_var[node_number][node_number];
    struct nodes{ 
        float *a;
        float *b;
        float *r;
        int length_r;
        float *s;
        int length_s;
        float **A;
        float **B;
        //right now we have not implemented the DIV protocol but when we do there is a need   for a different variable named seq_number_rec which is the last sequence number recieved.
        //last_seq_number_sent;
        //ack
        float **lambdaprimey_x_x;
        float **lambdax_y_x;
        float lambdax_x_x[numofflows];
        float **t;
        float **ttemp;
        float **tprime;
        float **lambdacomputeprime;
        float lambdacompute[numofflows];
        int *neighbors;
        int length_neighbors;


    } node[node_number];

    int i;
    int j;
    //int numofflows;
    /* srand((unsigned)time(0));
    seed1=random();//12345L;
    seed2=random();//54321L;*/
    setall(seed1,seed2);
    //signal(SIGCHLD, SIG_IGN);
       for(i=0;i<=node_number-1;i++)
    {
        for(j=0; j<=i-1; j++)
        {

            c_mean[i][j]=genchi(1.0);
             if (c_mean[i][j]>1)
             {
             c_mean[i][j]=1.0/c_mean[i][j];
             }
             if(i==j)
             {
             c_mean[i][j]=0;
             }
        }

    }
     for(i=0;i<=node_number-1;i++)
     {
     for(j=i; j<=node_number-1; j++)
     {
     c_mean[i][j]=c_mean[j][i];

     }
     }
    //we are assuming that the links are bimodal with a certain probability to be up or  down.
    for(i=0;i<=node_number-1;i++)
    {
        for(j=0;j<=node_number-1; j++)
        {
            c_var[i][j]=c_mean[i][j]*(1-c_mean[i][j]);
        }
    }
    // pid[0]=fork();

    for(i=0;i<node_number;i++)
    {
        pid[i]=fork();
        if(pid[i]==0)
        {
            int *temp_n=node[i].neighbors;
            float *temp_a=node[i].a;
            float *temp_b=node[i].b;
            float *temp_r=node[i].r;
            float *temp_s=node[i].s;
            //pid[i]=fork();

            int p;
            //The first step is to do the initialization in each process.
            for(p=0; p<=10; p++)
            {
                if(i==0){
                    printf("%d %d %d\n\n\n",p,i, node_number);}
                node[i].length_neighbors=0;

                if(c_mean[i][p]!=0)
                {
                    *temp_n=p;
                    temp_n++;
                    node[i].length_neighbors++;
                    *temp_a=c_var[i][p];
                    temp_a++;
                    *temp_b=c_var[p][p];
                    temp_b++;
                    *temp_r=c_mean[i][p];
                    temp_r++;
                    *temp_s=c_mean[p][i];
                    temp_s++;
                    free(&temp_n);
                    free(&temp_a);
                    free(&temp_b);
                    free(&temp_s);
                    free(&temp_r);
                }

            }


              node[i].A=diagonalcreation(node[i].length_neighbors,node[i].a);//, float    **diagonal)

    /*        for( k=0;k<node[i].neighbors; k++)
            {
                printf("\n");
                for(j=0; j<node[i].neighbors; j++)
                {
                    printf("%f\t",node[i].A[k][j]);
                }
            }
            */
            free(node[i].A);
            printf("behnaaaz");
            exit(0);

        }
        else if(pid[i]<0)
        {
            printf("error_pid");
            break;
        }

    }
    for(i=0;i<node_number;i++)
    {
        if(pid[i]!=0)
        {
            check++;
        }
    }
    if(check==node_number)
    {
        for(i=0;i<node_number;i++)
        {
            wait(NULL);
            printf("waitover");
        }
    }


    return(0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

姜生凉生 2024-11-26 13:43:42

首先,对于错误检查,不要使用 printf() ...而是使用 fprintf,并输出到 stderr,因为这不会buffer,其中 stdoutprintf 会缓冲输出,因此将这些函数与 stdout 一起使用不一定会在此时打印到控制台由于缓冲而导致的调用。

其次,在 for 循环中,您将内存释放给看起来没有通过 malloc() 分配的指针。这将导致未定义的行为,因为您只能在调用 malloc() 返回的同一指针上调用 free() ...您无法释放-up 数组的“部分”...您只能在完成后立即释放整个分配的内存块。

第三,您向 free() 传递一个指向已在堆栈上分配的值的指针(即 temp_n 等)。因此,您实际上将一个指向堆栈上地址的指针传递给 free(),该指针指向堆上的地址,而不是指向从返回的堆上内存地址的指针值。 malloc()。不管怎样,你仍然无法释放数组的一部分,而这正是你想要做的。

First, for error-checking, do not use printf() ... instead use fprintf, and output to stderr, since that does not buffer, where-as stdout and printf do buffer the output, so using those functions with stdout will not necessarily print to the console at the point-of-call due to buffering.

Secondly, in your for loop you are freeing memory to pointers that it does not appear have been allocated via malloc(). That's going to cause undefined behavior since you can only call free() on the same pointer that was returned from a call to malloc() ... you can't free-up "part" of an array ... you can only free an entire block of allocated memory at once after you're finished with it.

Third, you are passing to free() a pointer to a value that has been allocated on the stack (i.e., temp_n, etc.). So you're actually passing a pointer to an address on the stack that is pointing to an address on the heap to free(), not a pointer value to a memory address on the heap which was returned from malloc(). Either way though, you still can't deallocate part of an array, which is what it appears you're trying to-do.

万水千山粽是情ミ 2024-11-26 13:43:42

请注意,使用 fork() 时,子进程不会相互共享数据,也不会与父进程共享数据!不能在父进程中建立一个数据结构,然后使用fork()让子进程填充数据,最后读取父进程中的数据。每个子进程都会获得各自的数据结构副本,该副本在子进程终止时消失。

要创建共享数据的子进程,必须使用 pthread_create()。

Note that when using fork(), the child processes do not share data with each other nor with the parent! You can't set up a data structure in the parent, then use fork() to let child processes fill the data, and finally read the data in the parent. The child processes each get their individual copy of the data structure which disappears when the child terminates.

To create child processes that share data, you must use pthread_create().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文