- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个程序,其中父进程将 fork 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);
}
最佳答案
首先,对于错误检查,不要使用 printf()
...而是使用 fprintf
,并输出到 stderr
,因为不缓冲,而 stdout
和 printf
确实缓冲输出,因此将这些函数与 stdout
一起使用不一定会打印到控制台由于缓冲而在调用点发生。
其次,在 for
循环中,您将内存释放给指针,但该内存似乎并未通过 malloc()
分配。这将导致未定义的行为,因为您只能在调用 malloc()
返回的同一指针上调用 free()
...您无法释放-up 数组的“部分”...您只能在完成后立即释放整个分配的内存块。
第三,您将向 free()
传递一个指向已在堆栈上分配的值的指针(即 temp_n
等)。因此,您实际上将一个指向堆栈上地址的指针传递给 free()
,该指针指向堆上的地址,而不是指向从返回的堆上内存地址的指针值。 malloc()
。但无论哪种方式,您仍然无法释放数组的一部分,而这正是您正在尝试做的事情。
关于c - 试图找出为什么子进程比应有的时间更早终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6667082/
我是 Linux 的新手,并且继承了保持我们的单一 Linux 服务器运行的职责。这是我们的SVN服务器,所以比较重要。 原来在我之前维护它的人有一个 cron 任务,当有太多 svnserve 进程
Node 虽然自身存在多个线程,但是运行在 v8 上的 JavaScript 是单线程的。Node 的 child_process 模块用于创建子进程,我们可以通过子进程充分利用 CPU。范例:
Jenkins 有这么多进程处于事件状态是否正常? 我检查了我的设置,我只配置了 2 个“执行者”... htop http://d.pr/i/RZzG+ 最佳答案 您不仅要限制 Master 中的执
我正在尝试在 scala 中运行这样的 bash 命令: cat "example file.txt" | grep abc Scala 有一个特殊的流程管道语法,所以这是我的第一个方法: val f
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我需要一些帮助来理解并发编程的基础知识。事实上,我读得越多,就越感到困惑。因此,我理解进程是顺序执行的程序的一个实例,并且它可以由一个或多个线程组成。在单核CPU中,一次只能执行一个线程,而在多核CP
我的问题是在上一次集成测试后服务器进程没有关闭。 在integration.rs中,我有: lazy_static! { static ref SERVER: Arc> = {
我正在使用 Scala scala.sys.process图书馆。 我知道我可以用 ! 捕获退出代码和输出 !!但是如果我想同时捕获两者呢? 我看过这个答案 https://stackoverflow
我正在开发一个C++类(MyClass.cpp),将其编译为动态共享库(MyClass.so)。 同一台Linux计算机上运行的两个不同应用程序将使用此共享库。 它们是两个不同的应用程序。它不是多线程
我在我的 C 程序中使用 recvfrom() 从多个客户端接收 UDP 数据包,这些客户端可以使用自定义用户名登录。一旦他们登录,我希望他们的用户名与唯一的客户端进程配对,这样服务器就可以通过数据包
如何更改程序,以便函数 function_delayed_1 和 function_delayed_2 仅同时执行一次: int main(int argc, char *argv[]) {
考虑这两个程序: //in #define MAX 50 int main(int argc, char* argv[]) { int *count; int fd=shm
请告诉我如何一次打开三个终端,这样我的项目就可以轻松执行,而不必打开三个终端三次然后运行三个exe文件。请问我们如何通过脚本来做到这一点,即打开三个终端并执行三个 exe 文件。 最佳答案 在后台运行
我编写了一个监控服务来跟踪一组进程,并在服务行为异常、内存使用率高、超出 CPU 运行时间等时发出通知。 这在我的本地计算机上运行良好,但我需要它指向远程机器并获取这些机器上的进程信息。 我的方法,在
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 8年前关闭。 Improve this qu
我有一个允许用户上传文件的应用程序。上传完成后,必须在服务器上完成许多处理步骤(解压、存储、验证等...),因此稍后会在一切完成后通过电子邮件通知用户。 我见过很多示例,其中 System.Compo
这个问题对很多人来说可能听起来很愚蠢,但我想对这个话题有一个清晰的理解。例如:当我们在 linux(ubuntu, x86) 上构建一个 C 程序时,它会在成功编译和链接过程后生成 a.out。 a.
ps -eaf | grep java 命令在这里不是识别进程是否是 java 进程的解决方案,因为执行此命令后我的许多 java 进程未在输出中列出。 最佳答案 简答(希望有人写一个更全面的): 获
我有几个与内核态和用户态的 Windows 进程相关的问题。 如果我有一个 hello world 应用程序和一个暴露新系统调用 foo() 的 hello world 驱动程序,我很好奇在内核模式下
我找不到很多关于 Windows 中不受信任的完整性级别的信息,对此有一些疑问: 是否有不受信任的完整性级别进程可以创建命名对象的地方? (互斥锁、事件等) 不受信任的完整性级别进程是否应该能够打开一
我是一名优秀的程序员,十分优秀!