josephsturm
josephsturm

Reputation: 323

How can a C program poll for user input while simultaneously performing other actions in a Linux environment?

Background:

I'm a relatively inexperienced developer trying to write software to interface with a PCI motion controller. I'm using C (compiled with gcc) on Ubuntu Linux 18.04.

The program I'm writing needs to regularly check for unsolicited status messages sent by the motion controller (approx. once per second) and display any messages it finds on a terminal screen (for which I'm using the ncurses library).

What I have:

Right now, to do this, I'm calling a function that checks for unsolicited messages in a while loop. The code is roughly akin to:

while (1)
{
    // check for messages from PCI and store them in a traffic buffer
    checkForMessages(PCIconnection, trafficBuffer);

    // output the traffic buffer to the screen
    printf("%s", trafficBuffer);    
}

What I need:

I need the user to be prompted for input in a way that allows them to end the loop. For example, the user could input end causing the loop to stop and the program to continue.

The problem:

I'm not aware of a way to achieve this without putting fgets inside the while loop, causing the program to stop and wait for the user to input something on every loop iteration.

I've looked for a solution, but I haven't been able to find discussion on how to achieve the functionality I need. Opening a new thread or process seems like a step in the right direction?

I'm open to completely restructuring my code if what I'm currently doing is poor practice.

Thank you for any help!

Upvotes: 12

Views: 2320

Answers (3)

Kit.
Kit.

Reputation: 2406

I believe that the "Unix" way would be not to ask for user input, but to react to a user signal. For example, when the user presses Ctrl-C, the currently running process receives SIGINT.

An example how to properly use SIGINT to interrupt a loop can be found here. Copying it into the answer in case the link gets stale:

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

static volatile sig_atomic_t got_signal = 0;

static void my_sig_handler(int signo)
{
    got_signal = 1;
}

int main()
{
    struct sigaction sa;

    memset(&sa, 0, sizeof(struct sigaction));
    sa.sa_handler = &my_sig_handler;
    if (sigaction(SIGINT, &sa, NULL) == -1) {
        perror("sigaction");
        return EXIT_FAILURE;
    }

    for (;;) {
        if (got_signal) {
            got_signal = 0;
            printf("Received interrupt signal!\n");
        }
        printf("Doing useful stuff...\n");
        sleep(1); /* Sleep is not only useful, it is essential! */
    }
    return EXIT_SUCCESS;
}

(in your case it would be a good idea to put break; into the if block or to use while(!got_signal))

Upvotes: 3

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136515

Your task requires an event loop based on select or epoll. One event it would wait for is user input - when STDIN_FILENO becomes ready for read. Another is the 1-second periodic timer when you need to poll the controller.

There are quite a few libraries that implement an event loop for you so that you can focus on what events you need to handle and how. libevent is one of the oldest, feature rich and popular.

Upvotes: 12

user5550963
user5550963

Reputation:

Simple answer is multi-threading, where you have thread deployed to wait for user input, while loop continues on. So have this:

char flag = 1;

while (flag) {
     // run the loop

     // if thing happens deploy the thread which will ask user for input

}

I have not done threading in a while, I think this page would be better than me trying to explain it to you: https://randu.org/tutorials/threads/

Upvotes: 3

Related Questions