Solaris上的Ppoll [英] ppoll on solaris

查看:17
本文介绍了Solaris上的Ppoll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码可以在Linux中编译,但不能在Solaris中编译,因为pPoll()显然是特定于Linux的(我在Solaris中遇到了GCC未定义的符号错误)。需要帮忙把它改装一下吗?我不认为只使用poll()是个好主意,但话又说回来,这段代码不是我写的。(我从Writing a command line shell with C; trying to use ncurses/C for the first time获得)

#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/** VT100 command to clear the screen. Use puts(VT100_CLEAR_SCREEN) to clear
 *  the screen. */
#define VT100_CLEAR_SCREEN "33[2J"

/** VT100 command to reset the cursor to the top left hand corner of the
 *  screen. */
#define VT100_CURSOR_TO_ORIGIN "33[H"

struct frame_s
{
    int x;
    int y;
    char *data;
};

static int draw_frame(struct frame_s *frame)
{
    int row;
    char *data;
    int attrib;

    puts(VT100_CLEAR_SCREEN);
    puts(VT100_CURSOR_TO_ORIGIN);

    for (row = 0, data = frame->data; row  < frame->y; row++, data += frame->x)
    {
        /*  0 for normal, 1 for bold, 7 for reverse. */
        attrib = 0;

        /*  The VT100 commands to move the cursor, set the attribute, and the
         *  actual frame line. */
        fprintf(stdout, "33[%d;%dH33[0m33[%dm%.*s", row + 1, 0, attrib, frame->x, data);
        fflush(stdout);
    }

    return (0);
}

int main(void)
{
    const struct timespec timeout = { .tv_sec = 1, .tv_nsec = 0 };
    struct frame_s frame;
    struct termios tty_old;
    struct termios tty_new;
    unsigned char line[128];
    unsigned int count = 0;
    int ret;
    struct pollfd fds[1];
    sigset_t sigmask;
    struct tm *tp;
    time_t current_time;

    /*  Set up a little frame. */
    frame.x = 80;
    frame.y = 5;
    frame.data = malloc(frame.x * frame.y);

    if (frame.data == NULL)
    {
        fprintf(stderr, "No memory
");
        exit (1);
    }

    memset(frame.data, ' ', frame.x * frame.y);

    /*  Get the terminal state. */
    tcgetattr(STDIN_FILENO, &tty_old);
    tty_new = tty_old;

    /*  Turn off "cooked" mode (line buffering) and set minimum characters
     *  to zero (i.e. non-blocking). */
    tty_new.c_lflag &= ~ICANON;
    tty_new.c_cc[VMIN] = 0;

    /*  Set the terminal attributes. */
    tcsetattr(STDIN_FILENO, TCSANOW, &tty_new);

    /*  Un-mask all signals while in ppoll() so any signal will cause
     *  ppoll() to return prematurely. */
    sigemptyset(&sigmask);

    fds[0].events = POLLIN;
    fds[0].fd = STDIN_FILENO;

    /*  Loop forever waiting for key presses. Update the output on every key
     *  press and every 1.0s (when ppoll() times out). */
    do
    {
        fds[0].revents = 0;
        ret = ppoll(fds, sizeof(fds) / sizeof(struct pollfd), &timeout, &sigmask);

        if (fds[0].revents & POLLIN)
        {
            ret = read(STDIN_FILENO, &line[count], sizeof(line) - count);

            if (ret > 0)
            {
                line[count + ret] = '';

                if (strcmp(&line[count], "33[A") == 0)
                {
                    snprintf(frame.data, frame.x, "up");
                    count = 0;
                }
                else if (strcmp(&line[count], "33[B") == 0)
                {
                    snprintf(frame.data, frame.x, "down");
                    count = 0;
                }
                else if (line[count] == '
')
                {
                    snprintf(frame.data, frame.x, "entered: %s", line);
                    count = 0;
                }
                else
                {
                    count += ret;
                }
            }
        }

        /*  Print the current time to the output buffer. */
        current_time = time(NULL);
        tp = localtime(&current_time);
        strftime(&frame.data[1 * frame.x], frame.x, "%Y/%m/%d %H:%M:%S", tp);

        /*  Print the command line. */
        line[count] = '';
        snprintf(&frame.data[(frame.y - 1) * frame.x], frame.x, "$ %s", line);

        draw_frame(&frame);
    }
    while (1);

    /*  Restore terminal and free resources. */
    tcsetattr(STDIN_FILENO, TCSANOW, &tty_old);
    free(frame.data);

    return (0);
}

推荐答案

您目前可以在此处将ppoll()替换为poll()的原因是不是,因为提供给ppoll()的信号掩码是空的-这实际上是通常的情况-但因为ppoll()之前的信号掩码可能也是空的。

这是可能的,因为外壳程序当前不尝试处理任何信号。只要您想让外壳处理信号,就需要使用以下序列:

/* Block all signals */
sigprocmask(SIG_SETMASK, all_signals);

/* Check and handle any signals that have occured. */
check_signals();

/* Wait for activity on a file descriptor or a signal */
ppoll(..., empty_set);

ppoll()在这里是必需的,因为否则会出现竞争条件--信号可能会在check_signals();poll()调用之间到达,而这将被错过。因此,信号在此持续时间内被阻止,然后ppoll()中以原子方式解除阻止。

由于ppoll()是Solaris不提供的GNU扩展,因此您需要将代码更改为使用pselect(),这是POSIX标准函数。


若要将代码转换为使用pselect(),请将循环的开头替换为:

do
{
    fd_set rdset;
    int nfds = STDIN_FILENO + 1;

    FD_ZERO(&rdset);
    FD_SET(STDIN_FILENO, &rdset);
    ret = pselect(nfds, &rdset, NULL, NULL, &timeout, &sigmask);

    if (ret < 0) {
        if (errno == EINTR)
            continue;
        else
            break;
    }

    if (FD_ISSET(STDIN_FILENO, &rdset))
    {
        ret = read(STDIN_FILENO, &line[count], sizeof(line) - count);

    /* ... */

然后可以删除变量fds,因为不再需要它。

请注意,我已经添加了代码来检查pselect()的返回值是否也有错误-旧代码应该对ppoll()执行相同的操作。

这篇关于Solaris上的Ppoll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆