Skip to main content

Posts

Showing posts from August, 2011

How to add /proc file system entry for the module, illustrated here

here is simple example to demonstrate adding /proc file system entry for the module, as follows shiv@ubuntu:~/ds/linx$ cat heloproc.c /*  * Kernel program demo to illustrate how to create/add entry to  * /proc file system  */ #include <linux/init.h> #include <linux/module.h> #include <linux/proc_fs.h> /* Module license */ MODULE_LICENSE("Dual BSD/GPL"); /*  * This is the service routine for added /proc entry  * when user try to look into /proc/<entry added> this routine will be executed  *  * char *buf - below is page allocated by kernel and passed to service routine  * eof and start would be used only when using /proc for  * getting more than one page (usually page is 4KB)  * for complete info you could refer linux docs  */ int demo_read_fs(char *buf, char **start, off_t offset, int count, int *eof, void *data) {   int len=0;  len = sprintf(buf, "This is Demo proc fs creation\n\n");  return len; } /*  * cre

sample program to demonstrate writing daemon program in C and linux

/*  * program to demonstrate writing daemon programs in C and linux  * This Daemon print(adds) time or date every minute, depending on  * user choice t or d to syslog file  */ #include <stdio.h> #include <sys/stat.h> #include <syslog.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> #include <time.h> #include <stdlib.h> /*  * parameters to daemon using argc and argv  */ int main(int c, char *argv[]) {  pid_t pid, sid;  char ch;  time_t t;  char  buf[80];  struct tm *ts; /*  * this daemon expects one argument  * as daemon name is count number 1 and one arg to daemon  * so checking for count <2  */  if(c<2) {     printf("usage: <program name> d | t \n");     return 0;  }  ch = argv[1][0];  printf("The choice is %c\n", ch);  if (!((ch =='d') || (ch == 't'))) {      printf("Not right choice ..\n");      return 0;  }  /* step 1 of da

Interactive C program to implement Instert begining, delete begining and reversing of single linked list

shiv@ubuntu:~/ds/list$ cat   revlist.c /*  * Interactive C program to simulate following operations  * on single linked list  * 1. insert begining of the list  * 2. display data in the list (traversing)  * 3. delete element at begining of the list  * 4. reverse the elements in single linked list  */ #include<stdio.h> #include<stdlib.h> struct list { int data; struct list *next; }; typedef struct list *L; /* function to insert element into list */ void insert(L *head) {  /* look for typedef temp is pointer not variable,   *  same will apply to all   */  L temp;  temp=malloc(sizeof(struct list));  if(!temp) {     printf("Can't allocate memory\n");     return;  }  printf("Enter element to insert\n");  scanf("%d", &temp->data);  temp->next = NULL;  if(*head == NULL) {     *head = temp;  } else {     temp->next = *head;     *head = temp;  } } /* function to display elements in the list */ vo

Interactive program to simulate Queue functionality using Linked list

/*  * Copy right by Shiv Yaragatti  * you are free to use, modify  and redistribute code on own risk  * Interactive C Program to implement Queue using  linked list  */ /* header needs have all required header files like stdio.h etc */  #include "header.h" struct queue { int data; struct queue *next; }; typedef struct queue *Q; /* Queue head */ struct qhead_struct { struct queue  *head; struct queue  *tail; }; typedef struct qhead_struct  *H; /* Insert elements into queue */ void insert(H *hq) {   int e;   Q temp;  temp= malloc(sizeof(struct queue));  if(!temp) {    printf("Can't allocate memory\n");    return;  }  printf("Enter element\n");  scanf("%d", &e);  temp->data =e;  temp->next = NULL; /* add element queue at tail */  if((*hq)->head == NULL) {     (*hq)->head =(*hq)->tail =temp;  } else {     (*hq)->tail->next = temp;     (*hq)->tail = temp;        }      } /* delete e

Interactive C program to implement stack using doubly linked list

shiv@ubuntu:~/ds/stack$ cat stack.c /*  * Copy right by Shiv Yaragatti  * you are free to use, modify  and redistribute code on own risk  * Interactive C Program to implement STACK using Doubly linked list  */ #include "header.h" struct stack { int data; struct stack *next; struct stack *prev; }; typedef struct stack *S; /*Push elements in to stack */ void push(S *stack) {   int e;   S temp;   temp=malloc(sizeof(struct stack));   if(!temp){      printf("Can't allocate memory\n");      return;  }  printf("Enter element to push\n");  scanf("%d", &e);  temp->data=e;  if(*stack == NULL){     temp->next = temp->prev = NULL;     *stack=temp;  } else {    temp->next = *stack;    (*stack)->prev = temp;    temp->prev = NULL;    *stack=temp;  } } /* Display elements in the stack */ void display(S s){    S temp;    temp=s;    printf("Elements are ...\n");    while(temp) {     printf("%d\n", temp->data