C 试图找出为什么子进程';我们会比他们应该的更早被终止
我试图编写一个程序,其中父进程将派生n个子进程(节点\子进程的数量),子进程必须执行一个特定的任务,这对每个子进程都是一样的C 试图找出为什么子进程';我们会比他们应该的更早被终止,c,process,wait,fork,C,Process,Wait,Fork,我试图编写一个程序,其中父进程将派生n个子进程(节点\子进程的数量),子进程必须执行一个特定的任务,这对每个子进程都是一样的 在我的代码中放置了几个printf之后,我意识到子进程中的for循环中有一个错误,即for没有在正确的位置终止,例如,如果代码读取,for(p=0;p首先,对于错误检查,不要使用printf()…改用fprintf,并输出到stderr,因为它不缓冲,其中asstdout和printf会缓冲输出,因此使用带有stdout的那些函数不一定会由于缓冲而在调用点打印到控制台 其
在我的代码中放置了几个printf之后,我意识到子进程中的for循环中有一个错误,即for没有在正确的位置终止,例如,如果代码读取,
for(p=0;p首先,对于错误检查,不要使用printf()
…改用fprintf
,并输出到stderr
,因为它不缓冲,其中asstdout
和printf
会缓冲输出,因此使用带有stdout
的那些函数不一定会由于缓冲而在调用点打印到控制台
其次,在for
循环中,您正在将内存释放给通过malloc()
分配的指针。这将导致未定义的行为,因为您只能对调用malloc()
返回的同一指针调用free()
。您无法释放“部分”一个数组的…您只能在完成后立即释放整个已分配内存块
第三,您正在向free()
传递一个指向已在堆栈上分配的值的指针(即temp\n
,等等)。因此,实际上您正在向堆栈上的地址传递一个指针,该地址指向堆上的free()地址
,而不是指向堆上的内存地址的指针值,该地址是从malloc()
返回的。尽管如此,您仍然无法释放数组的一部分,这似乎是您正在尝试执行的操作。请注意,在使用fork()时,子进程之间不共享数据,也不与父进程共享数据!您不能在父进程中设置数据结构,然后使用fork()让子进程填充数据,最后读取父进程中的数据。子进程各自获取数据结构的单独副本,该副本在子进程终止时消失
要创建共享数据的子进程,必须使用pthread_create()。给定#定义节点编号1
,和for(i=0;i
,这让人惊讶的是,您得到了6。另外,为每个子进程调用一个函数;不要将太多的活动塞进main()
program。如果不将过程控制和计算混为一谈,就很难获得正确的过程控制。
#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);
}