Kernel Korner - Easy I/O with IO Channels
Listing 1. A Complete and Working Console Application That Uses IO Channels to Communicate across Two Pipes
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <glib.h>
static gboolean
gio_in (GIOChannel *gio, GIOCondition condition, gpointer data)
{
GIOStatus ret;
GError *err = NULL;
gchar *msg;
gsize len;
if (condition & G_IO_HUP)
g_error ("Read end of pipe died!\n");
ret = g_io_channel_read_line (gio, &msg, &len, NULL, &err);
if (ret == G_IO_STATUS_ERROR)
g_error ("Error reading: %s\n", err->message);
printf ("Read %u bytes: %s\n", len, msg);
g_free (msg);
return TRUE;
}
static gboolean
gio_out (GIOChannel *gio, GIOCondition condition, gpointer data)
{
const gchar *msg = "The price of greatness is responsibility.\n";
GIOStatus ret;
GError *err = NULL;
gsize len;
if (condition & G_IO_HUP)
g_error ("Write end of pipe died!\n");
ret = g_io_channel_write_chars (gio, msg, -1, &len, &err);
if (ret == G_IO_STATUS_ERROR)
g_error ("Error writing: %s\n", err->message);
printf ("Wrote %u bytes.\n", len);
return TRUE;
}
void
init_channels (void)
{
GIOChannel *gio_read, *gio_write;
int fd[2], ret;
ret = pipe (fd);
if (ret == -1)
g_error ("Creating pipe failed: %s\n", strerror (errno));
gio_read = g_io_channel_unix_new (fd[0]);
gio_write = g_io_channel_unix_new (fd[1]);
if (!gio_read || !gio_write)
g_error ("Cannot create new GIOChannel!\n");
if (!g_io_add_watch (gio_read, G_IO_IN | G_IO_HUP, gio_in, NULL))
g_error ("Cannot add watch on GIOChannel!\n");
if (!g_io_add_watch (gio_write, G_IO_OUT | G_IO_HUP, gio_out,
NULL))
g_error ("Cannot add watch on GIOChannel!\n");
}
int
main (void)
{
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
init_channels ();
g_main_loop_run (loop); /* Wheee! */
return 0;
}
To be sure, this is an example rooted solely in explanation. It is silly to operate a pipe like this in a single application. Further, the program will continually read from and write to the pipe (you can kill the process with Ctrl-C). Nonetheless, this example serves a good purpose: it demonstrates event-driven programming and the utility of a main loop multiplexing I/O. The natural extension of this program would be to separate it into two processes, a consumer and a producer, and actually communicate interprocess over the pipe. Add a handful of other IO Channels, some GUI events, a few timers, and so on, to the main loop, and you will have a real program!
There are two ways to create a new IO Channel. The easiest method creates the IO Channel from an existing open file descriptor. The file descriptor can map to any object, including sockets and pipes:
GIOChannel *gio;
gio = g_io_channel_unix_new (fd);
if (!gio)
g_error ("Error creating new GIOChannel!\n");
As its name suggests, this function is UNIX-specific. Another method is available for creating an IO Channel in a platform-independent manner:
GIOChannel *gio;
GError *err = NULL;
gio = g_io_channel_new_file ("/etc/passwd"
"r",
&err);
if (!gio)
g_error ("Error creating new GIOChannel: %s\n",
err->msg);
The second parameter specifies the mode with which to open the file: one of r, w, r+, w+, a or a+. These values have the same meaning as with fopen(). For example, in this code snippet, we are asking to create a read-only IO Channel.
In our example program in Listing 1, we create two IO Channels using g_io_channel_unix_new(), one for each end of the pipe.
Given an IO Channel, creating a watch is easy:
guint ret;
ret = g_io_add_watch (gio, G_IO_IN, callback, NULL);
if (!ret)
g_error ("Error creating watch!\n");
The first parameter, gio, is the IO Channel we want to watch. The second parameter is a mask of one or more conditions for which to watch. The condition G_IO_IN is true when there is data to be read without blocking. Other conditions are G_IO_OUT (data can be written without blocking), G_IO_PRI (urgent data is available to read), G_IO_ERR (an error occurred) and G_IO_HUP (the connection was hung up). The third parameter is the callback function that the Glib main loop will invoke when the event occurs.
Watch callbacks take the following form:
gboolean callback (GIOChannel *gio,
GIOCondition condition,
gpointer data);
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Dynamic DNS—an Object Lesson in Problem Solving
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- Tech Tip: Really Simple HTTP Server with Python
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




8 min 59 sec ago
3 hours 20 min ago
5 hours 35 min ago
6 hours 4 min ago
7 hours 2 min ago
8 hours 31 min ago
9 hours 39 min ago
10 hours 26 min ago
17 hours 1 min ago
22 hours 40 min ago