Thursday, February 5, 2015

Brief differences between tasklets and Softirqs

Formulated this comparison based on books and posts I read so far:

Click on the image to enlarge.

Explanation and code snippet to read hotplug events from a netlink socket

Complete source file can be downloaded here:
Source code for this program is here.
Complete repository can be downloaded or cloned here.

Parts of the code are as explained below:
The kernel notifies the userspace like (udev) about new available devices by  (1) Spawning a process or (2) a packet to a netlink socket on attach. Code taken directly from the kernel documentation:
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>

  #include <sys/poll.h>
  #include <sys/socket.h>
  #include <sys/types.h>
  #include <unistd.h>

  #include <linux/types.h>
  #include <linux/netlink.h>
Error handling:
  void die(char *s)
Main file:
  int main(int argc, char *argv[])
 struct sockaddr_nl nls;
 struct pollfd pfd;
 char buf[512];

 // Open hotplug event netlink socket

 memset(&nls,0,sizeof(struct sockaddr_nl));
 nls.nl_family = AF_NETLINK;
 nls.nl_pid = getpid();
 nls.nl_groups = -1; = POLLIN;
 if (pfd.fd==-1)
  die("Not root\n");
Listen to the socket:
 if (bind(pfd.fd, (void *)&nls, sizeof(struct sockaddr_nl)))
  die("Bind failed\n");
 while (-1!=poll(&pfd, 1, -1)) {
  int i, len = recv(pfd.fd, buf, sizeof(buf), MSG_DONTWAIT);
  if (len == -1) die("recv\n");
Print the data once it is read:
  i = 0;
  while (i<len) {
   printf("%s\n", buf+i);
   i += strlen(buf+i)+1;

 // Dear gcc: shut up.
 return 0;
Running the code: