【问题标题】:Execute two functions at the same time independently in CC语言中同时独立执行两个函数
【发布时间】:2020-01-04 07:07:50
【问题描述】:

我认为我在 c 中可能存在线程问题,但我不确定。

我的目标是在 while(1) 循环中执行两个单独的函数,如下所示: 这些函数之一是 kbget() 以检索在非规范模式下在终端中按下的键。 第二种是通过 ioctl(1, TIOCGWINSZ,...) 函数不断获取终端窗口大小。

它通常不起作用,因为在执行第二个函数以重新评估终端窗口大小之前,while(1) 循环停止以获取用户的按键。如果在按下某个键之前调整终端窗口的大小,则不会执行评估其大小的函数,除非再次按下随机键。

换句话说,调整终端窗口的大小不会更新下面的 Window 结构中的大小值,除非按下一个键。 我希望程序在调整终端大小时更新 y_size 和 x_size 值“实时”。

以下是没有 POSIX 线程的代码中的问题: 执行:

gcc -Wall scr.h main.c -o main && ./main 

(scr.h 下面有 kbget() 来改变终端模式):

main.c:

#include "scr.h"
#include <sys/ioctl.h>

#define gotoyx(y, x)       printf("\033[%d;%dH", (y), (x))  // equivalent to move(y, x) in ncurses
#define del_from_cursor(x) printf("\033[%dX", (x))          // delete x characters from cursor position

typedef struct {
    int y_size;
    int x_size;
} Window;

int main(void) 
{
    printf("\033[?1049h\033[2J\033[H"); // remember position & clear screen 

    gotoyx(1, 10);
    printf("Press <ESC> to stop program.");
    gotoyx(2, 10);
    printf("Resizing the terminal window does not 'automatically' update the size shown on screen");

    Window w;
    struct winsize w_s;

    while (1) {

        // evaluate terminal size
        if (!ioctl(1, TIOCGWINSZ, &w_s)) {
            w.y_size = w_s.ws_row;
            w.x_size = w_s.ws_col;
        }

        // print terminal size and center it
        gotoyx(w.y_size / 2, w.x_size / 2);
        del_from_cursor(5);
        printf("w.y_size: %d", w.y_size);
        gotoyx((w.y_size / 2) + 1, w.x_size / 2);
        del_from_cursor(5);
        printf("w.x_size: %d", w.x_size);

        // get key pressed by user in terminal & exit if <ESC> is pressed
        if (kbget() == 0x001b) { break; }       
    }
    printf("\033[2J\033[H\033[?1049l"); // clear screen & restore
    return 0;
}

我尝试过使用线程解决这个问题,但到目前为止我没有成功。

我通过添加 2 个函数(get_window_size 和 get_key)修改了上面的 main.c 文件: (scr.h 中有 get_key() 中的 kbget() 函数可以将终端更改为规范模式)

main.c:

#include "scr.h"
#include <sys/ioctl.h>
#include <pthread.h>

#define gotoyx(y, x)       printf("\033[%d;%dH", (y), (x))
#define del_from_cursor(x) printf("\033[%dX", (x))

typedef struct {
    int y_size;
    int x_size;
} Window;

void *get_window_size(void *arg)
{
    Window *w = (Window *)arg;
    struct winsize w_s;
    if (!ioctl(1, TIOCGWINSZ, &w_s)) {
        w->y_size = w_s.ws_row;
        w->x_size = w_s.ws_col;
    }
    pthread_exit(0);
}

void *get_key(void *arg)
{
    int *key = (int *)arg;
    free(arg);
    *key = kbget();
    int *entered_key = malloc(sizeof(*key));

    *entered_key = *key;
    pthread_exit(entered_key);
}

int main(void)
{
    printf("\033[?1049h\033[2J\033[H");

    Window w;

    pthread_t tid[3];
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_create(&tid[0], &attr, get_window_size, &w);

    int *c = malloc(sizeof(*c));
    int *key_pressed;

    while (1) {
        // for initial size
        pthread_join(tid[0], NULL);

        // printing size to screen
        gotoyx(w.y_size / 2, w.x_size / 2);
        del_from_cursor(5);
        printf("w.y_size: %d", w.y_size);
        gotoyx((w.y_size / 2) + 1, w.x_size / 2);
        del_from_cursor(5);
        printf("w.x_size: %d", w.x_size);

        // get window size
        pthread_attr_t attr1;
        pthread_attr_init(&attr1);
        pthread_create(&tid[1], &attr1, get_window_size, &w);

        // get key entered by user
        pthread_attr_t attr2;
        pthread_attr_init(&attr2);
        pthread_create(&tid[2], &attr2, get_key, c);

        pthread_join(tid[1], NULL);
        pthread_join(tid[2], (void **)&key_pressed);

        if (*key_pressed == 0x001b) {
            break;
        } else {
            free(key_pressed);
        }
    }
    if (key_pressed != NULL) {
        free(key_pressed);
    }
    printf("\033[2J\033[H\033[?1049l");
    return 0;
}

scr.h 文件将终端模式更改为非规范(从此处调用上面的 kbget() 函数): 我认为 scr.h 中没有任何问题,因为它取自这里 (Move the cursor in a C program)。

scr.h:

#ifndef SCR_H
#define SCR_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

struct termios term, oterm;

int getch(void)
{
    int c = 0;
    tcgetattr(STDIN_FILENO, &oterm);
    memcpy(&term, &oterm, sizeof(term));
    term.c_lflag &= ~(ICANON | ECHO);
    term.c_cc[VMIN] = 1;
    term.c_cc[VTIME] = 0;
    tcsetattr(STDIN_FILENO, TCSANOW, &term);
    c = getchar();
    tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
    return c;
}

int kbhit(void)
{
    int c = 0;

    tcgetattr(STDIN_FILENO, &oterm);
    memcpy(&term, &oterm, sizeof(term));
    term.c_lflag &= ~(ICANON | ECHO);
    term.c_cc[VMIN] = 0;
    term.c_cc[VTIME] = 1;
    tcsetattr(STDIN_FILENO, TCSANOW, &term);
    c = getchar();
    tcsetattr(STDIN_FILENO, TCSANOW, &oterm);
    if (c != -1) { ungetc(c, stdin); }
    return c != -1 ? 1 : 0;
}

int kbesc(void)
{
    int c = 0;
    if (!kbhit()) { return 0x001b; } // 0x001b is the <ESC> key
    c = getch();
    if (c == 0) { while (kbhit()) { getch(); } }
    return c;
}

int kbget(void)
{
    int c = getch();
    return c == 0x001b ? kbesc() : c; // 0x001b is the <ESC> key
}
#endif // SCR_H

在使用 valgrind 执行时,我在上面的代码中使用 pthread 也收到错误 Invalid write of size 4

gcc -Wall scr.h main.c -pthread -o main
valgrind -v --leak-check=yes ./main

我知道 ncurses 和 pdcurses 的存在。我只是为自己做这个练习。

更新

我已将代码更改为以下内容,不幸的是,ret 变量永远不会更改为 -1:

#include "scr.h"
#include <errno.h>
#include <sys/ioctl.h>

#define gotoyx(y, x)       printf("\033[%d;%dH", (y), (x))
#define del_from_cursor(x) printf("\033[%dX", (x))

typedef struct {
    int y_size;
    int x_size;
} Window;

static int sigwinch_arrived = 0;

void sigwinch_handler(int signum) 
{ sigwinch_arrived = 1; }

void on_window_size_change(Window *w) 
{
    struct winsize w_s;
    // evaluate terminal size
    if (!ioctl(1, TIOCGWINSZ, &w_s)) {
        w->y_size = w_s.ws_row;
        w->x_size = w_s.ws_col;
    }

    // print terminal size in its center
    gotoyx(w->y_size / 2, w->x_size / 2);
    del_from_cursor(15);
    printf("w.y_size: %d", w->y_size);
    gotoyx((w->y_size / 2) + 1, w->x_size / 2);
    del_from_cursor(15);
    printf("w.x_size: %d", w->x_size);
}

int main(void) 
{
    printf("\033[?1049h\033[2J\033[H"); 

    gotoyx(1, 10);
    printf("Press <ESC> to stop program.");
    gotoyx(2, 10);
    printf("Resizing the terminal window does not 'automatically' update the size shown on screen");

    Window w;

    int ret;
    while (1) {

        // get key pressed by user in terminal & exit if <ESC> is pressed
        ret = kbget();
        gotoyx(10, 10);
        del_from_cursor(8);
        printf("ret: %d", ret);
        if (ret == -1) {
            if (errno == EAGAIN) {
                if (sigwinch_arrived) {
                    sigwinch_arrived = 0;
                    on_window_size_change(&w);
                }
            }
        } else if (ret == 0x001b) { 
            break; 
        }       
    }
    printf("\033[2J\033[H\033[?1049l"); 
    return 0;
}

【问题讨论】:

  • 你可以有一个读取消息队列的主线程。然后你有线程提供这个队列,一个在发生时发送按键,另一个发送屏幕大小,第三个可能发送定时器消息(虽然有一个用于定时器的整个线程有点矫枉过正,线程很便宜,它会让你的主循环非常简单) .
  • 恕我直言,termios 适合作为非规范模式下的fgets,但如果您需要完全控制终端,请不要重新发明轮子并考虑使用ncursesnewt
  • 文件中的大部分代码:scr.h 应该在一个名为scr.c 的单独文件中(可能)然后文件:scr.h 应该只包含功能的“接口”在scr.c
  • volatile static int sigwinch_arrived = 0; volatile 在这里可以提供帮助。

标签: c linux posix vt100


【解决方案1】:

扩展:根据this answer,如果你的ncurses是用--enable-sigwinch标志编译的,它会自动执行下面的解决方案(如果你在ncurses_init()之前没有覆盖SIGWINCH)。在这种情况下,如果发生调整大小事件,getch() (wgetch()) 将简单地返回 KEY_RESIZE


如果您的控制字符终端的大小发生变化,您的进程应该会收到一个SIGWINCH 信号(窗口大小变化,Linux 上的信号 28)。

可以由内核发送(如果字符桌面上有模式开关),也可以由虚拟终端软件(xterm、gnome-terminal、screen等)发送。

如果您的进程收到信号,则其阻塞内核调用(包括getch())会以-EAGAIN 错误号停止。这意味着,由于信号到达,阻塞调用在时间之前停止了。

请注意,从信号处理程序中,您不能做太多事情(例如:没有malloc()),如果您尽可能少做最好的事情。典型的信号处理程序更改一个静态的全局变量,其值由主程序检查。

未经测试的示例代码:

static int sigwinch_arrived = 0;

// this is called from the signal handler - nothing complex is allowed here
void sigwinch_handler(int signum) {
  sigwinch_arrived = 1;
}

// callback if there is a window size change
void on_window_size_change() {
   ...
}

// main program
...
while (1) { // your main event handler loop
  int ret = getch();
  if (ret == ERR) {
    if (errno == EAGAIN) {
      if (sigwinch_arrived) {
         sigwinch_arrived = 0;
         on_window_size_change();
      }
    }
  }
  ...
}

【讨论】:

  • 我已经更改了我的代码,但你的 ret 值从未更改为 -1?你知道为什么吗?谢谢
  • @sulla getch() 出错时返回 ERR。我在帖子中修复了它。 man getch.
猜你喜欢
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-03
相关资源
最近更新 更多