简体   繁体   English

在 fork 中调用一个函数,该函数内是一个 while 循环,它应该读取输入,但是,完全跳过了 scanf()

[英]Calling a function within a fork, within the function is a while loop which should be reading input, however, scanf() is completely skipped

Here is where main() forks and calls the function:这里是main()分支并调用函数的地方:

#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>


#include"sp-pipe-client.h"

pid_t clientChild, serverChild, c1 child1Stat;

int main() {

    if ((serverChild = fork()) == 0) {

    }
    else if ((clientChild = fork()) == 0) {
        SP_Pipe_Client();
    }
    else {
        c1 = wait(&child1Stat);
    } 
}

header:标题:


void SP_Pipe_Client();

And here is the function in which scanf is being completely skipped, while loop goes infinite printing the menu:这是完全跳过scanf的函数,而循环无限打印菜单:



#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>

char input = 'a', fileName[50];
int puzzleSize = 4;
int tile;

void SP_Pipe_Client() {
    while (input != 'q') {
        //  if (IsGameWon(puzzleSize, slidingPuzzle)) {
        //      printf("Congratulations, you have won the game!\n");
        //  }

        printf("Menu: [p]rint, [m]ove, [n]ew, [s]ave, [l]oad, [q]uit?\n");
        scanf(" %c", &input);
        if (input == 'p') {
            //DisplayBoard(puzzleSize, slidingPuzzle);
        }
        else if (input == 'm') {
            printf("Enter tile # to move: ");
            scanf(" %d", &tile);
            //MovePiece(tile, puzzleSize, slidingPuzzle, rowP, colP);
        }
        else if (input == 'n') {
            printf("Enter a size for new game (2-10): ");
            scanf(" %d", &puzzleSize);
            while (puzzleSize < 2 || puzzleSize > 10) {
                printf("Enter a valid size for new game (2-10): ");
                scanf(" %d", &puzzleSize);
            }
            //free(slidingPuzzle);
            //slidingPuzzle = Initialize(puzzleSize);

        }
        else if(input == 's'){
            printf("Please enter file name for saving. (max 30 characters including '.txt'\n");
            scanf("%s", fileName);
            //SaveGame(puzzleSize, slidingPuzzle, fileName);
        }
        else if(input == 'l'){
            printf("Please enter file name for saving. (max 30 characters including '.txt'\n");
            scanf("%s", fileName);
            //slidingPuzzle = LoadGame(size, fileName, slidingPuzzle);
        }
        else if(input == 'q') {
            //TearDown(slidingPuzzle);
            exit(0);
        }
        else {
            printf("Invalid input, try again\n");
        }
    }
}

The main function is supposed to call this function and a server function that I have yet to code, these functions are within their own .c files. main 函数应该调用这个函数和一个我还没有编写代码的服务器函数,这些函数在它们自己的 .c 文件中。 When forked the child calls the function and infinitely prints the menu and the else statement at the bottom.当 fork 时,子进程调用该函数并在底部无限地打印菜单和 else 语句。

I have already tried changing which side of "%c" eats the \\n , as well as giving it to both sides and using \\n itself, I have also tried fgetc() both to eat the new line and to get input itself but no matter what the loop remains infinite, scanf() and fgetc() never occur.我已经尝试改变"%c"哪一边吃\\n ,以及把它给两边并使用 \\n 本身,我也试过fgetc()既吃新行又得到输入本身但是无论循环如何保持无限, scanf()fgetc()不会发生。

Edit: Also I have tried moving my variable declarations, they used to be inside the function called by the child, made no difference as well.编辑:我也试过移动我的变量声明,它们曾经在孩子调用的函数内,也没有区别。

I have figured it out.我已经想通了。 The problem is that I only called wait() one time, but I made two child processes, so my main was waiting for only one child to die, which because I haven't written code for my server child it was ending and so my main process was ending, leaving the client child to continue on it's own as an orphan and not take any input.问题是我只调用了 wait() 一次,但是我创建了两个子进程,所以我的主进程只等待一个子进程死亡,这是因为我还没有为我的服务器子进程编写代码,所以我的主进程即将结束,让客户孩子作为孤儿继续自己,不接受任何输入。 I fixed this by adding "c2 = wait(&child2Stat);"我通过添加“c2 = wait(&child2Stat);”来解决这个问题to the else statement allowing my main process to wait for both the client and server children:到 else 语句,允许我的主进程等待客户端和服务器子进程:

int main(){

pipe(pipe_command);
pipe(pipe_param_result);

if((serverChild = fork()) == 0){

}
else if((clientChild = fork()) == 0){
SP_Pipe_Client();

}
else{
    c1 = wait(&child1Stat);
    c2 = wait(&child2Stat);
}
 
}

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

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