Posts

Showing posts from 2016

Linux Debugfs implementation as a userspace interface

debugfs is a Linux file system. It is a user space interface to and from the Linux kernel. It is mounted at /sys/kernel/debug.  The kernel must be compiled with the   CONFIG_DEBUG_FS option to use this file system. It exists in the Linux kernel since version 2.6.10-rc3 [1] In the below example, you will see an example to a debugfs. A 32-bit flag is created under the /sys/kernel/debug/debugfs_ex directory called enable for read and write. debugfs_ex.c #include <linux/version.h> #include <linux/module.h> #include <linux/debugfs.h> static struct dentry * dn; /* Dentry object for debugfs directory */ static int enable; /* Enable flag for debugfs_ex module */ /* * debugfs_ex_init() */ static int __init debugfs_ex_init ( void ) { printk(KERN_INFO "debugfs_ex init \n " ); dn = debugfs_create_dir( "debugfs_ex" , NULL ); if ( ! dn) { printk(KERN_ERR "Failed to create debugfs_ex directory in debugfs \n " ); ...

A simple Stack implementation: push, pop, print_stack

In this source code, a simple stack is implemented.  A single array is used to implement 3 stacks.  Let's say we have an array with a size of 300. It consists of 3 stacks concatenated to each other. The user can add/remove elements from each stack by passing the stack number as a parameter to the push/pop functions.  Usage: push(s, 12, 1); // Pushes element 12 to the stack number 1. push(s, 2, 3); // Pushes element 42 to the stack number 3. pop(s, 3); // Pops an element from the stack number 3. Source Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #define NUM_OF_STACK 3 #define STACK_SIZE 10 /* * stack data structure */ struct stack { int array[STACK_SIZE * NUM_OF_STACK]; int size[NUM_OF_STACK]; }; /* * print_stack() * Prints the stack */ void print_stack ( struct stack * s) { int i; for (i = 0 ; i < STACK_SIZE; i ++ ) { printf( "%d " , s -> array[i]...