Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6872599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:57:00+00:00 2026-05-27T03:57:00+00:00

An app needs to be able to set callbacks on various events. The events

  • 0

An app needs to be able to set callbacks on various events. The events can be any of:

  • A UTC time has been reached

  • Data has arrived at a file descriptor

  • A child process has completed.

I need to write a function that will dispatch the events. The app will run on an embedded platform and should take reasonable steps to minimise CPU utilisation.

One approach would be a busy loop:

#define MAX_EVENT 10

typedef enum tagEventType {EV_DATA, EV_CHILD, EV_TIME} tEventType;
typedef void(*tEventCallback)(int);
typedef struct tagEvent
{
    int sequence;
    tEventType type;
    tEventCallback callback;
    time_t time;
    int fd;
    pid_t pid;    
} tEvent;

static tEvent eventsTable[MAX_EVENT];    

static void processEvents(void)
{
    int i;
    for ( i=0; i<MAX_EVENT; ++i )
    {
        if ( eventsTable[i].sequence > 0 )
        {
            switch(eventsTable[i].type)
            {
                case EV_DATA:
                    if ( checkForDataUsingPoll( eventsTable[i].fd) )
                    {
                        eventsTable[i].callback(eventsTable[i].sequence);
                    }                    
                    break;
                case EV_CHILD:
                    if ( kill(eventsTable[i].pid, 0) == -1 )
                    {
                        eventsTable[i].callback(eventsTable[i].sequence);
                        eventsTable[i].sequence = 0;                        
                    }
                    break;
                case EV_TIME:
                    {
                        if ( time(NULL) > eventsTable[i].time )
                        {
                            eventsTable[i].callback(eventsTable[i].sequence);
                            eventsTable[i].sequence = 0;
                        }
                    }
                    break;
            }
        }
    }
}

int main(  int argc, char* argv[] )
{
    signal(SIGCHLD, SIG_IGN);
    while(1)
    {
        processEvents();
        usleep(1000);
    }
    return 0;    
}

checkForDataUsingPoll() uses poll() with a timeout of zero to check whether data has arrived at the file descriptor. I’ve omitted it for brevity.

Is there a way of doing this which avoids the busy loop? Some mixture of poll, signals, and alarm perhaps?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:57:00+00:00Added an answer on May 27, 2026 at 3:57 am

    For file descriptors, there is poll (which you already use) or select or epoll or libevent. You can also check the SIGIO signal, or the functions prefixed with aio_.

    For the time, you can check the alarm system call.

    For termination of child processes, check the SIGCHLD signal.

    Edit To answer the comment: For the signals, you don’t really have to do anything, the signal handlers you set will be called automatically by the system when they occur. If they happen set a flag and possible other variables to inform your program what happened.

    When a signal is received, poll will return with an error and errno set to EINTR. If that is the case, check the flags set by the signal handlers to see what happened, and call whatever functions you need. If it was an alarm, then you have to restart the alarm for the next event. Then call poll again, waiting for the next event to occur.

    Unless you have other things to do, you can even call poll with an infinite timeout. It will return on signals or file descriptor ready.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that needs to be able use either an sqlite3 datebase
I'm making a django app that needs to be able to make emails and
I am making an app that needs to be able to share stories to
I have an application which needs to be able to write to Any User/Current
I have an HTML/JavaScript Adobe AIR app which needs to be listening for data
my app needs to both burn discs and create burn folders. i'm able to
Our Rails 3 app needs to be able to accept foreign characters like ä
I have a web app that needs to be able to convert word documents
I'm in the process of developing an Android app. I have been able to
Issue: The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.