Roundrobin - Scheduling - Preemptive- Systems Lab - C Program

Program:

#include<stdio.h>
#define true 1
#define false 0
int n,tq,totwt=0,tottrnd=0;
struct pr
{
    int srvst,wt;
//    int wt;
    int trndt;
    int flag;
    int temp;
}prc[10];

void printpr()
{
    printf("\nProcess_id\tServicetime\tWaitingtime\tTurnarndtime\n");int i;
    for(i=0;i<n;i++)
    {
        printf("\n%d \t\t%d \t\t%d \t\t%d\t\n",i,prc[i].srvst,prc[i].wt,prc[i].trndt);
    }
    printf("Average waiting time=%f\nAverage turnaroundtime=%f\n\n",(float)totwt/n,(float)tottrnd/n);
}

void rschedule()
{
    int trnd=0,i=0,t1;
    while(completed()==false)
    {
        if(prc[i].flag==false)
        {
            if(prc[i].temp==0||prc[i].temp<=tq)
            {  
                prc[i].flag=true;
                trnd+=prc[i].temp;
                tottrnd+=prc[i].trndt=trnd;
                prc[i].temp=0;
            }
            else
            {  
                trnd+=tq;
                prc[i].temp-=tq;
            }
        }
        i=(i+1)%n;
    }
}

int completed()
{
    int sum=0,i;
    for(i=0;i<n;i++)
    {
        if(prc[i].flag==true)
            sum++;
    }
    if(sum==n)
        return true;
        return false;
}      

main()
{
    int i;
    printf("\n\t\t<<<ROUND ROBIN SCHEDULING>>>\nEnter the timequantum: ");
    scanf("%d",&tq);  
    printf("Enter the number of processes:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        //printf("\nEnter the details for process %d:\nService time:",i);
        printf("\n Enter process %d Service time:",i);
        scanf("%d",&prc[i].srvst);
        prc[i].flag=false;
        prc[i].temp=prc[i].srvst;
    }
    prc[0].wt=0;int wtprmtr=0;
    for(i=0;i<n-1;i++)
    {
        if(prc[i].srvst<tq)
            wtprmtr+=prc[i].srvst;
        else
            wtprmtr+=tq;
        prc[i+1].wt=wtprmtr;
        totwt+=prc[i+1].wt;
    }
    rschedule();
    printpr();
}


Output:

nn@ubuntu:~$ gcc rrn.c
nn@ubuntu:~$ ./a.out

        <<<ROUND ROBIN SCHEDULING>>>
Enter the timequantum: 50
Enter the number of processes:5

 Enter process 0 Service time:350

 Enter process 1 Service time:125

 Enter process 2 Service time:475

 Enter process 3 Service time:250

 Enter process 4 Service time:75

Process_id    Servicetime    Waitingtime    Turnarndtime

0                   350                  0                     1100   

1                   125                  50                   550   

2                   475                  100                 1275   

3                   250                  150                 950   

4                     75                  200                 475   
Average waiting time=100.000000
Average turnaroundtime=870.000000
                                                                         
nn@ubuntu:~$



Operator Overloading -1 (Digit to Word) - C++

Program:
#include<iostream>
#include<string.h>
using namespace std;
class num
{
public:
    char str[50],temp[50];
    void cnvrt(int t)
    {   
    switch(t){   
        case 0:
            strcpy(str,"zero");
            break;
        case 1:
            strcpy(str,"one");
            break;
        case 2:
            strcpy(str,"two");
            break;
        case 3:
            strcpy(str,"three");
            break;
        case 4:
            strcpy(str,"four");
            break;
        case 5:
            strcpy(str,"five");
            break;
        case 6:
            strcpy(str,"six");
            break;
        case 7:
            strcpy(str,"seven");
            break;
        case 8:
            strcpy(str,"eight");
            break;
        case 9:
            strcpy(str,"nine");
            break;
            }
                         
    }
    num operator+(num stin)
    {
        num temp;
        strcpy(temp.str,str);
        strcat(temp.str,stin.str);
        return temp;
    }
};
int main()
{   
    num a[15],op;
    int i=0,j,k=0,l,n,r,ar[15];
    cout<<"Enter the number: ";
    cin>>n;
    while(n!=0)
    {
        r=n%10;
        ar[k++]=r;
        i++;
        n=n/10;
    }
    for(j=0,l=(k-1);j<i,l>=0;j++,l--)
    {
        a[j].cnvrt(ar[l]);
    }
    strcpy(op.str," ");
    for(j=0;j<i;j++)
    {
        op=op+a[j];
    }
    cout<<"\n"<<op.str<<endl;
    return 1;
}

Output:
nn@ubuntu:~$ g++ n2.cpp
nn@ubuntu:~$ ./a.out
Enter the number: 1234567890

 onetwothreefourfivesixseveneightninezero
nn@ubuntu:~$

Stack Implementation Using Template - C++

#include<iostream>
using namespace std;
template<class t>
class stack
{
public:
    t *a;
    int top,i,size;
    stack(int siz)
    {
        a=new t[siz];
        size=siz;
        top=-1;
    }
                       
    void push(t ele)
    {
        if(top!=size-1){a[++top]=ele;}
    }
               
    t pop()     
    {
          return(a[top--]);
    }
                       
    void print()
    {
        for(i=0;i<top;i++)
            cout<<" "<<a[i];
                               
        cout<<"\n";
    }
               
};
                   
int main()
{   
    int i,ele;
    float el;
    char e;
    cout<<"\n\nINTEGER STACK:\n";
    stack<int> st1(10);
    for(i=1;i<=5;i++)
    {
        cout<<"\n\tpushed item:"<<i;
        st1.push(i);
    }
    cout<<"\n\n";
    for(i=0;i<5;i++)
    {
        ele=st1.pop(); 
        cout<<"\t\npoped Item: "<<ele;
    }
    cout<<"\n\nFLOAT STACK:\n";
    stack<float> st2(10);
    for(i=1;i<=5;i++)
    {
        el=(.05*i);
        cout<<"\n\tpushed item:"<<el;
        st2.push(el);
    }
    cout<<"\n\n";
    for(i=0;i<5;i++)
    {
        el=st2.pop(); 
        cout<<"\t\npoped Item: "<<el;
    }
    cout<<"\n\nCHARACTER STACK:\n";
    stack<char> st3(10);
    for(i=0;i<5;i++)
    {
        e=65+i;
        cout<<"\n\tpushed item:"<<e;
        st3.push(65+i);
    }
    cout<<"\n\n";
    for(i=0;i<5;i++)
    {
        e=st3.pop(); 
        cout<<"\t\npoped Item: "<<e;
    }
    cout<<"\n";
    return(1);
}

Output:
nn@ubuntu:~$ g++ ct1.cpp
nn@ubuntu:~$ ./a.out


INTEGER STACK:

    pushed item:1
    pushed item:2
    pushed item:3
    pushed item:4
    pushed item:5

   
poped Item: 5   
poped Item: 4   
poped Item: 3   
poped Item: 2   
poped Item: 1

FLOAT STACK:

    pushed item:0.05
    pushed item:0.1
    pushed item:0.15
    pushed item:0.2
    pushed item:0.25

   
poped Item: 0.25   
poped Item: 0.2   
poped Item: 0.15   
poped Item: 0.1   
poped Item: 0.05

CHARACTER STACK:

    pushed item:A
    pushed item:B
    pushed item:C
    pushed item:D
    pushed item:E

   
poped Item: E   
poped Item: D   
poped Item: C   
poped Item: B   
poped Item: A
nn@ubuntu:~$
 


Operator Overloading -2 - (Digit to Word) - C++

 Program:
#include<iostream>
#include<string.h>
using namespace std;
class strin
{
    char str[50];
public:
    strin()
    {
        strcpy(str," ");
    }
    strin(char* mystr)
    {
        strcpy(str,mystr);
    }
    void operator+(strin s)
    {
        strcpy(str,s.str);
        cout<<str;
    }
};

main()
{
    strin str1,str2;
    int n,n1,dg,y=0,lastzero=0;
    cout<<"\t\tOPERATOR OVERLOADING -II (12 --> ONE TWO)\nEnter the number: ";
    cin>>n;n1=n;
    while(n>0)
    {
        dg=n%10;
       
        y=y*10+dg;
        n=n/10;
    }
    while(n1%10==0)
    {
        lastzero++;
        n1/=10;
        if(lastzero>20)
            {
                lastzero=1;
                break;
            }
    }
   
    while(y>0)
    {
        dg=y%10;
        switch(dg)
        {
            case 1: str2=(char*)" one";
                str1 + str2;
                break;
            case 2 :str2=(char*)" two";
                str1 + str2;
                break;
            case 3: str2=(char*)" three";
                str1 + str2;
                break;
            case 4 :str2=(char*)" four";
                str1 + str2;
                break;
            case 5: str2=(char*)" five";
                str1 + str2;
                break;
            case 6 :str2=(char*)" six";
                str1 + str2;
                break;
            case 7: str2=(char*)" seven";
                str1 + str2;
                break;
            case 8 :str2=(char*)" eight";
                str1 + str2;
                break;
            case 9: str2=(char*)" nine";
                str1 + str2;
                break;
            case 0 :str2=(char*)" zero";
            str1 + str2;
                break;
            default:
                cout<<"\n\nError.......exiting...";
                break;break;
        }
    y=y/10;
    }
    while(lastzero!=0)
    {
        cout<<" zero";
        lastzero--;
    }
    cout<<endl;
}


Output:
nn@ubuntu:~$ g++ op.cpp
nn@ubuntu:~$ ./a.out
        OPERATOR OVERLOADING -II (12 --> ONE TWO)
Enter the number: 1234567890
 one two three four five six seven eight nine zero
nn@ubuntu:~$

Inter Process Communication using Pipe- Fibonacci - Systems Lab - C

Program:
#include<stdio.h>
main()
{
    int pid;
    int p1[2],p2[2];
    pipe(p1);
    pipe(p2);
    int b,n,i,f1,f2;int ar[30],br[30];
    pid=fork();
    if(pid==0)
    {
        printf("enter count:");
        fflush(stdin);
        scanf("%d",&n);
        close(p1[0]);
        write(p1[1],&n,4);
       
        close(p2[1]);
        read(p2[0],br,30*sizeof(int));
        printf("\nFibonacci:\n");
        for(i=0;i<n;i++)
            printf("%d\n",br[i]);
           
    }
    else if(pid>0)
    {
    close(p1[1]);
    read(p1[0],&b,4);
    //printf("count is:%d",b);
   
    f1=0,f2=1;
    ar[0]=0;
    ar[1]=1;
    int i;
    for(i=2;i<b;i++)
    {
        int f3=f1+f2;
        f1=f2;
        f2=f3;
        ar[i]=f3;
    }
    close(p2[0]);
    write(p2[1],ar,30*sizeof(int));
    }
}


Output:
nn@ubuntu:~$ gcc pp.c
nn@ubuntu:~$ ./a.out
enter count:5
nn@ubuntu:~$
Fibonacci:
0
1
1
2
3

Inter Process Communication using Pipe - Sorting - Systems Lab

Program:
#include<stdio.h>
main()
 {
      int i,j,n,n1;
      int pid;
      int a[5],b[5],c[5];
      int p1[2],p2[2];
       pipe(p1);
      pipe(p2);
      pid=fork();
      if(pid==0)
      {
      printf("enter the limit \n");
            fflush(stdin);
           scanf("%d",&n);
          
               close(p1[0]);
        write(p1[1],&n,sizeof(int));
    close(p2[1]);
    read(p2[0],b,5*sizeof(int));
      for(i=0;i<n;i++)
        printf("%d\t",b[i]);


        }
        else
        {
         close(p1[1]);
    read(p1[0],&n1,sizeof(int));
    printf("enter the numnbers");
             for(i=0;i<n1;i++)
               scanf("%d",&a[i]);
 
      for(i=0;i<n1;i++)
      for(j=0;j<n1-i-1;j++)
       if(b[j]>b[j+1])
       {
         int t=b[j];
         b[j]=b[j+1];
         b[j+1]=t;
        }
   
    close(p2[0]);
    write(p2[1],a,5*sizeof(int));
   }
 }
   
#include<stdio.h>
main()
 {
      int i,j,n,n1;
      int pid;
      int a[5],b[5],c[5];
      int p1[2],p2[2];
       pipe(p1);
      pipe(p2);
      pid=fork();
      if(pid==0)
      {
      printf("enter the limit \n");
            fflush(stdin);
           scanf("%d",&n);
           
               close(p1[0]);
        write(p1[1],&n,sizeof(int));
    close(p2[1]);
    read(p2[0],b,5*sizeof(int));
      for(i=0;i<n;i++)
        printf("%d\t",b[i]);


        }
        else
        {
         close(p1[1]);
    read(p1[0],&n1,sizeof(int));
    printf("enter the numnbers");
             for(i=0;i<n1;i++)
               scanf("%d",&a[i]);
 
      for(i=0;i<n1;i++)
      for(j=0;j<n1-i-1;j++)
       if(b[j]>b[j+1])
       {
         int t=b[j];
         b[j]=b[j+1];
         b[j+1]=t;
        }
   
    close(p2[0]);
    write(p2[1],a,5*sizeof(int));
   }
 }


Output:
 nn@ubuntu:~$ gcc pb1.c
nn@ubuntu:~$ ./a.out
enter the limit
5
enter the numnbers5
4
3
2
1
5    4    3    2    1
nn@ubuntu:~$ gcc pb1.c

Check Palindrome - LISP

Program:
(defun palindrome()
(format t "Enter the string(in \"\"):")
(setf x (read))
(if(string= x (reverse x))
    (format t "palindrome")
    (format t "Not palindrome")
    )                                                                          
)


Output:
[1]> (load 'pal.lsp)
;; Loading file pal.lsp ...
;; Loaded file pal.lsp
T
[2]> (palindrome)
Enter the string(in ""):"malayalam"
palindrome
NIL
[3]> (palindrome)
Enter the string(in ""):"HAI"
Not palindrome
NIL
 [4]>


Towers of Hanoi - Programming Environment Lab - JAVA

Program:
import java.io.*;
class han
{
    public void phanoi(int n,int source,int temp,int destn)
    {
        if(n>0)
        {
            phanoi(n-1,source,destn,temp);
            System.out.println("move top disk from "+source+" to "+destn);
            phanoi(n-1,temp,source,destn);
        }

    }
}
class hanoi
{
public static void main (String arg[]) throws IOException
{
    DataInputStream x=new DataInputStream(System.in);
    System.out.print("enter no of disks: ");
    int n=Integer.parseInt(x.readLine());
    han ob=new han();
    ob.phanoi(n,1,2,3);
}
}
Output:
nn@ubuntu:~$ javac hanoi.java
nn@ubuntu:~$ java hanoi
enter no of disks:3
move top disk from 1 to 3
move top disk from 1 to 2
move top disk from 3 to 2
move top disk from 1 to 3
move top disk from 2 to 1
move top disk from 2 to 3
move top disk from 1 to 3
nn@ubuntu:~$

Shared Memory - String - Systems Lab - C Program


Program:

#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/wait.h>
#include<sys/types.h>
#include"string.h"
#define shmsize 100
#define shmmode (SHM_R|SHM_W|IPC_CREAT|IPC_EXCL)
#define shmkey (key_t)31
int size;
int main()
{
    int shmid1, shmid2, pid, status;
   
    int *shmdata1, *shmdata2, *shmdata;
   
    char str1[20],str2[20],str3[20];
    int i,j,k,size2,size3;
    struct shmid_ds *shmidds;
    shmid1 = shmget(shmkey,shmsize,shmmode);
    shmdata1 = (int*)shmat(shmid1,0,0);
    shmdata = shmdata1;
    printf("Enter the string:");
    scanf("%s",str1);
    size=strlen(str1);
    for(j=0;j<size;j++)
    {
        *shmdata1=str1[j];
        shmdata1=shmdata1+sizeof(char);
    }
    pid = fork();           
    if(pid == 0)
    {
        printf("Enter the string:");
        scanf("%s",str2);
        size2=strlen(str2);
        for(i=0;i<size2;i++)
        {
            *shmdata1=str2[i];
            shmdata1+=sizeof(char);
        }
        size=size+size2;
    }
    while((pid = wait(&status))!= -1);
    shmdata1=shmdata;
    for(i=0;i<size;i++)
    {
        str3[i]=*shmdata1;
        shmdata1+=sizeof(char);
    }
    str3[size]='\0';
    printf("%s\n\n\n\n\n\n",str3);
shmdt((void*)shmdata);
shmdt((void*)shmdata1);
shmctl(shmid1,IPC_RMID,shmidds);
    return 1;
}

Output:
nn@ubuntu:~$ gcc shmstr.c
nn@ubuntu:~$ ./a.out
Enter the string:hai
Enter the string:helo
haihelo





hai





nn@ubuntu:~$

Armstrong Number Check/Generation - LISP

1.To Check

Program

(defun str(n)
        (setf num n sum 0)
        (loop while (/= n 0) do
            (setf digit (mod n 10))
            (setf sum (+ sum (* (* digit digit) digit)))
            (setf n (floor n 10))
        )
        (if(= sum num) (print "Armstrong Number....")
            (print "Not a Armstrong Number....")
        )
t)

Output:

Break 20 [21]> (load 'arstr.lsp)
;;       Loading file arstr.lsp ...
;;       Loaded file arstr.lsp
T
Break 20 [21]> (str 153)

"Armstrong Number...."
T
Break 20 [21]>


2.To Generate

Program:

(defun arm(rang)
    (do((j 1 (+ j 1))) ((= j rang))
        (setf n j)
        (setf num n sum 1)
        (loop for i from n above 0 do
            (setf f1 (mod n 10))
            (setf sum (+ sum (* (* f1 f1) f1)))
            (setf n (floor n 10))
            (setf i n)
        )
        (if(= sum num) (print num))
    )
t       
)

Output:

Break 15 [16]> (load 'arm.lsp)
;;    Loading file arm.lsp ...
;;    Loaded file arm.lsp
T
Break 15 [16]> (str 157)

153
T
Break 15 [16]>

Strong number Check/Genration - LISP

1.To Check
 
Program:


(defun str(n)
        (setf num n sum 0)
        (loop while (/= n 0) do
            (setf digit (mod n 10))
            (setf sum (+ sum (fact digit)))
            (setf n (floor n 10))
        )
        (if(= sum num) (print "Strong Number....")
            (print "Not a Strong Number....")
        )
t       
)
                 
(defun fact(n)
    (if (= n 0) 1
         (* n (fact(- n 1)))
    )
)

Output:
Break 18 [19]> (load 'str1.lsp)
;;     Loading file str1.lsp ...
;;     Loaded file str1.lsp
T
Break 18 [19]> (str 145)

"Strong Number...."
T
Break 18 [19]>

2.To Generate


Program:
(defun str(nn)
    (do((j 1 (+ j 1))) ((= j nn))
        (setf n j)
        (setf num n sum 1)
        (loop for i from n above 0 do
            (setf f1 (mod n 10))
            (setf sum (+ sum (fact f1)))
            (setf n (floor n 10))
            (setf i n)
        )
        (if(= sum num) (print num))
    )
t       
)
(defun fact(n)
    (if (= n 0) 1
         (* n (fact(- n 1)))
    )
)
Output:

 Break 15 [16]> (load 'str.lsp)
;;    Loading file str.lsp ...
;;    Loaded file str.lsp
T
Break 15 [16]> (str 147)

145
T
Break 15 [16]>

Sorting Using Template - C++

Program:

Download Runnable Code

#include<iostream>
using namespace std;
template<class t>
void sort(t a[],int n)
{
    int i,j;t tem;
    for(i=0;i<n;i++)
        for(j=0;j<n-i-1;j++)
            if(a[j]<a[j+1])
            {
                tem=a[j];
                a[j]=a[j+1];
                a[j+1]=tem;
            }
    cout<<"\n\t--------After Sorting---------\n\n";
    for(i=0;i<n;i++) 
        cout<<" "<<a[i];
}
               
int main()
{
    int i,n,a[25],ch;float b[25];bool exit=false;   
    cout<<"\n\t------SORTING USING TEMPLATE-----------\n\nEnter the number of elements: ";
    cin>>n;

    do{
        cout<<"\n\t--------MENU---------\n\n\t1.Integer Sorting\n\t2.Floating point Sorting\n\t3.Exit\n\nEnter your choice:";
        cin>>ch;
        switch(ch)
        {
            case 1:
                cout<<"\n\t------INT TYPE-----------\n\n";
                cout<<"\nEnter elements:\n";
                for(i=0;i<n;i++)
                    cin>>a[i];
                sort(a,n);
                break;
           
            case 2:
                cout<<"\n\t------FLOAT TYPE-----------\n\n";
                cout<<"\nEnter elements:\n";
                for(i=0;i<n;i++)
                    cin>>b[i];
                sort(b,n);
                break;
            case 3:
               
            default:
                cout<<"Exiting..... \n\t\t\t\t\t\t...www.2k8618.blogspot.com\n";
                exit=true;
        }
    }while(!exit);
return 0;   
}

Output:

nn@ubuntu:~$ ./a.out

    ------SORTING USING TEMPLATE-----------

Enter the number of elements: 5

    --------MENU---------

    1.Integer Sorting
    2.Floating point Sorting
    3.Exit

Enter your choice:1

    ------INT TYPE-----------


Enter elements:
5
4
3
2
1

    --------After Sorting---------

 5 4 3 2 1
    --------MENU---------

    1.Integer Sorting
    2.Floating point Sorting
    3.Exit

Enter your choice:2

    ------FLOAT TYPE-----------


Enter elements:
1.9
1.2
1.0
1.4
1.7

    --------After Sorting---------

 1.9 1.7 1.4 1.2 1
    --------MENU---------

    1.Integer Sorting
    2.Floating point Sorting
    3.Exit

Enter your choice:3
Exiting.....
                                                                       ...www.2k8618.blogspot.com

Download

Check Prime/Prime Generation -LISP

Program:

(defun gen(a b)
(if (<= a 1) (setf a 2))
(do ((i a (+ i 1) )) ((= i b))
    (prm i)
    ))
(defun prm( num)
    (setf flag 1) (print num)
    (do ((i 2 (+ i 1) )) ((= i  num))
        (if (= 0 (mod num i)) (setf flag 2)
            )
    )
    (if (= flag 1) (format t "prime") (format t "not prime"))
)

Output:

Break 13 [15]> (load 'pr2.lsp)
;; Loading file pr2.lsp ...
;; Loaded file pr2.lsp
T
Break 13 [15]> (gen 1 20)

2 prime
3 prime
4 not prime
5 prime
6 not prime
7 prime
8 not prime
9 not prime
10 not prime
11 prime
12 not prime
13 prime
14 not prime
15 not prime
16 not prime
17 prime
18 not prime
19 prime
NIL
Break 13 [15]>

Download Runnable Code

String Sort - LISP

Program:

Download Runnable Code

;;BUBBLESORT (STRING)

(defun breadn(n)
(setf arr (make-array n))
(format t "Enter the strings:~&")
(dotimes (x n t)
(setf (aref arr x) (read))))


(defun bprintn(n)
(dotimes(x n t)
    (print (aref arr x))))
(defun swap(x y)
    (setf temp (aref arr x))
    (setf (aref arr x) (aref arr y))
    (setf (aref arr y) temp)
)

(defun bsortn ( n )
    (breadn n);;(bprintn n)
    (do (( i 1 (+ i 1)))
    ((= i n))
    (setf k (- n 1))
    (do((j 0 (+ j 1    )))
    ((= j k))
    (setf m (+ j 1))
    (if (string> (aref arr j) (aref arr m))
        (swap j m))
))
    (bprintn n)
)
Output:
Break 2 [4]> (load 'bbs.lsp)
;; Loading file bbs.lsp ...
;; Loaded file bbs.lsp
T
Break 2 [4]> (bsortn 5)
Enter the strings:
orange
guava
apple
banana
mango

APPLE
BANANA
GUAVA
MANGO
ORANGE
T
Break 2 [4]>

Download

Abundant Numbers - LISP

Download Runnable Program

Program:

;Abundant numbers
(defun abn(n)
        (format t "<<<GENERATING ABUNDANT NUMBERS IN THE RANGE 1 TO ~D" N)
      ;; (setf sum 1)
    (do((i 1 (+ i 1))) ((= i (+ n 1)))
            (setf sum (sumd i)    dbl (* 2 i))
             (if(> sum dbl)    (print i))
    )
   
)
 
(defun sumd(n)
    (setf m (floor n 2))(setf sum 1 )
    (do((i 2(+ i 1))) ((> i m))
        (if(= (mod n i) 0)
                (setf sum (+ sum i) )
        )
       
    )
    (setf sum (+ sum n) )
    sum
)
Output:
Break 2 [4]> (load 'abn.lsp)
;; Loading file abn.lsp ...
;; Loaded file abn.lsp
T
Break 2 [4]> (abn 50)
<<<GENERATING ABUNDANT NUMBERS IN THE RANGE 1 TO 50
12
18
20
24
30
36
40
42
48
NIL
Break 2 [4]>




Download

* Tree Generation - Lisp

Lisp programme to generate the following structure:

*
**
***
****
*****
******
Program: 
Download Runnable Code
(defun gen(n)
    (dotimes(x (+ n 1) t)
        (dotimes(y x t)  (format t "*")
        )
        (format t "~&")
    )
)
Output:
Break 2 [4]> (load 'ge.lsp)
;; Loading file ge.lsp ...
;; Loaded file ge.lsp
T
Break 2 [4]> (gen 6)
*
**
***
****
*****
******
 
Download 

Scheduling - Non-Preemptive - Systems Lab - C Program

///Scheduling Non-Preemptive -OS Lab

#include<stdio.h>
int i,j,n;float totwt,totserv;
struct pr
    {
        char name[5];
        float tservice;
        int priority;
        float twait;
        float trnd;
    };
    struct pr temp;
    struct pr pr1[20];
void init()
{
    printf("\nEnter the number of processes:");
    scanf("%d",&n);
  
    for(i=0;i<n;i++)
    {
        printf("\nEnter the details of process %d:\n1.Name: ",i+1);
        scanf("%s",pr1[i].name);
        printf("2.Service time: ");
        scanf("%f",&pr1[i].tservice);
        printf("3.Priority: ");
        scanf("%d",&pr1[i].priority);
    }
}
void prioritysort()
{
    for(i=0;i<n;i++)
    for(j=0;j<n-i-1;j++)
    {
        if(pr1[j].priority>pr1[j+1].priority)
        {
            temp=pr1[j];
            pr1[j]=pr1[j+1];
            pr1[j+1]=temp;
        }
    }
}
void sjnsort()
{
for(i=0;i<n;i++)
    for(j=0;j<n-i-1;j++)
    {
        if(pr1[j].tservice>pr1[j+1].tservice)
        {
            temp=pr1[j];
            pr1[j]=pr1[j+1];
            pr1[j+1]=temp;
        }
    }
}
void calculation()
{
pr1[0].twait=0;totwt=0;totserv=pr1[0].trnd=pr1[0].tservice;
    for(i=1;i<n;i++)
    {
        //printf("\nwait= %f",totwt);
        pr1[i].twait=pr1[i-1].twait+pr1[i-1].tservice;
        totwt+=pr1[i].twait;
        pr1[i].trnd=pr1[i].twait+pr1[i].tservice;
        totserv+=pr1[i].trnd;

    }
}
void printing()
{
    printf("Name \tPriority \tServiceTime \t WaitTime \t TurnAroundTime\n");
    for(i=0;i<n;i++)
        {
        printf("%s \t %d \t\t %f \t %f \t %f\n",pr1[i].name,pr1[i].priority,pr1[i].tservice,pr1[i].twait,pr1[i].trnd);
        }
        printf("\nAverage wait:%f \tAverage turnaround:%f \n",totwt/n,totserv/n);
}

main()
{
    int ch,exit=0;
    init();
  
    while(exit==0)
    {
    printf("\n---MENU---\n1.FIRST COME FIRST SERVED (FCFS)\n\t2.PRIORITY SCHEDULING\n\t\t3.SHORTEST JOB NEXT (SJN)\n\t\t\t5.Exit\nEnter your choice:");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1:printf("\t<<<FCFS SCHEDULING>>>\n");
            break;
        case 2:printf("\t<<<PRIORITY SCHEDULING>>>\n");
            prioritysort();
            break;
        case 3:printf("\t<<<SJN SCHEDULING>>>\n");
            sjnsort();
            break;
        case 5:    printf("\nExiting...\n");
        default: exit=1;
          
            break;
    }
    if(exit==0)
    {
        calculation();
        printing();  
    }

    }

}


Matrix Multiplication using Shared Memory- Systems Lab - C Program



//Matrix Multiplication using Shared Memory-OS Lab

#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/wait.h>
#include<sys/types.h>
#define shmsize 100
#define shmmode (SHM_R|SHM_W)
#define shmkey (key_t)31

int main() {
    int shmid1, shmid2, pid, status;
    int *shmdata1, *shmdata2, *shmdata;
    int mtx1[10][10], mtx2[10][10];
    int i,j,k,r1,r2,c1,c2;
    struct shmd_ds *shmidds;
    shmid1 = shmget(shmkey,shmsize,shmmode|IPC_CREAT|IPC_EXCL);
    shmdata1 = (int*)shmat(shmid1,0,0);
    shmdata = shmdata1;
    printf("\nShmID: %d ShmData: %d \n",shmid1,*shmdata1);
    printf("Enter the rows and columns of matrix 1:");
    scanf("%d%d",&r1,&c1);
    printf("Enter the matrix 1: \n");
    for(i=0;i<r1;++i)
        for(j=0;j<c1;++j)
            scanf("%d",&mtx1[i][j]);
    printf("\nEnter the rows and columns of matrix 2:");
    scanf("%d%d",&r2,&c2);
    printf("Enter the matrix 2: \n");   
    for(i=0;i<r2;++i)
        for(j=0;j<c2;++j)
            scanf("%d",&mtx2[i][j]);   
    printf("\n Hello,note this...");
    if(r2!=c1) {
        printf("\nCannot Multiply");
        return 0;
    }

    for(i=0;i<r1/2;i++)
        for(j=0;j<c1;j++) {
        *shmdata1 = 0;
        for(k=0;k<c1;k++)
            *shmdata1 += mtx1[i][k]*mtx2[k][j];
            shmdata1 += sizeof(int);
        }
    pid = fork();
    if(pid == 0) {
        for(i=r1/2;i<r1;i++)
            for(j=0;j<c2;j++) {
                *shmdata1 = 0;
                for(k=0;k<c1;k++)
                    *shmdata1 += mtx1[i][k]*mtx2[k][j];
                    shmdata1 += sizeof(int);
            }       
    }
    while((pid = wait(&status))!= -1);
    shmdata1 = shmdata;
    printf("\n\n\nResult from %d\n", getpid());
    for(i=0;i<r1;++i) {
        printf("\n    ");
        for(j=0;j<c2;j++,shmdata1+=sizeof(int))
            printf("%d ",*shmdata1);
    }
    shmdt((void*)shmdata1);
    shmdt((void*)shmdata2);
    shmctl(shmid1,IPC_RMID,shmidds);
    //shmctl(shmid2,IPC_RMID,shmidds);
    return 1;
}


Readers Writers Problem - Semaphore - Systems Lab - C Program


//Readers Writers Problem -OS Lab

Program:
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>

sem_t mutex,writeblock;
int data = 0,rcount = 0;

void *reader(void *arg)
{
  int f;
  f = ((int)arg);
  sem_wait(&mutex);
  rcount = rcount + 1;
  if(rcount==1)
   sem_wait(&writeblock);
  sem_post(&mutex);
  printf("Data read by the reader%d is %d\n",f,data);
  sleep(1);
  sem_wait(&mutex);
  rcount = rcount - 1;
  if(rcount==0)
   sem_post(&writeblock);
  sem_post(&mutex);
}

void *writer(void *arg)
{
  int f;
  f = ((int) arg);
  sem_wait(&writeblock);
  data++;
  printf("Data writen by the writer%d is %d\n",f,data);
  sleep(1);
  sem_post(&writeblock);
}

main()
{
  int i,b; 
  pthread_t rtid[5],wtid[5];
  sem_init(&mutex,0,1);
  sem_init(&writeblock,0,1);
  for(i=0;i<=2;i++)
  {
    pthread_create(&wtid[i],NULL,writer,(void *)i);
    pthread_create(&rtid[i],NULL,reader,(void *)i);
  }
  for(i=0;i<=2;i++)
  {
    pthread_join(wtid[i],NULL);
    pthread_join(rtid[i],NULL);
  }
}



Producer-Consumer Problem - Semaphore - Systems Lab - C Program



//Producer Consumer Problem -OS Lab

Program:

#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
int buf[5],f,r;
sem_t mutex,full,empty;
void *produce(void *arg)
{
    int i;
    for(i=0;i<10;i++)
    {
        sem_wait(&empty);
        sem_wait(&mutex);
        printf("produced item is %d\n",i);
        buf[(++r)%5]=i;
        sleep(1);
        sem_post(&mutex);
                sem_post(&full);
        printf("full %u\n",full);
    }
}
void *consume(void *arg)
{
        int item,i;
        for(i=0;i<10;i++)
        {
                sem_wait(&full);
        printf("full %u\n",full);
                sem_wait(&mutex);
                item=buf[(++f)%5];
                printf("consumed item is %d\n",item);
                sleep(1);
                sem_post(&mutex);
                sem_post(&empty);
        }
}
main()
{
    pthread_t tid1,tid2;
    sem_init(&mutex,0,1);
    sem_init(&full,0,1);
    sem_init(&empty,0,5);
    pthread_create(&tid1,NULL,produce,NULL);
        pthread_create(&tid2,NULL,consume,NULL);
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
}



Dining Philosophers Problem - Systems Lab - C Program


//Dining Philosophers

Program:

#include<stdio.h>
#include<semaphore.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<pthread.h>
struct{
        int data[5];
        sem_t *mutex[5];
    }sh;
char *name[]={"p1","p2","p3","p4","p5"};
void *work(void *);
main()
{
        pthread_t tid[5];
        int i;
        for(i=0;i<5;i++)
        {
            sh.mutex[i]=sem_open(name[i],O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP,1);//
        }
        pthread_setconcurrency(6);
        setbuf(stdout,NULL);
        for(i=0;i<5;i++)
            pthread_create(&tid[i],NULL,work,&i);
        for(i=0;i<5;i++)
            pthread_join(tid[i],NULL);
        for(i=0;i<5;i++)
            sem_unlink(name[i]);
}
void *work(void *arg)
{
    int p,i=1;
    p=*((int *)arg);
    while(i++<2)
    {
        sem_wait(sh.mutex[p]);
        sem_wait(sh.mutex[(p+1)%5]);
        sh.data[p]=1;
        printf("p%d[%d %d %d %d %d]\n",p+1,sh.data[0],sh.data[1],sh.data[2],sh.data[3],sh.data[4]);
        sleep(1);//eating
        sh.data[p]=0;
        sem_post(sh.mutex[p]);
        sem_post(sh.mutex[(p+1)%5]);
        sleep(2);//thinking
    }
}








Bankers Algorithm- Systems Lab - C Program


//Bankers Algorithm

Program :

#include<stdio.h>
#define true 1
#define false 0
int m,n,i,j,count=0,process;
int max[10][10],alloc[10][10],need[10][10],c[10],avail[10],finish[10];
void readtable(int t[10][10])
{
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            scanf("%d",&t[i][j]);   
}
void printtable(int t[10][10])
{
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
            printf("\t%d",t[i][j]);   
        printf("\n");
    }
}
void readvector(int v[10])
{
    for(j=0;j<n;j++)
        scanf("%d",&v[j]);   
}
void printvector(int v[10])
{
    for(j=0;j<n;j++)
        printf("\t%d",v[j]);   
}
void init()
{
    printf("enter the number of process\n");
    scanf("%d",&m);
    printf("enter the number of resources\n");
    scanf("%d",&n);
    printf("enter the claim table\n");
    readtable(max);
    printf("enter the allocation table\n");
    readtable(alloc);
    printf("enter the max units of each resource\n");
    readvector(c);
    for(i=0;i<n;i++)
        finish[i]=false;
}

void findavail()
{
    int sum;
    for(j=0;j<n;j++)
    {
        sum=0;
        for(i=0;i<m;i++)   
        {
            sum=sum+alloc[i][j];
        }
        avail[j]=c[j]-sum;
    }
}
void findneed()
{
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)   
        {
            need[i][j]=max[i][j]-alloc[i][j];
        }
       
    }
}
void selectprocess()
{
    int flag;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)   
        {
            if(need[i][j]<=avail[j])
                flag=1;
            else
            {
                flag=0;       
                break;
            }
        }
        if((flag==1)&&(finish[i]==false))
        {
            process=i;
            count++;
            break;
        }
    }
    printf("current status is\n");
    printtable(alloc);
    if(flag==0)
    {
        printf("system is in unsafe state\n");
        exit(1);
    }
    printf("system is in safe state");
}
void executeprocess(int p)
{
    printf("excuting process is %d",p);
    printtable(alloc);
}
void releaseresource()
{
    for(j=0;j<n;j++)
        avail[j]=avail[j]+alloc[process][j];
    for(j=0;j<n;j++)
    {
        alloc[process][j]=0;
        need[process][j]=0;
    }
}

main()
{
    init();
    findavail();
    findneed();
    do
    {
        selectprocess();
        finish[process]=true;
        executeprocess(process);
        releaseresource();
    }while(count<m);
    printf("\n all proces executed correctly");
}





Producer Consumer Problem - JAVA

//PRODUCER CONSUMER PROBLEM
class Buffer
{
private int data;
private boolean available=false;
public synchronized int get()
{
while(available==false)
{
try
{
wait();
}
catch ( InterruptedException e)
{}
}
available=false;
notifyAll();
return data;
}
public synchronized void put(int value )
{
while(available==true)
{
try
{
wait();
}
catch ( InterruptedException e)
{}
}
data=value;
available=true;
notifyAll();
}
}
class Producer extends Thread
{
private Buffer buf;
public Producer (Buffer c)
{
buf=c;
}
public void run()
{
for(int i=0;i<5;i++)
{
buf.put(i);
System.out.println("Producer produced item:"+i);
try
{
sleep( (int) (Math.random()*100));
}
catch (InterruptedException e) { }
}
}
}
class Consumer extends Thread
{
private Buffer buf;
public Consumer (Buffer c)
{
buf=c;
}
public void run()
{
int value=0;
for(int i=0;i<5;i++)
{
value=buf.get();
System.out.println("Consumer consumed item:"+value);
try
{
sleep( (int) (Math.random()*100));
}
catch (InterruptedException e) { }
}
}
}
public class prodcons
{
public static void main (String args[])
{
Buffer b=new Buffer();
Producer p=new Producer(b);
Consumer c=new Consumer(b);
p.start();
c.start();
}
}
OUTPUT
students@ccflab-desktop:~$ javac prodcons.java
students@ccflab-desktop:~$ java prodcons
Producer produced item:0
Consumer consumed item:0
Producer produced item:1
Consumer consumed item:1
Producer produced item:2
Consumer consumed item:2
Producer produced item:3
Consumer consumed item:3
Producer produced item:4                                      //http://2k8618.blogspot.com
Consumer consumed item:4
students@ccflab-desktop:~$

Download

Interface Example - JAVA

        // INTERFACE PROGRAM IN JAVA

import java.io.*;
interface figure
{
    void read(int r);
    void display();
    void area();
}
    
class Circle implements figure
{
    double area;
    int radius;
    public void read( int r)
    {
        radius=r;
    }
    public void area()
    {
         area=3.14*radius*radius;
    }
    public void display()
    {
         System.out.print(area);
    }
    
}
class intrfce
{
    public static void main(String args[]) throws IOException
    {
        DataInputStream x= new DataInputStream(System.in);
        Circle circle=new Circle();
        System.out.print("Enter the radius of the circle:");
        circle.read(Integer.parseInt(x.readLine()));
        circle.area();
        System.out.print("Area of circle=");
        circle.display();
        
    }
}
 OUTPUT
students@ccflab-desktop:~$ javac intrfce.java
Note: intrfce.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
students@ccflab-desktop:~$ java intrfce
<<JAVA INTERFACE>>
Enter the radius of the circle:2
Area of circle=12.56                                            http://2k8618.blogspot.com
students@ccflab-desktop:~$

Download 

LISP PROGRAMS -S5

Bank Software - C++

#include<iostream>
#include<string>

using namespace std;
class account
 { struct bank{
    string name;
    float bal;
    int ac;
    }ba[10];
   
    int m;
    int n,t,q,e;
    float k,amo;
  
   public :
   
  
   int creat(int x,int t)
     { n=x;
       cout<<"\nEnter name:";
       cin>>ba[t].name;
       cout<<"\nYour Account number is :"<<n;
       ba[t].ac=n;
       cout<<"\nSelect account type\n1.SAVINGS\n2.CURRENT";
       cin>>m;
       cout<<"\nSuccessfully created an account!\n";
       a: cout<<"\ndeposite initial amount:";
       cin>>k;
       if(k<500)
       {
        cout<<"\n\nMinimum amount should be 500.Please re-deposite";
        goto a;
        }
       
       else
       { ba[t].bal=k;
        cout<<"\nBalance is:Rs."<<ba[t].bal;
        }
        return n;
      }
     void deposite()
      {
        cout<<"\nEnter account number";
           cin>>q;
           if(q<100)
            cout<<"\nInvalid account number";
           else
           {
           for(e=1;e<=10;++e)
             {
               if(ba[e].ac==q)
                break;
                }
        cout<<"\nEnter the amount to deposite:";
        cin>>amo;
        ba[e].bal=ba[e].bal+amo;
        }
       }
      void withdraw()
       {
          cout<<"\nEnter account number";
           cin>>q;
           if(q<100)
            cout<<"\nInvalid account number";
           else
           {
           for(e=1;e<=10;++e)
             {
               if(ba[e].ac==q)
                break;
                }
        
        
         cout<<"\nEnter the amount to withdraw";
         cin>>amo;
       
         if((ba[e].bal-amo)<500)
          {
           cout<<"Sorry!minimum amount should be 500.Please retry";
           }
          else
            ba[e].bal=ba[e].bal-amo;
             cout<<"\nBalance is:Rs."<<ba[e].bal;
           }
        }
        void display()
         { cout<<"\nEnter account number";
           cin>>q;
           if(q<100)
            cout<<"\nInvalid account number";
           else
           {
            for(e=1;e<=10;++e)
             {
               if(ba[e].ac==q)
                break;
                }
           cout<<"\nName is:"<<ba[e].name;
           cout<<"\nNew Balance is:"<<ba[e].bal;
           }
         }
          };
      int main()
       { int i=100,j,z,h=1;
         account ob1;
         int ch,k=1;
         while(k==1)
         {
          cout<<"\nMENU\n\n1.TO CREATE ACCOUNT\n2.TO CHECK BALANCE\n3.TO DEPOSITE\n4.TO WITHDRAW\n5.EXIT\nENTER YOUR CHOICE:";
          cin>>ch;
          switch(ch)
             {
                 case 1:
                        cout<<"Enter details of customer:\n";
                        j=ob1.creat(i,h);
                        i++;h++;
                       
                        break;
                case 2:ob1.display();
                         break;
                  case 3: ob1.deposite();
                          break;
                   case 4:ob1.withdraw();
                          break;
                    case 5:k=0;
                           break;
                }
             }
             return 0;
          }
                        
Output:
students@cselab-desktop:~$ g++ ban.cpp
students@cselab-desktop:~$ ./a.out

MENU

1.TO CREATE ACCOUNT
2.TO CHECK BALANCE
3.TO DEPOSITE
4.TO WITHDRAW
5.EXIT
ENTER YOUR CHOICE:1
Enter details of customer:

Enter name:hiran

Your Account number is :100
Select account type
1.SAVINGS
2.CURRENT1

Successfully created an account!

deposite initial amount:100


Minimum amount should be 500.Please re-deposite
deposite initial amount:500

Balance is:Rs.500
MENU

1.TO CREATE ACCOUNT
2.TO CHECK BALANCE
3.TO DEPOSITE
4.TO WITHDRAW
5.EXIT
ENTER YOUR CHOICE:2

Enter account number100

Name is:hi
New Balance is:500
MENU

1.TO CREATE ACCOUNT
2.TO CHECK BALANCE
3.TO DEPOSITE
4.TO WITHDRAW
5.EXIT
ENTER YOUR CHOICE:4

Enter account number100

Enter the amount to withdraw100
Sorry!minimum amount should be 500.Please retry
Balance is:Rs.500
MENU

1.TO CREATE ACCOUNT
2.TO CHECK BALANCE
3.TO DEPOSITE
4.TO WITHDRAW
5.EXIT
ENTER YOUR CHOICE:5
students@cselab-desktop:~$

Vector - C++

Program:

#include<iostream>
using namespace std;
class vector
 {
   int a[20],i,j,k,n,s,p;
   public:
    void creat();
    void modify();
    void mult();
    void display();
  };
  void vector::creat()
   {
     cout<<"\nEnter number of elements:";
     cin>>n;
     cout<<"\nEnter elements:";
     for(i=1;i<=n;++i)
      cin>>a[i];
    }
   void vector::display()
    {
      cout<<"\nVector is:";
      for(i=1;i<=n;++i)
       cout<<a[i]<<"\t";
     }
    void vector::mult()
     {
      cout<<"\nEnter the scalar to be multiplied:";
      cin>>s;
      for(i=1;i<=n;++i)
       a[i]=a[i]*s;
      cout<<"\nVector after multiplication";
        display();
      }
      void vector::modify()
      {
        cout<<"\nEnter no. to be inserted:";
        cin>>j;
        cout<<"\n Enter position to insert:";
        cin>>p;
        for(i=n;i>=p;--i)
        
           a[i+1]=a[i];
           a[i+1]=j;
           n++;
       }
        int main()
       {
         vector ob1;
         int ch,k=1;
         while(k==1)
         {
          cout<<"\nMENU\n\n1.TO CREATE VECTOR\n2.TO DISPLAY\n3.TO MULTIPLY BY A SCALAR\n4.TO INSERT TO A POSITION\n5.EXIT\nENTER YOUR CHOICE";
          cin>>ch;
          switch(ch)
             {
                 case 1:ob1.creat();
                        break;
                  case 2:ob1.display();
                         break;
                  case 3: ob1.mult();
                          break;
                   case 4:ob1.modify();
                          break;
                    case 5:k=0;
                           break;
                }
             }
             return 0;
          }
Related Posts Plugin for WordPress, Blogger...