Saturday 13 November 2021

How to disable Sleep on Ubuntu Server 20.04

By disabling sleep service in ubuntu , it can be achieve in Ubunutu system :

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

Tuesday 31 January 2017

Basic Operations in Singly Linked List


Following are the basic operations supported by a list.
In series of linked list , we studied  Linked List Types

In this section will see operation of Linked List

Operation in Linked List:
  1. Insertion −  Adds an element at the beginning of the list.
  2. Deletion  −  Deletes an element at the beginning of the list.
  3. Display    −  Displays the complete list.
  4. Search     −  Searches an element using the given key.
  5. Delete      −  Deletes an element using the given key.


#include<stdio.h>
#include<stdlib.h>
 
struct node
{
    int data;
    struct node *next;
}*head;
 
 
 
void append(int num)
{
    struct node *temp,*right;
    temp= (struct node *)malloc(sizeof(struct node));
    temp->data=num;
    right=(struct node *)head;
    while(right->next != NULL)
    right=right->next;
    right->next =temp;
    right=temp;
    right->next=NULL;
}
 
 
 
void add( int num )
{
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    if (head== NULL)
    {
      head=temp;
      head->next=NULL;
    }
    else
    {
      temp->next=head;
      head=temp;
    }
}
void addafter(int num, int loc)
{
    int i;
    struct node *temp,*left,*right;
    right=head;
    for(i=1;i<loc;i++)
    {
      left=right;
      right=right->next;
    }
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    left->next=temp;
    left=temp;
    left->next=right;
    return;
}
 
 
 
void insert(int num)
{
    int c=0;
    struct node *temp;
    temp=head;
    if(temp==NULL)
    {
      add(num);
    }
    else
    {
      while(temp!=NULL)
      {
        if(temp->data<num)
        c++;
        temp=temp->next;
      }
      if(c==0)
        add(num);
      else if(c<count())
        addafter(num,++c);
      else
        append(num);
    }
}
 
 

int delete(int num)
{
    struct node *temp, *prev;
    temp=head;
    while(temp!=NULL)
    {
      if(temp->data==num)
      {
         if(temp==head)
         {
            head=temp->next;
            free(temp);
            return 1;
          }
          else
          {
             prev->next=temp->next;
             free(temp);
             return 1;
          }
        }
        else
        {
           prev=temp;
           temp= temp->next;
        }
      }
    return 0;
}
 
 
void  display(struct node *r)
{
    r=head;
    if(r==NULL)
    {
      return;
    }
    while(r!=NULL)
    {
       printf("%d ",r->data);
       r=r->next;
    }
    printf("\n");
}
 
 
int count()
{
    struct node *n;
    int c=0;
    n=head;
    while(n!=NULL)
    {
      n=n->next;
      c++;
    }
    return c;
}
 
 
int  main()
{
    int i,num;
    struct node *n;
    head=NULL;
    while(1)
    {
    printf("\nList Operations\n");
    printf("===============\n");
    printf("1.Insert\n");
    printf("2.Display\n");
    printf("3.Size\n");
    printf("4.Delete\n");
    printf("5.Exit\n");
    printf("Enter your choice : ");
    if(scanf("%d",&i)<=0){
        printf("Enter only an Integer\n");
        exit(0);
    } else {
        switch(i)
        {
        case 1:      
                 printf("Enter the number to insert : ");
                 scanf("%d",&num);
                 insert(num);
                 break;
        case 2:     
                if(head==NULL)
                {
                   printf("List is Empty\n");
                }
                else
                {
                   printf("Element(s) in the list are : ");
                }
                display(n);
                break;
        case 3:     
                printf("Size of the list is %d\n",count());
                break;
        case 4:     
                if(head==NULL)
                  printf("List is Empty\n");
                else {
                  printf("Enter the number to delete : ");
                  scanf("%d",&num);
                  if(delete(num))
                    printf("%d deleted successfully\n",num);
                  else
                    printf("%d not found in the list\n",num);
                }
                break;
        case 5:     
                return 0;
        default:    
                printf("Invalid option\n");
        }
    }
  }
    return 0;
}

Types of Linked List

Linked List :
A linked list is a data structures, which are connected together via links.
Definition :
Linked List is a sequence of links which contains items (data). Each link contains a connection to another link. Each record of a linked list is often called an node. The field of each node that contains the address(pointer of next link) of the next node is usually called the 'next link' or 'next pointer'. 
Example:
struct node {
  int data;
  struct node *next;
}

Here struct node is structure where data is node and next is link or pointer.

Types of Linked List:
Following are the various types of linked list.
  1. Simple Linked List − Item navigation is forward only.
  2. Doubly Linked List − Items can be navigated forward and backward.
  3. Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous.

Monday 30 January 2017

Execute shell command using popen() in C/C++


The popen() syscall executes a command and pipes the executes command to the calling program.
This syscall return a pointer to the stream which can be used by calling program to read and write to the pipe.

Below are the C/C++ snippets to run a simple command, read the stream from pipe (execute ls command)  and then write to console (display to console)

#include <stdio.h>
#include <stdlib.h>

int main(void)  {
 FILE *fp;
 char buff[1024];

 //Return NULL if popen fail 
 if(!(fp = popen("ls -sail", "r"))){
     printf("Fail to open popen\n");
     exit(1);
 }

 //Read the stream and print on screen
 while(fgets(buff, sizeof(buff), fp)!=NULL){
     printf("%s", buff);
 }

 pclose(fp);
 return 0;
}


Determine parent process of given process in Linux

Determine Parent Process of given Process in Linux

There is two way to determine
1st Method:

Using ps command will able to determine the parent process ID of given process
Use:
ps -o ppid= <process id>
Example:
ps -o ppid=


Example Shell Script:
#!/bin/sh
pid=$1
if [ -z $pid ]
then
    read -p "PID: " pid
fi
ps -p ${pid:-$$} -o ppid=


2nd Method:

procfs structure given details of PPID of given pid

Use:
cat /proc/<pid of process>/status --> Search PPID

Example Shell Script:
#!/bin/sh
P=$1
if [ -z "$P" ]; then
    read P
fi
cat /proc/"$P"/status | grep PPid: | grep -o "[0-9]*"

Thursday 21 April 2016

Package require to analyze kernel core dump in RHEL system


To analyze the system crash dump file (vmcore) , we must have crash and debuginfo package installed on system. 

To install the crash package in your system, run the following as root:
#yum install crash
To install the kernel-debuginfo package, make sure that you have the yum-utils package installed and run the following command as root:
#debuginfo-install kernel
If not able to download the debuginfo kernel package with above command, then download the package from ftp server: http://ftp.heanet.ie/mirrors/puias/updates/


For example to download the RHEL6 (32-bit) related debuginfo package from:
http://ftp.heanet.ie/mirrors/puias/updates/6Server/en/os/debug/i386/

For example to download the RHEL6 (64-bit) related debuginfo package from:
http://ftp.heanet.ie/mirrors/puias/updates/6Server/en/os/debug/x86_64/


Package need to download:
1) Determine the kernel version of generated "vmcore" system , by executing "uname -r" command
2) Download the kernel-debuginfo-`uname -r`.rpm and kernel-debuginfo-common-`uname -r`.rpm
3) Install the package through rpm command i.e "rpm -ivh <package name>




Thursday 14 April 2016

Printk Timestamp Enable Method

We can enable and disable printk timestamps at runtime, by writing to /sys/module/printk/parameters/time.
By default printk timestamp is disable : 
[root@localhost ~]# cat /sys/module/printk/parameters/time
N

In order to enable the printk timestamp execute the below command:
[root@localhost ~]# echo "1" > /sys/module/printk/parameters/time
[root@localhost ~]# cat /sys/module/printk/parameters/time
Y


[root@localhost ~]# dmesg
[11320.847977] Hello World..

After that will able to see the timestamp. 

In order to disable to printk timestamp execute the below command
[root@localhost ~]# echo "0" > /sys/module/printk/parameters/time
[root@localhost ~]# cat /sys/module/printk/parameters/time
N