5

I've recently been exposed to GLib and am playing around with it. I have been able to replace a good deal of my C library functions with GLib functions (as an aside, I'm a huge fan of the command line handling!). However, one thing I haven't been able to find much documentation on is signal handling from the OS. Namely in Linux, is there a better way that I can handle CTRL+C or other signals to terminate the application? Currently I use signal(), but I'm wondering if there's a way that I can set up my handler for CTRL+C through GLib.

As always, thanks for the help.

It'sPete
  • 5,083
  • 8
  • 39
  • 72

1 Answers1

7

There is a way of using signals via GLIB! These are some of functions available for handling signals:

guint               g_unix_signal_add                   (gint signum,
                                                         GSourceFunc handler,
                                                         gpointer user_data);
guint               g_unix_signal_add_full              (gint priority,
                                                         gint signum,
                                                         GSourceFunc handler,
                                                         gpointer user_data,
                                                         GDestroyNotify notify);
GSource *           g_unix_signal_source_new            (gint signum);

Check out UNIX-specific utilities and integration, , g_unix_signal_add().

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
  • So I can just replace signal() with g_unix_signal_add()? That seems deceptively easy... – It'sPete Jul 26 '13 at 06:17
  • Why should it be more difficult? g_unix_signal_add is definied as a convenience function for g_unix_signal_source_new(), which attaches to the default GMainContex. You can achieve the same result using also this function g_unix_signal_source_new (). – Giuseppe Pes Jul 26 '13 at 06:33
  • What is the platform independent way to solve this? – Fabian Keßler Jun 07 '23 at 15:26