qua*_*ela 0 c printf multiprocessing while-loop
我有一个与while循环相关的奇怪问题.我有一个函数,它在process(print_file())的父元素的末尾被调用,并且它不接受真正的条件继续.这是我简单的多进程代码,如下所示.
#include <stdio.h> /* basic I/O routines. */
#include <stdlib.h>
#include <unistd.h> /* define fork(), etc. */
#include <sys/types.h> /* define pid_t, etc. */
#include <sys/wait.h> /* define wait(), etc. */
#include <signal.h> /* define signal(), etc. */
#include <pthread.h>
#include <time.h>
#include <ctype.h>
void print_file(char* [], char* []);
void child_process(int,int);
void parent_process();
int counter=0;
int main(int argc, char* argv[]) {
counter = atoi(argv[1]);
int i,k;
pid_t child_pid;
int child_status;
char* array[counter];
srand ( time(NULL) );
int temp;
for(i=0; i<counter; i++){
temp = rand()%4;
child_pid = fork();
switch(child_pid) {
case -1:
printf("Error occured with fork()\n");
exit(1);
case 0:
child_process(i,temp); /* Child Process */
exit(0);
}
}
wait(&child_status);
parent_process();
execl("/usr/bin/killall","killall","tail",(char *) 0);
return 0;
}
void child_process(int i,int temp){
FILE* fptr;
fptr = fopen("sample.txt","a+");
if( temp==0 ) {
fprintf(fptr,"A %d\n",i);
}
else if( temp==1 ) {
fprintf(fptr,"C %d\n",i);
}
else if( temp==2 ) {
fprintf(fptr,"G %d\n",i);
}
else if( temp==3 ) {
fprintf(fptr,"T %d\n",i);
}
fflush(fptr);
sleep(1);
fclose(fptr);
}
void parent_process(void){
FILE* fptr;
fptr = fopen("sample.txt","r");
char* str = (char*)malloc(1);
int temp,i,k;
char* array_opst[counter];
char* array[counter];
i=0;
while(!feof(fptr)){
fscanf(fptr,"%s%d",str,&temp);
if(feof(fptr))
break;
if(strcmp(str,"A")==0){
array[temp]="A";
array_opst[temp]="T";
printf("Array[%d] = %s\n",temp,array[temp]);
}
else if(strcmp(str,"C")==0){
array[temp]="C";
array_opst[temp]="G";
printf("Array[%d] = %s\n",temp,array[temp]);
}
else if(strcmp(str,"G")==0){
array[temp]="G";
array_opst[temp]="C";
printf("Array[%d] = %s\n",temp,array[temp]);
}
else if(strcmp(str,"T")==0){
array[temp]="T";
array_opst[temp]="A";
printf("Array[%d] = %s\n",temp,array[temp]);
}
i++;
}
fclose(fptr);
free(str);
print_file(array,array_opst);
}
void print_file(char* array[counter], char* array_opst[counter]) {
int j=0,i=1;
while(j<counter){
printf("%d", i);
i++;
j++;
if(i==10){
i=0;
}
}
return;
}
Run Code Online (Sandbox Code Playgroud)
在print_file函数中,即使条件满足,它也永远不会进入while循环.但每当我在函数的第一行放置printf print_file以检查它是否成功输入它打印.(小心这counter是一个全局变量).是什么导致这个问题?
循环的输出由于stdout被缓冲而没有出现(printf()循环内的语句包含no \n而stdout不是显式fflush()d)并且调用execl()in main(),它替换当前进程映像和stdout缓冲区不会像往常一样在正常的程序出口上刷新.在循环后添加换行符printf()或显式调用fflush(stdout);.
存在缓冲区溢出,导致未定义的行为:
char* str = (char*)malloc(1);
Run Code Online (Sandbox Code Playgroud)
分配1个字节,str然后使用:
fscanf(fptr,"%s%d",str,&temp);
Run Code Online (Sandbox Code Playgroud)
fscanf()添加一个空终止符,str即使只有一个char被读取,也会超出结尾.我无法看到动态分配它的原因,甚至当它需要是一个数组时,因为行的格式似乎是单个char后跟一个int:
fprintf(fptr,"A %d\n",i);
Run Code Online (Sandbox Code Playgroud)
要解决此问题,只需使用a char和格式说明符%c:
char ch;
if (2 == fscanf(fptr, "%c%d", &ch, &temp))
{
}
Run Code Online (Sandbox Code Playgroud)
经常检查返回值(fopen(),fprintf(),fscanf()等).