- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在上一门关于如何在大学里学习如何在 Linux 上编写多进程程序的类(class)。我仍然很新,正在尽我最大的努力学习,因此欢迎您发现任何错误的地方。
我有一个问题要求我迭代一个数组,主进程的一半,另一半在子进程上。我编写了这样做的代码,但问题是,我注意到如果我运行几次二进制文件,主(父)进程有时会出现段错误。
请查看代码,并告诉我它有什么问题,或者我是否遗漏了此类编程的一个关键方面。我的回答在评论之后开始//answer starts here.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#define ARRAY_SIZE 1000
int main()
{
int numbers[ARRAY_SIZE]; /* array to lookup */
int n; /* the number to find */
time_t t; /* needed to initialize random number generator (RNG) */
int i;
/* intializes RNG (srand():stdlib.h; time(): time.h) */
srand((unsigned)time(&t));
/* initialize array with random numbers (rand(): stdlib.h) */
for (i = 0; i < ARRAY_SIZE; i++)
numbers[i] = rand() % 10000;
/* initialize n */
n = rand() % 10000;
//answer starts here
int half = n / 2;
int count = 0;
int pid_status = 0;
pid_t pid_f = fork();
if (pid_f == -1)
{
return EXIT_FAILURE;
}
else
{
if (pid_f == 0) // child process iterates half end of the array
{
for (i = half; i < ARRAY_SIZE; ++i)
{
if (numbers[i] == n)
{
count++;
}
}
printf("Sons counter:%d\n", count);
exit(count);
} //else it's the father process
else
{
for (i = 0; i < half; ++i)
{
if (numbers[i] == n)
{
count++;
}
}
waitpid(pid_f, &pid_status, 0);
printf("Father counter:%d\n", count);
if (WIFEXITED(pid_status))
{
count += WEXITSTATUS(pid_status);
}
printf("Sum is=%d\n", count);
}
}
return 0;
}
最佳答案
段错误是由于n有时越界导致的:
n = rand() % 10000;
//answer starts here
int half = n / 2;
一半可以是5000个,但是numbers只有1000个元素。
也许你的意思是:
n = rand() % 1000;
关于c - fork 后 main 中的间歇性段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48915759/
我在Web应用程序中使用WebRTC进行音频播放。因为我是WebRTC的新手,所以我使用@https://webrtc.github.io/samples/src/content/peerconnec
上下文 我的 VBA 代码经常替换工作簿中的工作表。因此,我无法直接在工作表模块中使用代码,因为它最终会在此过程中被删除。 我使用用户定义的类来处理我的事件(强烈受到 Chip Pearson's w
我已经搜索过这个问题,如果这个问题已经得到解答,我深表歉意(我很高兴被重定向),但具体来说,我们的问题是间歇性的。 我们的客户提示当事件从我们的软件发送到他们的手机时,通知音频间歇性地没有“响起”。它
背景故事优先: 我们有一个正在运行的部署在尝试使用 JMeter 等工具对其进行负载测试时遇到间歇性 502。它是一个将 POST 数据记录到另一个容器上的 mysql 数据库的容器。它每秒处理大约
在向我托管的 https://网站发出简单的 GET 请求时,我不断收到间歇性 SecureChannelFailure 错误。没有错误进入服务器日志文件。每 100 次调用的频率小于 1 个错误,但
我正在通过 eval 运行一些 JavaScript(我知道,开枪吧),它基本上枚举了文档对象上的所有属性。我的问题是,虽然它在 firebug 中工作,但从脚本运行时,它在 Firefox 中抛出未
我发现了这个关于 iBeacon 的教程 (http://www.appcoda.com/ios7-programming-ibeacons-tutorial/),我觉得很有趣。我已经下载了他们的源代
我在我的开发箱上本地运行 WCF 服务,我的测试检查该服务一切正常。 通常,一切都很好,但有时(5% 的时间),我会收到错误 The requested service, 'net.tcp://csm
我正在使用 django 和 jQuery 构建一个网络应用程序,并且在其中一个页面上 $(document).com('click'... 事件间歇性地触发。我在结帐队列中有一个项目列表,以及删除每
Excepcion:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure ...
我正在尝试对 MySQL 5.5 数据库执行一系列检查/插入操作,但我经常遇到间歇性的 SIGSEGV 错误问题。在执行许多查询的过程中,SELECT 语句运行得很好。然而,在经过一些可变的时间或执行
我每天至少发生一次崩溃,我似乎真的无法理解。它似乎在随机时刻发生在我身上,我无法追踪堆栈来理解它发生的原因。如果有人能为我指出正确的方向,甚至向我展示一些关于如何正确追踪值的在线文档和教程,那将是完美
我尝试用一些更简单的函数重现它,但没有成功。所以下面的代码显示了我们的生产服务器抛出的 KeyError 的相关方法,很多。 class PokerGame: ... def serial
我们有一个托管在 Windows 服务中的 WCF 服务和一个访问该服务的非线程客户端。该服务正在执行对 SQL Server 2008 数据库的数据访问。间歇性地在客户端发生以下异常: System
我们有一个 SSL 问题,我 99% 认为这不是您通常使用的证书信任存储旋转木马。 我们有一个 Weblogic 服务器试图通过 LDAPS 与 Active Directory 建立 SSL 连接,
我有一个复杂的经典 ASP 系统,多年来运行良好,但最近开始出现奇怪的间歇性问题。 在某些表单上,人们会报告说他们点击了“提交”,但表单只是自行重置(或者,浏览器可能只是重新加载了表单——我的用户可能
我在编译顶点着色器时遇到间歇性错误,为新创建的 OpenGL 上下文的首次渲染做准备。它是通常在相同硬件上运行的相同顶点着色器。失败后,glGetShaderInfoLog 返回的信息日志通常显示如下
我有一个目前看来无法解决的 EXC_BAD_ACCESS 问题。我试过启用 NSZombie,这似乎是许多帖子中的建议,但我处理的是 c 指针而不是 obj c 对象,所以我没有获得任何有用的调试信息
在 iOS 上出现间歇性 SSL 错误。我已经关闭了 ATS,我们知道这也会发生在操作系统版本 < iOS9 上 Error Domain=NSURLErrorDomain Code=1011 "An
我有一个使用 RequireJS 的相当大的 Backbone.js 项目。随着项目规模的增长(这里的“规模”指的是独立模块文件的数量),间歇性错误开始出现。大多数情况下,这是一个对象错误: 未捕获的
我是一名优秀的程序员,十分优秀!