简体   繁体   English

如何在单独的进程中从终端获取输入?

[英]How do I get input from a terminal in a separate process?

I tried using fork() in order to get input from the terminal at the same time as my program was exchanging messages with server, but the result was a program that was reading input even before I pressed enter.我尝试使用 fork() 以便在我的程序与服务器交换消息的同时从终端获取输入,但结果是一个程序甚至在我按下 Enter 之前就在读取输入。

So if I use this:所以如果我使用这个:

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <mqueue.h>
#include <sys/msg.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>


int main(int argc, char ** argv){
    int pid = fork();

    if (pid == 0){
        for(int i =0; i<10; i++){
            char command[255];
            fgets (command, 255, stdin);
            printf("%s\n", command);
            sleep(1);
        }
        exit(0);
    } else {

    }
}

When I run the program this happens without me doing anything:当我运行程序时,我没有做任何事情就会发生这种情况:

�   
�   
�   
�   
�   
�   
�   
�   
�   
�   

Exactly the same thing happens if I use scanf("%s\n", command);如果我使用scanf("%s\n", command);会发生完全相同的事情。 or fgets (command, 255, stdin);fgets (command, 255, stdin); and all other variations I could find...以及我能找到的所有其他变体......

If I change the code to如果我将代码更改为

int main(int argc, char ** argv){
    int pid = fork();

    if (pid != 0){
        for(int i =0; i<10; i++){
            char command[255];
            fgets (command, 255, stdin);
            printf("%s\n", command);
            sleep(1);
        }
        exit(0);
    } else {

    }
}

then it works fine, but I wanted my input reading process to be a child as pid of the original process was a part of information given to my server process.然后它工作正常,但我希望我的输入读取过程是一个孩子,因为原始进程的 pid 是提供给我的服务器进程的信息的一部分。

Some additioanl information:一些附加信息:

I use makefile when compiling programs:我在编译程序时使用 makefile:

cc = gcc -Wall -D_DEFAULT_SOURCE -std=c11 -g

all: server client

dirs:
    mkdir -p build

server: dirs utils server.c
    $(cc) -c server.c -o build/server.o
    $(cc) build/server.o build/utils.a -o server
    
client: dirs utils client.c
    $(cc) -c client.c -o build/client.o
    $(cc) build/client.o build/utils.a -o client
    
utils: dirs utils.c
    $(cc) -c utils.c -o build/utils.o
    ar rcs build/utils.a build/utils.o
    
problem: dirs problem.c
    $(cc) -c problem.c -o build/problem.o
    $(cc) build/problem.o -o problem

clean:
    rm -Rf build *.out *.so *.o *.a

The parent process dies and the child process is adopted by the reaper or an subreaper process.父进程死亡,子进程被收割者或子收割者进程采用。 Therefore, your child process is "detached" from the terminal, and thus, the parent process have to call wait() or waitpid() .因此,您的子进程与终端“分离”,因此,父进程必须调用wait()waitpid()

 void init_reader(){
    int pid = fork();

    if (pid == 0){
        for(int i =0; i<10; i++){
            char command[255];
            fgets (command, 255, stdin);
            printf("%s\n", command);
            sleep(1);
        }
        exit(0);
    } else {
       wait(NULL);
    }
}

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

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