c - 在 C 中将字符写入管道

标签 c char signals fork pipe

我有以下程序,它基本上是从键盘读取字符(getch() 无需单击“ENTER”即可执行此操作,函数取自此处:Capture characters from standard input without waiting for enter to be pressed) ,然后如果 char 是合法移动之一(左、右等),它会将其写入管道,draw.out 正在从中读取数据。

draw.out 正在读取它收到的输入并打印到屏幕上。 (假设这个程序工作正常,我保证问题只在下面的代码中)。

问题在于:

  1. draw.out 的表现就像它一遍又一遍地接收我的输入。这意味着,出于某种原因,即使我只按了一次 DOWN 键,它也会发送 draw.out,就好像我点击了很多次一样。

  2. 发送一个输入后,我无法再发送,好像循环停止了。

我花了好几个小时来解决这个问题......我真的很感激帮助。

int main(int argc, const char* argv[])
{
pid_t child_pid;
int fda[2];
if(pipe(fda)<0)
    perror("pipe error");

if((child_pid=fork())<0)
    perror("fork error");
else
{
    //if we're in father
    if(child_pid>0)
    {
        char c=' ';

        //close read side
        close(fda[0]);

        while(c!=QUIT)
        {
            //get an instruction from keyboard
            while(c!=ROTATE && c!=LEFT && c!=RIGHT &&
                    c!=DOWN && c!=QUIT)
            {
                c=getch();
            }

            //write the instruction to pipe
            write(fda[1],&c,1);
            //notify the child
            kill(child_pid,SIGUSR2);

        }
    }
    //if we're in child process
    else
    {
        dup2(fda[0],0);
        close(fda[0]);
        close(fda[1]);
        execl("./draw.out","./draw.out",NULL);
    }
}

//close everything
close(fda[0]);
close(fda[1]);
return 0;
}

//this function works well in linux with tsch installed or in SSH bash shell
char getch()
{
char buf = 0;
struct termios old = {0};
if (tcgetattr(0, &old) < 0)
        perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &old) < 0)
        perror("tcsetattr ICANON");
if (read(0, &buf, 1) < 0)
        perror ("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if (tcsetattr(0, TCSADRAIN, &old) < 0)
        perror ("tcsetattr ~ICANON");
return (buf);
}

最佳答案

发送字符 c 后,您需要将其设置为 ROTATELEFTRIGHT< 以外的值DOWN

如果您不这样做,getch() 将永远不会被调用第二次...

所以在外部 while 的末尾(就在 kill 之后)或在循环的开始(就在包含 while 之前) getch() 调用)添加类似

的内容
if (c != QUIT)
  c = ' ';

关于c - 在 C 中将字符写入管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16121745/

相关文章:

c - ICU 转换与 ucnv_toUnicode

c++ - cplusplus中char数组和string的区别

c - 从函数返回的 char* 数组打印不正确的值(可能是内存管理)

c++ - 将 NON-POD 类型传递给 Variadic 函数是未定义的行为?

c - 将 srand() 放在头文件中是个好主意吗?

java - 将 char 与 java 与 python 中的字符串索引进行比较

linux - 在 Linux 内核中杀死一个(据说)忙碌的进程?

c - 信号和信号量系统V

c++ - 信号 - 循环内的槽连接

c - Malloc 在为整数分配空间时表现异常