Showing posts with label Semester 5. Show all posts
Showing posts with label Semester 5. Show all posts

J.E. Hopcroft, R. Motwani and J.D. Ullman, “Introduction to Automata Theory, Languages and Computations” - Pearson Education - Theoretical foundation of Computation (TFC)




Introduction to Automata Theory, Languages and Computations
J.E. Hopcroft, R. Motwani and J.D. Ullman



Roger S. Pressman - “Software Engineering – A practitioner’s Approach” - Fifth Edition - Software Engineering (SE)




Software Engineering – A practitioner’s Approach
Roger S. Pressman





Ian Sommerville, “Software engineering”, Sixth Edition - Software Engineering (SE)



 Software Engineering
Ian Sommerville

 




Kay Robbins, Steve Robbins: UNIX Systems Programming- Communication, Concurrency and Threads - Systems Lab




UNIX Systems Programming
Kay Robbins, Steve Robbins



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:~$
 


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]>

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;
}


Related Posts Plugin for WordPress, Blogger...