OPERATING SYSTEM LAB RECORDS | B.TECH / B.E. CSE LAB PROGRAMS | LINUX PROGRAMS



OPERATING SYSTEM LAB RECORDS 




1. Basic Linux Commands (Not Uploaded)
2. Searching a SUBSTRING IN A GIVEN TEXT
3. MENU BASED MATH CALCULATOR
4. PRINTING PATTERN USING LOOP STATEMENT
5. CONVERTING FILES NAMES FROM UPPERCASE TO LOWERCASE
6. MANIPULATING DATE/TIIME/CALENDAR
7. SHOWING VARIOUS SYSTEM INFORMATION
8.(a): FCFS SCHEDULING:
8.(b): SJF
8.(c): PRIORITY SCHEDULING.
9. READER-WRITER
10. DINING-PHILOSOPHER
11.  FIRST FIT , WORST FIT, BEST FIT ALLOCATION STRATEGY
12. BANKERS ALGORITHM
13. IMPLEMENT THE PRODUCER CONSUMER PROBLEM USING SEMAPHORE
14. MEMORY MANAGEMENT POLICY PAGING




EXP NO:-2 

EXP NAME:-SEARCHING A SUBSTRING IN A GIVEN TEXT


AIM:-

 To write a program for searching a substring in a given text by using shell 

programming.


ALGORITHM:-

1) To select a substring from string using ${string: starting position :root 

position}

2) Comparing two strings is done by {$s1=$s2}

3) To check for zero length string use [-z String]

4) To check for empty string use [String]

5) To check for non zero length string use –n as [-n $string]

6) The length of string is obtained by ${#string}


PROGRAM:-


echo "Enter The String: "

read str

echo "Enter the substring: "

read substr

prefix=${str%%$substr*}

index=${#prefix}

 if [[ index -eq ${#str} ]];

then

    echo "Substring is not present in string."

else

    echo "Index of substring in string : $index"

fi




EXP NO:-3 

EXP NAME:-MENU BASED MATH CALCULATOR


AIM:-

To write a program for menu based math calculator using shell scripting 

commands.


ALGORITHM:-

1) Read the operator 

2) Read the operands

3) Using the operator as choice for switch case write case for operators

4) End switch case 

5) Stop


PROGRAM:-


echo “ Menu Based Calculator”

echo "Enter the Operands"

read a

read b

echo "Enter the Operator"

read o

case $o in

"+" ) echo “$a + $b” = `expr $a + $b`;;

"-" ) echo “$a + $b” =  `expr $a - $b`;;

"*" ) echo “$a + $b” =   `expr $a \* $b`;;

"/" ) echo  “$a + $b” =  `expr $a / $b`;;

* ) echo " Inavlid Operation"

esac




EXP NO:-4

EXP NAME:-PRINTING PATTERN USING LOOP STATEMENT


AIM:-

 To print a pattern using loop statement by using shell scripting commands.


ALGORITHM:-

1) Read the number given.

2) Initialize the for loop where i<=$n.

3) Initiallize one more loop inside the above loop with j<=$i.

4) Print “*” and close the two loops.

5) Continue until the required root loops(rows) reached.


PROGRAM:-


echo Enter the limit

read n

for((i=1; i<=n; i++))

do

  for((j=1; j<=i; j++))

  do

    echo -n "* "

  done

  echo

done


EXP NO:-5 

EXP NAME:-CONVERTING FILES NAMES FROM UPPERCASE TO LOWERCASE


AIM:-

To write a program for converting files from uppercase case to lowercase.


ALGORITHM:-

 1) Get the file name.

 2) store the name in a variable.

3) Apply conversion to that variable.

 4) Store it in other variable.

 5) Finally display the converted file name.


PROGRAM:-


echo "Hello World";

for i in *

do

echo Before Converting to uppercase the filename is

echo $i

j=`echo $i | tr '[a-z]' '[A-Z]'`

echo After Converting to uppercase the filename is

echo $j

mv $i $j

done


EXP NO:-6 

EXP NAME:-MANIPULATING DATE/TIIME/CALENDAR


AIM:-

To write a program for manipulating the date/time/calendar using shell 

commands


ALGORITHM:-

1) To display login name logname variable is used.

2) Display userinfo using variable who I am.

3) Display date(present) using variable date.

4) Display current directory using pwd variable.


PROGRAM:-


echo “hello,$LOGNAME”

echo “user is , ‘who I am’”

echo “date is,’date’”

echo “current directory ,$(pwd)”



EXP NO:-7 

EXP NAME:-SHOWING VARIOUS SYSTEM INFORMATION


AIM:-

To write a program in vi editor to show various information using shell 

command


ALGORITHM:-

1. Get the system information such as network name and node name, kernel name, 

kernel version e.t.c .

2 Network $node name=$(uname –n).

 Kernel name=$(uname –s)

Kernel ru=$(uname –a).

Operating system =$(uname –m).

All information $(uname –A).


PROGRAM:-


echo "SYSTEM INFORMATION"

echo “Hello ,$LOGNAME”

echo “Current Date is = $(date)”

echo “User is ‘who I am’”

echo “Current Directory = $(pwd)”

echo "Network Name and Node Name = $(uname -n)"

echo "Kernal Name =$(uname -s)"

echo "Kernal Version=$(uname -v)"

echo "Kernal Release =$(uname -r)"

echo "Kernal OS =$(uname -o)"

echo “Proessor Type = $(uname -p)”

echo “Kernel Machine Information = $(uname –m)”

echo "All Information =$(uname -a)"


 

OUTPUT:-

NETWORK=mobility

Kernel NAME=Linux

Kernel version =#1 SMP Thu May 6 18:27:11 UTC 2010

Operating system = i686

All information = Linux mobility 2.6.33.3-85.fc13.i686.PAE #1 SMP Thu May 6 

18:27:11 UTC 2010 i686 i686 i386 GNU/Linux




EXP NO: 8 

EXP NAME:IMPLEMENTATION OF PROCESS SCHEDULING MECHANISM – FCFS, SJF,

 PRIORITY QUEUE. 


AIM:


Write a C program to implement the various process scheduling mechanisms such 

as FCFS, SJF, Priority .

8.(A) FCFS SCHEDULING:


ALGORITHM FOR FCFS SCHEDULING:


Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the CPU 

burst time

Step 4: Set the waiting of the first process as ‘0’ and its burst time as its turn around 

time 

Step 5: for each process in the Ready Q calculate

(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of 

process(n-1)

(b) Turn around time for Process(n)= waiting time of Process(n)+ Burst time 

for process(n)

Step 6: Calculate 

(a) Average waiting time = Total waiting Time / Number of process

(b) Average Turnaround time = Total Turnaround Time / Number of process

Step 7: Stop the process


PROGRAM:


#include<stdio.h>

#include<conio.h>

void main()

{

int nop,wt[10],twt,tat[10],ttat,i,j,bt[10],t;

float awt,atat;

awt=0.0;

atat=0.0;

printf("Enter the no.of process:");

scanf("%d",&nop);

for(i=0;i<nop;i++)

{

printf("Enter the burst time for process %d: ", i);

scanf("%d",&bt[i]);

}

wt[0]=0;

tat[0]=bt[0];

twt=wt[0];

ttat=tat[0];

for(i=1;i<nop;i++){

wt[i]=wt[i-1]+bt[i-1];

tat[i]=wt[i]+bt[i];

twt+=wt[i];

ttat+=tat[i];}

awt=(float)twt/nop;

atat=(float)ttat/nop;

printf("\nProcessid\tBurstTime\tWaitingTime\tTurnaroundTime\n");

for(i=0;i<nop;i++)

printf("%d\t\t%d\t\t%d\t\t%d\n",i,bt[i],wt[i],tat[i]);

printf("\nTotal Waiting Time:%d\n",twt);

printf("\nTotal Around Time:%d\n",ttat);

printf("\nAverage Waiting Time:%f\n",awt);

printf("\nAverage Total Around Time:%f\n",atat);

getch();}



OUTPUT:

Enter total number of processes:2

Enter process burst time

p[1]:42

p[2]: 95

Process Burst time Waiting time Turnaround time

p[1] 42 0 42

p[2] 95 42 137

Average waiting time: 21

Average turnaround time: 89


8. (B) SJF


ALGORITHM FOR SJF:


Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the CPU 

burst time

Step 4: Start the Ready Q according the shortest Burst time by sorting according to 

lowest to highest burst time.

Step 5: Set the waiting time of the first process as ‘0’ and its turnaround time as its 

burst time.

Step 6: For each process in the ready queue, calculate 

(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of 

process(n-1)

(b) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for 

process(n)

Step 7: Calculate 

(a) Average waiting time = Total waiting Time / Number of process

(b) Average Turnaround time = Total Turnaround Time / Number of process

Step 8: Stop the process


PROGRAM:


#include<stdio.h>

#include<conio.h>

void main(){

int nop,wt[10],twt,tat[10],ttat,i,j,bt[10],t;

float awt,atat;


awt=0.0;

atat=0.0;

printf("Enter the no.of process:");

scanf("%d",&nop);

for(i=0;i<nop;i++){

printf("Enter the burst time for process %d: ", i);

scanf("%d",&bt[i]);}

for(i=0;i<nop;i++){

for(j=i+1;j<nop;j++){

if(bt[i]>=bt[j]){

t=bt[i];

bt[i]=bt[j];

bt[j]=t;}}}

wt[0]=0;

tat[0]=bt[0];

twt=wt[0];

ttat=tat[0];

for(i=1;i<nop;i++){

wt[i]=wt[i-1]+bt[i-1];

tat[i]=wt[i]+bt[i];

twt+=wt[i];

ttat+=tat[i];}

awt=(float)twt/nop;

atat=(float)ttat/nop;

printf("\nProcessid\tBurstTime\tWaitingTime\tTurnaroundTime\n");

for(i=0;i<nop;i++)

printf("%d\t\t%d\t\t%d\t\t%d\n",i,bt[i],wt[i],tat[i]);

printf("\nTotal Waiting Time:%d\n",twt);

printf("\nTotal Around Time:%d\n",ttat);

printf("\nAverage Waiting Time:%f\n",awt);

printf("\nAverage Total Around Time:%f\n",atat);

getch();}



8. (C).PRIORITY SCHEDULING.


ALGORITHM FOR PRIORITY SCHEDULING.

Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the CPU 

burst time

Step 4: Sort the ready queue according to the priority number.

Step 5: Set the waiting of the first process as ‘0’ and its burst time as its turn around 

time 

Step 6: For each process in the Ready Q calculate

(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of 

process(n-1)

(b) Turn around time for Process(n)= waiting time of Process(n)+ Burst time 

for process(n)

Step 7: Calculate 

(a) Average waiting time = Total waiting Time / Number of process

(b) Average Turnaround time = Total Turnaround Time / Number of process

Step 8: Stop the process


PROGRAM:


#include <stdio.h> 

void

swap (int *a, int *b)

{

  int temp = *a;

  *a = *b;

  *b = temp;

} int


main ()

{

  int n;

  printf ("Enter Number of Processes: ");

  scanf ("%d", &n);

  int b[n], p[n], index[n];

  for (int i = 0; i < n; i++)

    {

      printf ("Enter Burst Time and Priority Value for Process %d: ", i + 1);

      scanf ("%d %d", &b[i], &p[i]);

      index[i] = i + 1;

  } for (int i = 0; i < n; i++)

    {

      int a = p[i], m = i;

      for (int j = i; j < n; j++)

{

  if (p[j] > a)

    {

      a = p[j];

      m = j;

    }

}

      swap (&p[i], &p[m]);

      swap (&b[i], 

&b[m]);

      

swap (&index[i], &index[m]);

    }

  int t = 0;

  

printf ("Order of process Execution is\n");

  for (int i = 0; i < n; i++)

    {

      printf ("P%d is executed from %d to %d\n", index[i], t, t + b[i]);

      t += b[i];

    } printf ("\n");

  printf ("P.Id B.Time W.Time TA Time\n");

  int wait_time = 0;

  for (int i = 0; i < n; i++)

    {

      printf ("P%d     %d     %d     %d\n", index[i], b[i], 

wait_time, wait_time + b[i]);

      wait_time += b[i];

    } return 0;

}



EXP NO:9 


EXP NAME:READER-WRITERS PROBLEM


AIM:

To write a program to implement readers and writers problem


ALGORITHM:

Start ;

/* Initialize semaphore variables*/

integer mutex=1; // Controls access to RC

integer DB=1; // controls access to data base

integer RC=0; // Number of process reading the database currently

1.Reader( ) // The algorithm for readers process

Repeat continuously

DOWN(mutex); // Lock the counter RC

RC=RC+1; // one more reader

If(RC=1)DOWN(DB); // This is the first reader.Lock the database for reading

UP(mutex); // Release exclusive access to RC

Read database(); // Read the database

DOWN(mutex); // Lock the counter RC

RC=RC-1; // Reader count less by one now

If(RC=0)UP(DB); // This is the last reader .Unlock the database.

UP(mutex); // Release exclusive access to RC

End

2.Writer( ) // The algorithm for Writers process

Reepeat continuously

DOWN(DB); // Lock the database

Write_Database(); // Read the database

UP(DB); // Release exclusive access to the database

End

Step a: initialize two semaphore mutex=1 and db=1 and rc,(Mutex controls the 

access to read count rc)

Step b: create two threads one as Reader() another as Writer()

Reader Process: 

Step 1: Get exclusive access to rc(lock Mutex)

Step 2: Increment rc by 1

Step 3: Get the exclusive access bd(lock bd)

Step 4: Release exclusive access to rc(unlock Mutex)

Step 5: Release exclusive access to rc(unlock Mutex)

Step 6: Read the data from database Step 7: Get the exclusive access to 

rc(lock mutex)

Step 8: Decrement rc by 1, if rc =0 this is the last reader.

Step 9: Release exclusive access to database(unlock mutex)

Step 10: Release exclusive access to rc(unlock mutex)


PROGRAM:

#include <pthread.h>

#include <semaphore.h> 

#include <stdio.h> 

sem_t wrt;pthread_mutex_t mutex;int cnt = 1; 

int numreader = 0;

void *

writer (void *wno)

{

  sem_wait (&wrt);

  cnt = cnt * 2;

  

printf ("Writer %d modified cnt to %d\n", (*((int *) wno)), cnt);

  sem_post (&wrt);

} void *


reader (void *rno)

{

  pthread_mutex_lock (&mutex);

  numreader++;

  if (numreader == 1)

    {

      sem_wait (&wrt);

    }

  pthread_mutex_unlock (&mutex);

  

printf ("Reader %d: read cnt as %d\n", *((int *) rno), cnt);

  pthread_mutex_lock (&mutex);

  numreader--;

  if (numreader == 0)

    {

      sem_post (&wrt);

    }

  pthread_mutex_unlock (&mutex);

}


int

main ()

{

  pthread_t read[10], write[5];

  pthread_mutex_init (&mutex, NULL);

  sem_init (&wrt, 0, 1);

  

int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

  for (int i = 0; i < 10; i++)

    {

      pthread_create (&read[i], NULL, (void *) reader, (void *) &a[i]);

  } for (int i = 0; i < 5; i++)

    {

      

pthread_create (&write[i], NULL, (void *) writer, (void *) &a[i]);

  } for (int i = 0; i < 10; i++)

    {

      pthread_join (read[i], NULL);

  } for (int i = 0; i < 5; i++)

    {

      pthread_join (write[i], NULL);

    } pthread_mutex_destroy (&mutex);

  sem_destroy (&wrt);

  return 0;

}



EXP NO: 10 DATE:

EXP NAME: DINING PHILOSOPHERS PROBLEM


AIM: 

 Write a program to solve the Dining Philosophers problem.


ALGORITHM:

1. Initialize the state array S as 0, Si =0 if the philosopher i is thinking or 1 if 

hungry.

2. Associate two functions getfork(i) and putfork(i) for each philosopher i.

3. For each philosopher I call getfork(i) , test(i) and putfork(i) if i is 0

4. Stop

Algorithm for getfork(i):

Step 1: set S[i]= 1 i.e. the philosopher i is hungry

Step 2: call test(i)

Algorithm for putfork(i)

Step 1: set S[i]=0 I.e. the philosopher i is thinking

Step 2: test(LEFT) and test(RIGHT)

Algorithm for test(i)

Step 1: check if (state[i]==HUNGRY && state[LEFT]!=EATING && 

state[RIGHT]!=EATING)

Step 2: give the i philosopher a chance to eat.



PROGRAM

#include <pthread.h>

#include <semaphore.h>

#include <stdio.h>


#define N 5

#define THINKING 2

#define HUNGRY 1

#define EATING 0

#define LEFT (phnum + 4) % N

#define RIGHT (phnum + 1) % N


int state[N];

int phil[N] = { 0, 1, 2, 3, 4 };


sem_t mutex;

sem_t S[N];


void test(int phnum)

{

if (state[phnum] == HUNGRY

&& state[LEFT] != EATING

&& state[RIGHT] != EATING) {

state[phnum] = EATING;


sleep(2);


printf("Philosopher %d takes fork %d and %d\n",

phnum + 1, LEFT + 1, phnum + 1);


printf("Philosopher %d is Eating\n", phnum + 1);


sem_post(&S[phnum]);

}

}



void take_fork(int phnum)

{


sem_wait(&mutex);



state[phnum] = HUNGRY;


printf("Philosopher %d is Hungry\n", phnum + 1);



test(phnum);


sem_post(&mutex);



sem_wait(&S[phnum]);


sleep(1);

}



void put_fork(int phnum)

{


sem_wait(&mutex);


state[phnum] = THINKING;


printf("Philosopher %d putting fork %d and %d down\n",

phnum + 1, LEFT + 1, phnum + 1);

printf("Philosopher %d is thinking\n", phnum + 1);


test(LEFT);

test(RIGHT);


sem_post(&mutex);

}


void* philosopher(void* num)

{


while (1) {


int* i = num;


sleep(1);


take_fork(*i);


sleep(0);


put_fork(*i);

}

}


int main()

{


int i;

pthread_t thread_id[N];



sem_init(&mutex, 0, 1);


for (i = 0; i < N; i++)


sem_init(&S[i], 0, 0);


for (i = 0; i < N; i++) {

pthread_create(&thread_id[i], NULL,

philosopher, &phil[i]);


printf("Philosopher %d is thinking\n", i + 1);

}


for (i = 0; i < N; i++)


pthread_join(thread_id[i], NULL);

}

OUTPUT:


DINNER’S PHILOSOPHERS PROBLEM

 ALL THE PHILOSOPHERS ARE THINKING !!....

The philosophers 1 falls hungry

Philosopher 1 can eat

Philosopher 1 has completed its work

The philosophers 2 falls hungry

Philosopher 2 can eat

Philosopher 2 has completed its work

The philosophers 3 falls hungry

Philosopher 3 can eat

Philosopher 3 has completed its work

The philosopher 4 falls hungry

Philosopher 4 can eat

Philosopher 4 has completed its work

The philosophers 5 falls hungry

Philosopher 5 can Philosopher 5 has completed its word

RESULT:



EXP NO: 11 

EXP NAME: FIRST FIT , WORST FIT, BEST FIT ALLOCATION STRATEGY


AIM:

To implement

a) First fit

b) Best fit

c) Worst fit &

d) To make comparative study


THEORY:

Memory Management Algorithm

In an environment that supports dynamic memory allocation, a number of 

strategies are used to allocate a memory space of size n (unused memory partition) 

from the list free holes to the processes that are competing for memory.

First Fit: Allocation the first hole which is big enough.

Best Fit: Allocation the smallest hole which is big enough

Worst Fit: Allocation the largest hole which is big enough


ALGORITHM:

Step 1: Start the program.

Step 2: Get the number of memory partition and their sizes.

Step 3: Get the number of processes and values of block size for each process.

Step 4: First fit algorithm searches all the entire memory block until a hole which is 

big enough is encountered. It allocates that memory block for the requesting 

process.

Step 5: Best-fit algorithm searches the memory blocks for the smallest hole which 

can be allocated to requesting process and allocates if.

Step 6: Worst fit algorithm searches the memory blocks for the largest hole and 

allocates it to the process.

Step 7: Analyses all the three memory management techniques and display the best 

algorithm which utilizes the memory resources effectively and efficiently.

Step 8: Stop the program.


PROGRAM:


#include<stdio.h>


int main()

{

int p[20],f[20],min,minindex,n,i,j,c,f1[20],f2[20],f3[20],k=0,h=0,flag,t=0,n1;


printf("enter the number of memory partitions:\n");

scanf("%d",&n);

printf("enter the number of process");

scanf("%d",&n1);

for(i=0;i<n;i++)

{

printf("\n enter the memory partition size %d:",i+1);

scanf("%d",&f[i]);

f2[i]=f[i];

f3[i]=f[i];

}

for(i=0;i<n;i++)

{

printf("\n enter the page size %d:",i+1);


scanf("%d",&p[i]);

}

do

{

printf("\n1.first fit\n");

printf("\n2.best fit\n");

printf("\n3.worst fit\n");

printf("\nenter your choice\n");

scanf("%d",&c);

switch(c)

{

case 1:

for(i=0;i<n1;i++)

{

for(j=0;j<n;j++)

{

f1[i]=0;

if(p[i]<=f[j])

{

f1[i]=f[j];

f[j]=0;

break;

}

}

}


break;

case 2:

for(i=0;i<n1;i++)

{

min=9999;

minindex=-1;

for(j=0;j<n;j++)

{

if(p[i]<=f2[j] && f2[j]!=0 && min>f2[j])

{

min=f2[j];

minindex=j;

}

}

f1[i]=f[minindex];

f2[minindex]=0;

}

break;

case 3:

for(i=0;i<n1;i++)

{

f1[i]=0;

for(j=0;j<n;j++)

{

if(p[i]<f3[j])

 

{

k++;

if(k==1)

f1[i]=f3[j];

if(f1[i]<=f3[j])

{

flag=1;

f1[i]=f3[j];

h=j;

}

}

}

k=0;

if(flag==1)

f3[h]=0;

}

break;

default:

printf("\n out of choice");

}

printf("\n----------\n");

printf("\n|page |frame |free \n");

printf("\n----------\n");

t=0;

for(i=0;i<n1;i++)


{

h=f1[i]-p[i];

if(h<0)

h=0;

printf("\n%d\t\t%d\t\t%d",p[i],f1[i],h);

t=t+h;

}

printf("\n----------\n");

printf("\n total free spae in memory:%d",t);

}

while(c<4);

}


EXPNO: 12 

EXP NAME: BANKERS ALGORITHM


AIM:

Write a program to implement Banker’s Algorithm


ALGORITHM:

This algorithm was suggested by Dijkstar, the name banker is used here to indicate 

that it uses a banker’s activity for providing loans and receiving payment against the 

given loan. This algorithm places very few restrictions on the processes competing 

for resources. Every request for the resource made by a process is thoroughly 

analyzed to check, whether it may lead to a deadlock situation. If the result is yes 

then the process is blocked on this request. At some future time, its request is 

considered once again for resource allocation. So this indicated that, the processes 

are free to request for the allocation, as well as de-allocation of resources without 

any constraints. So this generally reduces the idling of resources.

Suppose there are (P) number of Processes and (r ) number of resources then its 

time complexity is proportional to P x r2 

At any given stage the OS imposes certain constraints on any process trying to use 

the resource. At a given moment during the operation of the system, processes P, 

would have been allocated some resources. Let these allocations total up to S.

Let (K=r-1) be the number of remaining resources available with the system. Then 

k>=0 is true, when allocation is considered.

Let maxk be the maximum resource requirement of a given process Pi.

Actk be the actual resource allocation to Pi at any given moment.

Then we have the following condition. 

Maxk<=p for all k and

To 


Disadvantages of Banker’s algorithm:


1. The maximum number of resources needed by the processes must be known in 

advance

2. The no of processes should be fixed.


PROGRAM:


#include <stdio.h> 

int current[5][5], maximum_claim[5][5], available[5];

int allocation[5] = { 0, 0, 0, 0, 0 };


int maxres[5], running[5], safe = 0;


int counter = 0, i, j, exec, resources, processes, k = 1;

int

main ()

{

  printf ("\nEnter number of processes: ");

  scanf ("%d", &processes);

  for (i = 0; i < processes; i++)

    {

      running[i] = 1;

      counter++;

    }

  

printf ("\nEnter number of resources: ");

  scanf ("%d", &resources);

  printf ("\nEnter Claim Vector:");

  for (i = 0; i < resources; i++)

    {

      scanf ("%d", &maxres[i]);

    }

  

printf ("\nEnter Allocated Resource Table:\n");

  for (i = 0; i < processes; i++)

    {

      for (j = 0; j < resources; j++)

{

  scanf ("%d", &current[i][j]);

}

    }

  printf ("\nEnter Maximum Claim Table:\n");

  for (i = 0; i < processes; i++)

    {

      for (j = 0; j < resources; j++)

{

  scanf ("%d", &maximum_claim[i][j]);

}

    }

  printf ("\nThe Claim Vector is: ");

  for (i = 0; i < resources; i++)

    {

      printf ("\t%d", maxres[i]);

    }

  

printf ("\nThe Allocated Resource Table:\n");

  for (i = 0; i < processes; i++)

    {

      for (j = 0; j < resources; j++)

{

  printf ("\t%d", current[i][j]);

}

      printf ("\n");

    }

  printf ("\nThe Maximum Claim Table:\n");

  for (i = 0; i < processes; i++)

    {

      for (j = 0; j < resources; j++)

{

  printf ("\t%d", maximum_claim[i][j]);

}

      printf ("\n");

    }

  for (i = 0; i < processes; i++)

    {

      for (j = 0; j < resources; j++)

{

  allocation[j] += current[i][j];

}

    }

  printf ("\nAllocated resources:");

  for (i = 0; i < resources; i++)

    {

      printf ("\t%d", allocation[i]);

    }

  

}


EXP NO:13 


EXP NAME: IMPLEMENT THE PRODUCER CONSUMER PROBLEM USING SEMAPHORE


AIM:

To write a program to implement producer consumer problem using semaphore.


ALGORITHM:

Step 1: Start.

Step 2: Let n be the size of the buffer.

Step 3: check if there are any producer.

Step 4: if yes check whether the buffer is full.

Step 5: If no the producer item is stored in the buffer.

Step 6: If the buffer is full the producer has to wait.

Step 7: Check there is any consumer. If yes check whether the buffer is empty 

Step 8: If no the consumer consumes them from the buffer.

Step 9: If the buffer is empty, the consumer has to wait.

Step 10: Repeat checking for the producer and consumer till required.

Step 11: Terminate the process.


PROGRAM:


#include <stdio.h>

 #include <stdlib.h>

 int main(){int s,n,b=0,p=0,c=0; printf("\n producer and consumer problem");

 do{printf("\n menu");

 printf("\n 1.producer an item");

 printf("\n 2.consumer an item");

 printf("\n 3.add item to the buffer"); 

 printf("\n 4.display status");

 printf("\n 5.exit");

 printf("\n enter the choice");

 scanf("%d",&s);

 switch(s){case 1:p=p+1;

 printf("\n item to be produced");

 break;

 case 2:if(b!=0){c=c+1;b=b-1;

 printf("\n item to be consumed");

 }else{ 

 printf ("\n the buffer is empty please wait...");

}


break;

case 

3:

if (b < n)

  {

    if (p != 0)

      {

b = b + 1;

printf ("\n item added to buffer");

      }

    else

      printf ("\n no.of items to add...");

  }

else

  printf ("\n buffer is full,please wait");

break;

case 4:

printf ("no.of items produced :%d", p);

printf ("\n no.of consumed items:%d", c);


printf ("\n no.of buffered item:%d", b);

break;

case 5:

exit (0);

}

}


while (s <= 5);

return 0;

}




OUTPUT:

1. Producer

2. Consumer

3. Exit 

Enter your choice : 1

Producer produces the item 1

Enter your choice : 1

Producer produces the item 2

Enter your choice : 1

Producer produces the item 3

Enter your choice : 1

Buffer is full

Enter your choice : 2

Consumer consumes item 3

Enter your choice : 2

Consumer consumes item 2

Enter your choice : 2

Consumer consumes item 1

Enter your choice : 2

Buffer is empty

Enter your choice : 3

RESULT:


EXP NO: 14 

EXP NAME: TO IMPLEMENT THE MEMORY MANAGEMENT POLICY-PAGING


AIM:


To implement the memory management policy-paging

ALGORITHM:

Step 1: Read all the necessary input from the keyboard.

Step 2: Pages - Logical memory is broken into fixed - sized blocks.

Step 3: Frames – Physical memory is broken into fixed – sized blocks.

Step 4: Calculate the physical address using the following

 Physical address = ( Frame number * Frame size ) + offset

Step 5: Display the physical address.

Step 6: Stop the process.


PROGRAM:


#include<stdio.h>


int main()

{

 int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;

 int s[10], fno[10][20];




printf("\nEnter the memory size -- ");

scanf("%d",&ms);


printf("\nEnter the page size -- ");

scanf("%d",&ps);


nop = ms/ps;

printf("\nThe no. of pages available in memory are -- %d ",nop);


printf("\nEnter number of processes -- ");

 scanf("%d",&np);

rempages = nop;

for(i=1;i<=np;i++)


{


printf("\nEnter no. of pages required for p[%d]-- ",i);

 scanf("%d",&s[i]);


if(s[i] >rempages)

{


printf("\nMemory is Full");

break;

}

rempages = rempages - s[i];


printf("\nEnter pagetable for p[%d] --- ",i);

 for(j=0;j<s[i];j++)

scanf("%d",&fno[i][j]);

}


printf("\nEnter Logical Address to find Physical Address ");

printf("\nEnter process no. and pagenumber and offset -- ");


scanf("%d %d %d",&x,&y, &offset);




if(x>np || y>=s[i] || offset>=ps)

printf("\nInvalid Process or Page Number or offset");


else

{ pa=fno[x][y]*ps+offset;

printf("\nThe Physical Address is -- %d",pa);


}

}




Thank you for visiting www.physicswallah.in !

3 comments:

  1. best site for notes

    ReplyDelete
  2. %option noyywrap
    %{
    #include
    void yyerror(char *);
    %}

    letter [a-z A-Z]
    digit [0-9]
    op [-+*]
    %%
    else|int|float {printf("%s is a keyward",yytext);}
    {digit}+ {printf("%s is a number",yytext);}
    {letter}({letter}|{digit})* {printf("%s is an identifier",yytext);}
    {op} {printf("%s is an operator",yytext);}
    . yyerror("error");
    %%
    void yyerror(char *s)
    {
    fprintf(stderr,"%s\n",s);
    }
    int main()
    {
    yylex();
    return 0;
    }

    ReplyDelete
  3. %option noyywrap
    %{
    #include
    #include"y.tab.h"
    void yyerror(char *s);
    extern int yylval;
    %}
    digit [0-9]
    %%
    {digit}+ {yylval=atoi(yytext);return NUM;}
    [-+*/\n] {return *yytext;}
    \( {return *yytext;}
    \) {return *yytext;}
    . {yyerror("syntax error");}
    %%






    %{
    #include
    void yyerror(char*);
    extern int yylex(void);
    %}
    %token NUM
    %%
    S:
    S E '\n' {printf("%d\n",$2);}
    |
    ;
    E:
    E '+' E {$$=$1+$3;}
    |E '-' E {$$=$1-$3;}
    |E '*' E {$$=$1*$3;}
    |E '/' E {$$=$1/$3;}
    |'(' E ')' {$$=$2;}
    |NUM {$$=$1;}
    %%
    void yyerror(char *s)
    {
    printf("%s",s);
    }
    int main()
    {
    yyparse();
    return 0;
    }

    ReplyDelete