Saturday, February 27, 2016

Stack Program Using C++

************** Stack Program Using C++*******************

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name :- MyStack.cpp

Description :- This program illustrate the concept of stack in C++. Object Oriented Implementation of stack.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<iostream>
using namespace std;

typedef int BOOL;

#define TRUE 1
#define FALSE 0

//
// Declaration of class stack
//

class Stack
{
        //
        // Class Member
        //
        private:
                    int top;
                    int MAX;
                    int *arr;

       public :
                   Stack();                   // Default Constructor.
                  Stack(int);               // Parameterize constructor.
                  Stack(Stack &);      // Copy Constructor.
                  ~Stack();                 // Destructor.

   ////////////// /////////Wrapper Functions ///////////////////////////////////

                  void Display();    
                  void Push(int);
                  int Pop();

 /////////////////////// Helper Functions ///////////////////////////////////////

      private:
                 BOOL IsStackFull();
                 BOOL IsStackEmpty();
};

/////////////////////////////// Function Definations ////////////////////////////////////////////////////////

Stack :: Stack()
{
         top = 0;
         MAX = 0;
        arr = new int[MAX];
}


Stack :; Stack( int Size = 10)
{
             top = -1;
             MAX = Size;
             arr = new int[MAX];
}

Stack :: Stack(Stack &ref)
{
          top = ref.top;
          MAX  = ref.MAX;
          arr = new int[MAX];
       
          for(int i = 0; i < ref.MAX; i++)
          {
                  arr[i] = ref.arr[i];
           }
}

Stack :: ~Stack()
{
           delete arr[];
}

//
// To Check Stack is Full or Not
//

BOOL Stack :: IsStackFull()
{
             if( top == MAX)
             {
                            return TRUE;
             }
              else
             {
                            return FALSE;
              }
}

//
// To Check Stack is Empty of not
//

BOOL Stack :: IsStackEmpty()
{
            if( top == -1)
            {
                     return TRUE;
             }
             else
             {
                    return FALSE;
              }
}

//
// Insert Element in to Stack
//

void Stack :: Push(int iNum)
{
         if(IsStackFull())
         {
                return;
         }
         arr[++top] = no;
}

//
// Delete Element From Stack
//
int Stack :: Pop()
{
         if(IsStackEmpty())
        {
               return -1;
        }
        return arr[top--];
}

//
// Display Element of Stack
//

void Stack :: Display()
{
       for(int i = top; i >= 0; i--)
       {
             count<<arr[i];
       }
}

//
// Entry Point Function
//

int main()
{
         Stack obj1;       // Size is 10
         Stack obj2(20);       // Size is 20

         obj1.Push(10);
         obj1.Push(20);

         obj2.Push(33);
         obj2.Push(40);
      
        Stack obj3(obj1);         // Copy Constructor

         cout<<obj1.Pop();     // 20

        obj3.Display();           // 20, 10

         return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                 

Friday, February 26, 2016

Kernel Module Basic Programs

*******************  Kernel Module Basic Programs************************************

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name :- Module1.c

Description :- This program illustrate the concept of first kernel module program.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<linux/module.h>
#include<linux/kernel.h>

int init_module(void)
{
printk(KERN_INFO "Jay shreeKrishna\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Hare Ram\n");
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

File Name :- Makefile

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

obj-m += Module1.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Program Name :- Module2.c

Description : - This program illustrate the concept, we have to write your user defined entry point functions and its calling mechanism.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>

static int __init Module_2_init(void)
{
printk(KERN_INFO "Jay shreeKrishna\n");
return 0;
}
static void __exit Module_2_exit(void)
{
printk(KERN_INFO "Jay RadheShyam\n");
}
module_init(Module_2_init);
module_exit(Module_2_exit);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

File Name :- Makefile

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

obj-m += Module2.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name :- Module3.c

Description :- This program illustrate concept of to define integer variable and print it in kernel log file using kernel module. 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>

static int Module3_data __initdata = 3;

static int __init Module_3_init(void)
{
printk(KERN_INFO "Jay shreeKrishna %d\n", Module3_data);
return 0;
}
static void __exit Module_3_exit(void)
{
printk(KERN_INFO "Jay RadheShyam\n");
}
module_init(Module_3_init);
module_exit(Module_3_exit);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

File Name :- Makefile

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

obj-m += Module3.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name :- Module4.c

Description :- This program illustrate the concept of GPL Licening schem. See this information using command as " modinfo Module4.ko " 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>

static int __init Module_4_init(void)
{
printk(KERN_INFO "Hello Sunil \n");
return 0;
}

static void __exit Module_4_exit(void)
{
printk(KERN_INFO "Bye Sunil \n");
}

module_init(Module_4_init);
module_exit(Module_4_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Sunil Bhagwat");

MODULE_DESCRIPTION("This my kernel module");

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

File Name :- Makefile

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

obj-m += Module4.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name :- Module5.c

Description :- This program illustrate the concept of fulle GPL Licening schem.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/*
* hello3.c - Demonstrates module documentation.
*/

#include<linux/module.h> /* Needed by all modules */
#include<linux/kernel.h> /* Needed for KERN_INFO */
#include<linux/init.h> /* Needed for the macros */
#define DRIVER_AUTHOR "Sunil V Bhagwat <p@dirac.org>"
#define DRIVER_DESC "A Sample Driver"

static int __init init_Module_5(void)
{
printk(KERN_INFO "Jay shreeKrishna 3\n");
return 0;
}
static void __exit cleanup_Module_5(void)
{
printk(KERN_INFO "Jay RadheShyam 3\n");
}
module_init(init_Module_5);
module_exit(cleanup_Module_5);
/*
* You can use string, like this:
*/

/*
* Get rid of taint message by declaring code as GPL.
*/
MODULE_LICENSE("GPL");

/*
* Or with defines, like this:
*/

MODULE_AUTHOR(DRIVER_AUTHOR); /* Who wrote this module? */
MODULE_DESCRIPTION(DRIVER_DESC); /* What does this module do */

/*
* This module uses /dev/testdevice. The MODULE_SUPPORTED_DEVICE macro might
* be used in the future to help automatic configuration of modules, but is
* currently unused other than for documentation purposes.
*/
MODULE_SUPPORTED_DEVICE("testdevice"); 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

File Name :- Makefile

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

obj-m += Module5.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name :- Module6.c

Description :- This program illustrate the concept of setting access permission to users.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/moduleparam.h>
#include<linux/stat.h>

int myint = 20;

module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(myint, "integer variable");

static int __init Module_6_init(void)
{
printk(KERN_INFO "Hello Sunil %d\n", myint);
return 0;
}

static void __exit Module_6_exit(void)
{
printk(KERN_INFO "Bye sunil\n");
}

module_init(Module_6_init);
module_exit(Module_6_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Sunil Bhagwat");

MODULE_DESCRIPTION("This is my module for int variable demo");

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

File Name :- Makefile

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

obj-m += Module5.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name :- First.c

Description :- This program illustrate the concept of module split into various file. 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>

int init_module(void)
{
printk(KERN_INFO "Hello sunil bhagwat");
return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name :- Second.c

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>

void cleanup_module(void)
{
printk(KERN_INFO "Bye sunil bhagwat");
}

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Sunil Bhagwat");

MODULE_DESCRIPTION("This is my module");

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

File Name :- Makefile

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

obj-m += FirstSecond.o

FirstSecond-objs := First.o Second.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name :- Driver_1.c

Description :- This Program illustrate the concept of first Driver virtual program .

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<asm/uaccess.h>
#include<linux/device.h>

#define DEVICE_NAME "My Driver"

#define CLASS_NAME "MY DEVICE DRIVER"

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Sunil Bhagwat");

MODULE_DESCRIPTION("My Demo Device Driver");

MODULE_VERSION("0.1");

static int majorNumber;

static char message[250] = {0};

static short size_of_message;

static int numberOpens = 0;

static struct class* charClass = NULL;

static struct device* charDevice = NULL;

static int dev_open(struct inode *, struct file *);

static int dev_release(struct inode *, struct file *);

static ssize_t dev_read(struct file *, char *, size_t, loff_t *);

static ssize_t dev_write(struct file *, const char *, size_t, loff_t *);

static struct file_operations fops = 
{
.open = dev_open,
.read = dev_read,
.write = dev_write,
.release = dev_release,
};

static int __init char_init(void)
{
printk(KERN_INFO "MyDriver : Driver loaded sucessfully \n");
majorNumber = register_chrdev(0, DEVICE_NAME, &fops);

if(majorNumber < 0)
{
printk(KERN_ALERT "MyDriver : failed to register a major number \n");
return majorNumber;
}
printk(KERN_INFO "MyDriver : reistered correctly with major number %d \n", majorNumber);

charClass = class_create(THIS_MODULE, CLASS_NAME);

if(IS_ERR(charClass))
{
unregister_chrdev(majorNumber, DEVICE_NAME);
printk(KERN_ALERT "Failed to create the device \n");

return PTR_ERR(charClass);
}
printk(KERN_INFO "MyDriver : device class registered correctly \n");

charDevice = device_create(charClass, NULL, MKDEV(majorNumber, 0), NULL, DEVICE_NAME);

if(IS_ERR(charDevice))
{
class_destroy(charClass);
unregister_chrdev(majorNumber, DEVICE_NAME);
printk(KERN_ALERT  "Failed to create the device \n");
return PTR_ERR(charDevice);
}
printk(KERN_INFO "MyDriver : device class created correctly \n");
return 0;
}

static void __exit char_exit(void)
{
device_destroy(charClass, MKDEV(majorNumber, 0));
class_unregister(charClass);
class_destroy(charClass);
unregister_chrdev(majorNumber, DEVICE_NAME);
printk(KERN_INFO "MyDevice : Goodbye from our driver! \n");
}

static int dev_open(struct inode *inodep, struct file *filep)
{
numberOpens++;
printk(KERN_INFO "MyDriver : Device has been opened %d time(s) \n", numberOpens);
return 0;
}

static ssize_t dev_read(struct file *filep, char *buffer, size_t len, loff_t *offset)
{
int error_count = 0;
error_count = copy_to_user(buffer, message, size_of_message);
if(error_count == 0)
{
printk(KERN_INFO "MyDriver : Sent %d characters to the user \n", size_of_message);
return (size_of_message = 0);
}
else
{
printk(KERN_INFO "MyDriver : Failed to send %d characters to the user \n", error_count);
return -EFAULT;
}
}

static ssize_t dev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset)
{
sprintf(message, "%s(%d letters)", buffer, len);
size_of_message = strlen(message);
printk(KERN_INFO "MyDriver : Received %dcharacters from the user \n",len);
return len;
}

static int dev_release(struct inode *inodep, struct file *filep) 
{
printk(KERN_INFO "MyDriver : Device successfully closed \n");
return 0;
}


module_init(char_init);
module_exit(char_exit);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

File Name :- Makefile

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

obj-m += Driver_1.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name :- Demo.c

Description :- This is a demo program to access that virtual device.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>

#define BUFFER_LENGTH 256

static char receive[BUFFER_LENGTH];

int main()
{
int ret, fd;
char stringToSend[BUFFER_LENGTH];
printf("Starting device test code example...\n");
fd = open("/dev/My Driver", O_RDWR);
if(fd < 0)
{
perror("Failed to open the device...");
return errno;
}
printf("Type in a short string to send to the kernel module: \n ");
scanf("%[^\n]%*c",stringToSend);
printf("Writing message to he device [%s].\n",stringToSend);
ret = write(fd, stringToSend, strlen(stringToSend));

if(ret < 0)
{
perror("Failed to write the message to the device.");
return errno;
}
printf("Press Enter to read back from the device...\n");
getchar();
printf("Reading from the device...\n");
ret = read(fd, receive, BUFFER_LENGTH);
if(ret < 0)
{
perror("Failed to read the message from the device.");
return errno;
}
printf("The received message is:[%s]\n",receive);
printf("End of the program\n");
return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name :- Driver_2.c

Description :- This program illustrate the concept of registering the Driver program.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<linux/module.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/types.h>
#include<linux/fs.h>
#include<linux/kdev_t.h>

static dev_t mydev;

static char buffer[64];

MODULE_AUTHOR("Sunil Bhagwat");
MODULE_LICENSE("GPL");

static __init chardrv_in(void)
{
printk(KERN_INFO "Character driver being loaded.\n");
alloc_chrdev_region(&mydev, 0, 1, "Demo Driver");

printk(KERN_INFO "Information of our driver is %s \n", format_dev_t(buffer, mydev));
return 0;
}

static void __exit chardrv_out(void)
{
printk(KERN_INFO "Characterr driver being unloaded. \n");
unregister_chrdev_region(mydev, 1);
}

module_init(chardrv_in);
module_exit(chardrv_out);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


File Name :- Makefile

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

obj-m += Driver_2.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Tuesday, February 23, 2016

Introduction to Watch Command In Linux

*********Introduction to Watch Command In Linux***********

Watch is Linux command that allows you to execute command or program periodically also shown your output on screen.This means that we shown your command output time to time. By default watch re-execute command or program every 2 seconds.The time can  be easily change our requirement.

Watch is  extremely easy to use in terminal of Linux. We have to just open a terminal and type below command

                                      watch free -m 




The above command will check your system free memory and update result of free commands for every two seconds.

How to change periodical time we have to just use  -n option , that specifies the interval with which the command will be executed. This interval is specifies in seconds. 

We have to run script.sh file in every 10 seconds, we have to specify following command.

                              watch -n 10 script.sh

Note that, we have to run this above command, we have to cd that directory and run that command or we have to specify absolute path.

Other useful option of watch command is,

-b : Create the beep sound if the exit of command is non-zero.
-c : Interpret the ANSI colour sequence.
-d : Highlight the changes in command output.

If we want to monitor logged-In user, server uptime and load average output in continuously phase every few seconds, then use the following commands as shown,

                             watch uptime

Watch Linux Load Average


To exit this command we have to press Ctr + C

/////////////////////////////////////////////////////////////////////////////////////////////

Monday, February 22, 2016

Step to Set Path For Java Program In Windows

*************** Step to Set Path For Java Program In Windows ****************************

Step 1 : Before we have to set path from your computer, check whether path is already set or not. Open Command Prompt and type javac and java. We have to occur any error then not set a path.

Step 2 : Download and Install jdk from oracle site.

Step 3 : Go to My Computer and Open Folder " C:\Program Files\Java\jdk1.7.0_10\bin " and copy that path.







Step 4 : We have to Right click on MyComputer->properties->Advance System Security-> Environment Variable->New. We have to set application variable path, variable name and variable value is paste a copy in that text box and press OK.










Step 5 : We have also set System Path, select path variable in that below list and edit that variable. Do not delete all path in that variable. We have to edit that press semicolon and close previous path and paste your java path. click Ok.

Step 6 : We have to check path set successfully or not. open in command prompt and type javac and java.


Another way is to open Advance System Setting is

Control panel->System->Advance System Setting->Environment Variable.





/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

SYBSc (CS) Sem III : Data Structure Slip 20 Que - 2

    SAVITIBAI PHULE UNIVERSITY OF PUNE S. Y. B.Sc. (Computer Science) Semester III Practical Examination       SUBJECT: CS-233 Practical cou...