Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Close XLib application from another process

I have a Xlib-based program with an event loop that uses XNextEvent to receive and process relevant events.

I would like to be able to gracefully close this program from another process (actually from a shell script). I need to do some cleanup when closing, so I considered to setup a signal handler (for example for SIGUSR1) and when this signal is received, do the appropriate cleanup.

My question is, how can I interrupt the (blocking) XNextEvent call from the signal handler?

Any other suggestions?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I found a way to do this based on this SO question and this one.

Basically you can use the ConnectionNumber() macro to get the fd that XNextEvent() is reading from. This lets me call select() myself to wait for data on the Xlib fd and some other fd. Now it is select() that is blocking, and not XNextEvent(). I can easily unblock select() from my signal handler by writing to the second fd.

Pseudo-code for the event loop:

/* Get X11 fd */
x11_fd = ConnectionNumber(display);

while(1) {
    /* Create a File Description Set containing x11_fd and other_fd */
    FD_ZERO(&in_fds);
    FD_SET(x11_fd, &in_fds);
    FD_SET(other_fd, &in_fds);

    /* Wait for X Event or exit signal */
    ret = select(nfds, &in_fds, ...);
    if (FD_ISSET(other_fd, &in_fds) {
        /* Do any cleanup and exit */
    } else {
        while (XEventsQueued(display, QueuedAlready) > 0) {
            /* Process X events */
        }
    }
}
over 4 years ago · Santiago Trujillo Report

0

Assuming you have the process id, you can use the kill function:

int kill(pid_t pid, int sig);

You can send any signal but SIGKILL (SIGKILL cannot be handled)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!