gpt4 book ai didi

c - scanf from child 扫描任何 parent 已经扫描过的东西

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

我的程序及其两个子进程都以有序的方式从标准输入读取输入。我发现的问题是:

鉴于此输入:

32
51453a
140

父进程读到32,也就是说第一个子进程需要再读2个数。父进程然后通过管道发送 1 个字节,向第一个进程发出信号,它需要读取两个数字。当子进程接收到信号并开始读取这些数字时,子进程读取的不是 51453a 和 140,而是 32 51453a。

我一直在努力理解为什么会发生这种情况,如何解决或更好,如何避免这种情况。

我在 Mac OSX 上运行

编辑:此问题仅在从文件重定向输入(运行 ./polygon < in.txt)时发生。当直接从控制台运行时,它不会发生。还添加了更多代码以提供更好的画面。

void runWriterProcess(char *outFile, int writerFd[2]) {
close(writerFd[1]);
close(STDIN_FILENO);
dup(writerFd[0]);
close(writerFd[0]);
char *const params[] = {"./writer", outFile, NULL};
execv("./writer", params);
}
void reader32(int INSIG, int OUTPUT) {
long unsigned polygonParts[2];
char runSignal[2], polygonBuffer[17];
int64 nextPolygon;
while(read(INSIG, runSignal, 1) > 0) {
scanf("%lx", &polygonParts[0]);
scanf("%lx", &polygonParts[1]);
nextPolygon = polygonParts[1];
nextPolygon = nextPolygon << 32;
nextPolygon += polygonParts[0];
sprintf(polygonBuffer, "%16llx", nextPolygon);
write(OUTPUT, polygonBuffer, (int)strlen(polygonBuffer));
}
finishError("reader32\0");
exit(EXIT_SUCCESS);
}

void reader64(int INSIG, int OUTPUT) {
char runSignal[2], polygonBuffer[17];
int64 nextPolygon;
while(read(INSIG, runSignal, 1) > 0) {
scanf("%16llx", &nextPolygon);
sprintf(polygonBuffer, "%16llx", nextPolygon);
write(OUTPUT, polygonBuffer, 16);
}
finishError("reader64\0");
exit(EXIT_SUCCESS);
}

void runMainLoop(int reader32, int reader64, int readPipe) {
long unsigned dummy[2];
int64 bigDummy, nextPolygon;
char polygonBuffer[17];
int readerToRun;
for(;;) {
scanf("%d", &readerToRun);
if (readerToRun == 32) {
write(reader32, "1", 1);
} else {
write(reader64, "1", 1);
}
read(readPipe, polygonBuffer, 16);
sscanf(polygonBuffer, "%16llx", &nextPolygon);
if(runOnPolygon(nextPolygon)) break;
}
write(reader64, "0\n", 2);
}

void createReaders(pid_t *reader32pid, pid_t *reader64pid) {
int fd32[2], fd64[2], fdBoth[2], readerToRun;
long unsigned polygonParts[2];
int64 nextPolygon, dummy;
char runSignal[2], polygonBuffer[17];
polygonBuffer[16] = '\0';
pipe(fd32);
pipe(fd64);
pipe(fdBoth);
if ((*reader32pid = fork()) == 0) {
close(fd32[1]);
close(fd64[0]);
close(fd64[1]);
close(fdBoth[0]);
reader32(fd32[0], fdBoth[1]);
}
if ((*reader64pid = fork()) == 0) {
close(fd32[0]);
close(fd32[1]);
close(fd64[1]);
close(fdBoth[0]);
reader64(fd64[0], fdBoth[1]);
}
close(fd32[0]);
close(fd64[0]);
close(fdBoth[1]);
runMainLoop(fd32[1], fd64[1], fdBoth[0]);
close(fd32[1]);
close(fd64[1]);
wait(NULL);
wait(NULL);
finishError("main_process\0");
}

void finishError(char processName[13]) {
char output[50];
sprintf(output, "%s pid=%d is going to exit\n", processName, getpid());
write(STDERR_FILENO, output, strlen(output) + 1);
}

int main(int argc, const char *argv[])
{
polygonList.head = NULL;
polygonList.tail = NULL;
pid_t writerPid, reader32pid, reader64pid;
int writerFd[2];
char outFile[11];
scanf("%s", outFile);
pipe(writerFd);
if (fork() == 0) {
runWriterProcess(outFile, writerFd);
} else {
close(STDOUT_FILENO);
dup(writerFd[1]);
close(writerFd[0]);
close(writerFd[1]);
createReaders(&reader32pid, &reader64pid);
}
freeList();
return 0;
}

最佳答案

这叫做缓冲。当您fork 时,子进程几乎是父进程的完整副本,包括输入缓冲区等内容。

最好的解决方案 (IMO) 是从 child 的标准输入中读取。而是让父进程完成所有输入,然后通过管道将其发送给子进程。

关于c - scanf from child 扫描任何 parent 已经扫描过的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47927400/

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