gpt4 book ai didi

c - C 中奇怪的 fork() 问题

转载 作者:太空宇宙 更新时间:2023-11-04 04:33:13 24 4
gpt4 key购买 nike

这是对基本 mapReduce 的赋值,它查找文本文件中字符串的出现次数。

我有一个只调用 spawnMapper() 的 main。

问题是 fork() == 0 但它不会进入该部分并打印“我成功了!”或其他任何东西。如果我将 int pid 设置为 fork 返回的内容,或者如果我直接使用 fork,就会出现这种情况。

请帮忙,它可以编译但无法正常工作,我很困惑。

#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "reducer.h"

//parent = greater than 1 child = 0

char fpeek(FILE *file);

void spawnMapper(char* keyword, char* fileLocation, int numberMappers){

FILE *fptr;

if((fptr=fopen(fileLocation, "r")) == NULL){
printf("ERROR! opening file");
exit(1);
}

fseek(fptr, 0L, SEEK_END);
int size = ftell(fptr);

int mapperSize = size/numberMappers;
fseek(fptr, 0L, SEEK_SET);

int i;
for(i = 0; i < numberMappers; i++){

fptr = fopen(fileLocation, "r");
int pToC[2]; //pipe parent to child
int cToP[2]; //pipe child to parent
if(pipe(pToC) == -1 || pipe(cToP) == -1){
printf("pipe failure");
exit(1);
}

if (fork() == 0) {
printf("I made it!");
fptr = fopen(fileLocation,"r");
int threadSize = 0;
int found = 0;
while(fpeek(fptr) != EOF && threadSize < mapperSize){
fseek(fptr, i*mapperSize, SEEK_SET);
char* stringCheck;
fscanf(fptr, "%s", stringCheck);
if(strcmp(keyword, stringCheck) == 0){
found += 1;
}
threadSize += strlen(stringCheck);
}
printf("found %d", found);
//pipes to parent how many found
}
else{

}
}
fclose(fptr);

}

char fpeek(FILE *stream){
char c;

c = fgetc(stream);
ungetc(c, stream);

return c;
}

最佳答案

Kaylum 在评论中指出 printf 是行缓冲的。这是一个普遍的误解。 stdin 是行缓冲的;其他功能(例如 putchar)也受到影响。尽管如此,他的解决方案是正确的:打印一个换行符以强制立即打印整行。

或者,fflush(stdout); 将立即打印文本, 不需要换行符。

在我看来,Mike 的建议似乎行不通,因为在您的程序运行之前,潜在的问题就与 termios 相关;终端处于熟化/规范模式(意味着 termios 将在发送所有数据之前等到换行符,以便它可以代表应用程序处理诸如退格之类的事情)。如果您想将终端设置为原始模式,这意味着一旦按下键就会将字符发送到您的程序,这与 C 无关,但您可能需要阅读 this pagethis page ...

关于c - C 中奇怪的 fork() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33771437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com