简体   繁体   English

我的C程序默认是多线程还是stdout对我的打印语句重新排序?

[英]My c program is multi-threading by default or stdout reordering my print statements?

I am running into a weird issue. 我遇到一个奇怪的问题。 I wrote a simple c program (two source files, relevant sections shown below). 我编写了一个简单的c程序(两个源文件,相关部分如下所示)。 When I run the program (ac has the main function), I get the output (since the number of times I am calling "process" is high, I am printing the last few print outs from the output to the screen): 当我运行程序(ac具有主要功能)时,我得到了输出(由于我所谓的“进程”的次数很高,因此我将从输出到屏幕上打印出最后几个打印输出):

key: 'Q' 键:“ Q”
key: 'A' 键:“ A”
1 1
key: 'A' 键:“ A”
key: 'A' 键:“ A”
1 1
key: 'A' 键:“ A”
key: 'A' 键:“ A”
1 1
key: 'A' 键:“ A”
key: 'A' 键:“ A”
key: 'C' 键:“ C”
Segmentation fault (core dumped) 分段故障(核心已转储)

ac AC

void process(int* hash, char* input, int number_of_ac) { 无效过程(int *哈希,char *输入,int number_of_ac){
int value, i = 0; 整数值,i = 0;
for(; i < number_of_ac; i++) { for(; i <number_of_ac; i ++){
value = get_value(hash, input[i]); 值= get_value(哈希,输入[i]);
printf("1\\n"); 的printf( “1个\\ n”);
increment_value(hash, input[i]); 增量值(哈希,输入[i]);
} }
} }

bc 公元前

int get_value(int* hash, char key) { int get_value(int * hash,char key){
printf("key: '%c'\\n", key); printf(“ key:'%c'\\ n”,key);
return hash[get_index(key)]; 返回hash [get_index(key)];
} }

void increment_value(int* hash, char key) { 无效的增量值(int *哈希,字符键){
int value = get_value(hash, key); int value = get_value(hash,key);
hash[get_index(key)] = ++value; hash [get_index(key)] = ++ value;
} }

My question specifically is (the ultimate goal is for me to try to debug why I am getting a segmentation fault on a high number of "process" calls and not on a smaller number of calls), shouldn't I see a print out like this: 我的问题特别是(最终目的是要我调试为什么在大量“流程”调用而不是较少数量的调用上出现分段错误),我是否应该看到类似的打印输出这个:

key: 'Q' 键:“ Q”
1 1
key: 'A' 键:“ A”
1 1
key: 'A' 键:“ A”
1 1
key: 'A' 键:“ A”
etc. 等等

Why do I see a print of two letters before I see the 1 again? 为什么在再次看到1之前看到两个字母的打印? Is this a buffer reordering issue of my statements being printed to the screen? 这是我的报表打印到屏幕上的缓冲区重新排序问题吗? Or, is it somehow my c program is multi-threading behind the scene? 还是我的C程序在后台多线程化?

By the way, this exact same program runs on my laptop with a MUCH LARGER set of input: 顺便说一句,这个完全相同的程序在我的笔记本电脑上运行,输入量大得多:

Linux ubuntu 3.8.0-25-generic #37-Ubuntu SMP Thu Jun 6 20:47:07 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux Linux ubuntu 3.8.0-25-generic#37-Ubuntu SMP Thu Jun 6 20:47:07 UTC 2013 x86_64 x86_64 x86_64 GNU / Linux

But it breaks with the Segmentation Fault above on a much smaller input on another machine: 但是,它在另一台机器上的小得多的输入上与上面的“分段错误”一起中断了:

Linux xyz 2.6.32-279.19.1.el6.cny6.7068.x86_64 #1 SMP Mon Mar 11 08:59:43 CDT 2013 x86_64 x86_64 x86_64 GNU/Linux Linux xyz 2.6.32-279.19.1.el6.cny6.7068.x86_64#1 SMP Mon Mar 11 08:59:43 CDT 2013 x86_64 x86_64 x86_64 GNU / Linux

Any ideas? 有任何想法吗? I appreciate your assistance! 感谢您的协助!

Look how you call increment value: 看一下如何调用增量值:

void increment_value(int* hash, char key) {
   int value = get_value(hash, key); // calling get_value
   hash[get_index(key)] = ++value;
}

so you are printing a char here and you call get_value again in process function. 因此,您要在此处打印一个字符,然后在流程函数中再次调用get_value。 That why you are getting multiples prints of chars before '1' 这就是为什么要在'1'之前打印多个字符的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM