这是一段代码:
int somefunc() {
/* ... */
while ((pos = KMP_index(array, size, pattern, plen)) > -1) {
count++;
}
return count;
}
somefunc()
在多个子进程中调用,每个 fork 调用一次。
我的代码在 Linux x86_64 和 i386 上按预期编译和工作。但是当我在 Atom 上网本(Arch Linux i686)上运行它时,count
变量永远不会超过 2!
while (...) {
count++; //succesfully increments
}
return count; //it's maximum 2!
但是,如果我添加 printf() :
while (...) {
count++; //succesfully increments
printf("%d", anything);
}
return count; //value as expected
打印空字符或fflush
输入标准输出在这里不起作用。我必须打印至少一个字符,然后变量才可以。它让我发疯。
有人可以告诉我,为什么我还要使用这样的“解决方法”?这可能是我的linux环境的问题吗?(没什么特别的,GCC 4.8,股票内核)谢谢。
PS 整个来源在这里http://pastebin.com/4eEHMbKn。是的,这是一个家庭作业 :) 我需要创建一个类似 grep 的实用程序,在单独的进程中处理每个文件。