Producer
-Consumer Problem:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
void main() {
int s, n = 5; // Set buffer size to 5
int b = 0; // Number of items in buffer
int p = 0; // Number of produced items
int c = 0; // Number of consumed items
clrscr();
printf("
Producer and Consumer Problem");
do {
printf("
Menu:");
printf("
1. Produce an item");
printf("
2. Consume an item");
printf("
3. Display status");
printf("
4. Exit");
printf("
Enter your choice: ");
scanf("%d", &s);
switch (s) {
case 1:
if (b < n) {
p++;
if (p > 0) {
b++;
printf("
Item added to buffer.");
} else {
printf("
No items to add to the buffer.");}
} else {
printf("
Buffer is full, please wait.");}
break;
case 2:
if (b > 0) {
c++;b--
;
printf("
Item consumed.");
} else {
printf("
Buffer is empty, please wait...");}
break;
case 3:
printf("
Number of items produced: %d", p);
printf("
Number of items consumed: %d", c);
printf("
Number of items in buffer: %d", b);
break;
case 4:
printf("
Exiting...");
break;
default:
printf("
Invalid choice. Please try again.");
break;}
} while (s != 4);
getch(); }Reader
-Writer Problem
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main() {
Int sread=0, swrite=0;
int ch,r=0;
clrscr();
printf("
Reader writer");
do{
printf("
Menu");
printf("
\
n
\t 1.Read from file");
printf("
\t2.Write to file");
printf("
\t 3.Exit the reader");
printf("
\t 4.Exit the writer");
printf("
\t 5.Exit");
printf("
Enter your choice:");
scanf("%d",&ch);
switch(ch) {
case 1: if(swrite0)
{
sread=1;
r+=1;
printf("
Reader %d reads",r);
}
else
{printf("
Not possible");
}
break;
case 2: if(sread0 && swrite0) {
swrite=1;
printf("
Writer in Progress");
}
else if(swrite1)
{printf("
Writer writes the files");
}
else if(sread1)
{printf("
Cannot write while reader
reads the file");
}
else
printf("
Cannot write file");
break;
case 3: if(r!=0)
{
printf(
"
The reader %d closes the
file",r);
r
-=1;
if(r0) {
printf("
Currently no readers access
the file");
sread=0; }}
else if(r1) {
printf("
Only 1 reader file");
}
else
printf("%d reader are reading the
file
",r);
break;
case 4: if (swrite1)
{
printf("
Writer close the file");
swrite=0; }
else
printf("
There is no writer in the file");
break;
case 5: exit(0);
} }
while(ch<6);
getch(); }Dining Philosopher’s Problem
#include<stdio.h>
#include<conio.h>
#define LEFT (i+4) %5
#define RIGHT (i+1) %5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
int state[5];
void putforks(int);
void test(int);
void takeforks(int);
void philosopher(int i) {
if(state[i]0) {
takeforks(i);
if(state[i]==EATING)
printf("
Eating in process....");
putforks(i); }}
void putforks(int i) {
state[i]=THINKING;
printf("
philosopher %d completed its works",i);
test(LEFT);
test(RIGHT); }
void takeforks(int i) {
state[i]=HUNGRY;
test(i); }
void test(int i) {
if(state[i]HUNGRY && state[LEFT]!=EATING &&
state[RIGHT]!=EATING) {
printf("
philosopher %d can eat",i);
state[i]=EATING; }}
void main() {
int i;
clrscr();
for(i=1;i<=5;i++)
state[i]=0;
printf("
\
n
\
t
\
t
\t Dining Philosopher Problem");
printf("
\
n
\
t
\t...........");
for(i=1;i<=5;i++) {
printf("
\
n
the philosopher %d falls hungry
",i);
philosopher(i); }
getch(); }FIRST COME FIRST SERVE SCHEDULING
#include<stdio.h>
#include<conio.h>
void main() {
int nop,wt[10],twt,tat[10],ttat,i,j,bt[10],t;
float awt,atat;
clrscr();
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("
Processid
\tBurstTime
\tWaitingTime
\tTurnaroundTi
me
");
for(i=0;i<nop;i++)
printf("%d
\
t
\t%d
\
t
\t%d
\
t
\t%d
",i,bt[i],wt[i],tat[i]);
printf("
Total Waiting Time:%d
",twt);
printf("
Total Around Time:%d
",ttat);
printf("
Average Waiting Time:%f
",awt);
printf("
Average Total Around Time:%f
",atat);
getch();SHORTEST JOB FIRST SCHEDULING
#include<stdio.h>
#include<conio.h>
void main() {
int nop,wt[10],twt,tat[10],ttat,i,j,bt[10],t;
float awt,atat;
clrscr();
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("
Processid
\tBurstTime
\tWaitingTime
\tTurnaroundTi
me
");
for(i=0;i<nop;i++)
printf("%d
\
t
\t%d
\
t
\t%d
\
t
\t%d
",i,bt[i],wt[i],tat[i]);
printf("
Total Waiting Time:%d
",twt);
printf("
Total Around Time:%d
",ttat);
printf("
Average Waiting Time:%f
",awt);
printf("
Average Total Around Time:%f
",atat);
getch(); }PRIORITY QUEUE SCHEDULING
#include<stdio.h>
#include<conio.h>
void main() {
int nop,t,wt[10],twt,tat[10],ttat,i,j,p[10],b[10],tmp;
float awt, atat;
clrscr();
awt=0.0;
atat=0.0;0
printf("Enter the number of process:");
scanf("%d",&nop);
for(i=0;i<nop;i++) {
printf("Enter the burst time of Process %d:",i);
scanf("%d",&b[i]); }
for(i=0;i<nop;i++)
printf("Enter the priority number of each Process %d:",i);
scanf("%d",&p[i]); }
for(i=0;i<nop;i++) {
for(j=i+1;j<nop;j++){
if(p[i]>p[j]){
t=p[i];
p[i]=p[j];
p[j]=t;
tmp=b[i];
b[i]=b[j];
b[j]=tmp;}}}
wt[0]=0;
tat[0]=b[0];
twt=wt[0];
ttat=tat[0];
for(i=1;i<nop;i++) {
wt[i]=wt[i
-1]+b[i
-1];
tat[i]=wt[i]+b[i];
twt+=wt[i];
ttat+=tat[i]; }
awt=(float)twt/nop;
atat=(float)ttat/nop;
printf("Process No:
\tPriority:
\tBurst Time:
\tWaiting
Time
\tTurnaround Time:
");
for(i=0;i<nop;i++)
printf("%d
\
t
\t%d
\
t
\t%d
\
t
\t%d
\
t
\t%d
",i,p[i],b[i],wt[i],tat[i]);
printf("Total TurnAround Time:%d
",ttat);
printf("Total Waiting Time:%d
",twt);
printf("Average Waiting Time:%f
",awt);
printf("Average Turnaround Time:%f
",atat);
getch(); }BANKER’S ALGORITHM
#include <stdio.h>
#define MAXPROCESSES 10
#define MAXRESOURCES 10
int allocation[MAXPROCESSES][MAXRESOURCES];
int maximum[MAXPROCESSES][MAXRESOURCES];
int need[MAXPROCESSES][MAXRESOURCES];
int available[MAXRESOURCES];
int processes, resources;
int i,j,k,p;
// Function to calculate the Need matrix
void calculateNeed() {
for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
need[i][j] = maximum[i][j]
- allocation[i][j];}}
}
// Function to check if the system is in a safe state
int isSafe() {
int work[MAXRESOURCES], finish[MAXPROCESSES];
int safeSequence[MAXPROCESSES], count = 0;
int found = 0;
for ( i = 0; i < resources; i++) {
work[i] = available[i];}
for (i = 0; i < processes; i++) {
finish[i] = 0;}
while (count < processes) {
for ( p = 0; p < processes; p++) {
if (finish[p] == 0) {
for (j = 0; j < resources; j++) {
if (need[p][j] > work[j])
break;}
if (j == resources) {
for (k = 0; k < resources; k++) {
work[k] += allocation[p][k];}
safeSequence[count++] = p;
finish[p] = 1;
found = 1;}}}
if (found == 0) {
printf("The system is not in a safe state.
");
return 0;}}
printf("The system is in a safe state.
Safe sequence is: ");
for (i = 0; i < processes; i++) {
printf("%d ", safeSequence[i]);}
printf("
");
return 1; }
int main() {
printf("Enter the number of processes: ");
scanf("%d", &processes);
printf("Enter the number of resources: ");
scanf("%d", &resources);
printf("Enter the allocation matrix:
");
for (i = 0; i < processes; i++) {
for ( j = 0; j < resources; j++) {
scanf("%d", &allocation[i][j]);}}
printf("Enter the maximum matrix:
");
for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
scanf("%d", &maximum[i][j]);}}
printf("Enter the available resources:
");
for (i = 0; i < resources; i++) {
scanf("%d", &available[i]);}
calculateNeed();
if (isSafe()) {
printf("The system is safe.
");
} else {
printf("The system is not safe.
");}
return 0;
}FIRST FIT, WORST FIT, BEST FIT ALLOCATION STRATEGY
#include <stdio.h>
#define MAX 25
void firstFit(int blockSize[], int m, int processSize[], int n) {
int allocation[20];
int i,j;
for (i = 0; i < n; i++) {
allocation[i] =
-1;}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j]
-= processSize[i];
break;}}}
printf("
First Fit Allocation:
");
printf("Process No.
\tProcess Size
\tBlock No.
");
for (i = 0; i < n; i++) {
printf("%d
\
t
\t%d
\
t
\t", i + 1, processSize[i]);
if (allocation[i] !=
-1)
printf("%d
", allocation[i] + 1);
else
printf("Not Allocated
");}
}
void bestFit(int blockSize[], int m, int processSize[], int n) {
int allocation[20];
int bestIdx =
-1;
for (i = 0; i < n; i++) {
allocation[i] =
-1;}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (bestIdx ==
-1 || blockSize[bestIdx] > blockSize[j]) {
bestIdx = j;}}}
if (bestIdx !=
-1) {
allocation[i] = bestIdx;
blockSize[bestIdx]
-= processSize[i];}}
printf("
Best Fit Allocation:
");
printf("Process No.
\tProcess Size
\tBlock No.
");
for (i = 0; i < n; i++) {
printf("%d
\
t
\t%d
\
t
\t", i + 1, processSize[i]);
if (allocation[i] !=
-1)
printf("%d
", allocation[i] + 1);
else
printf("Not Allocated
");}
}
void worstFit(int blockSize[], int m, int processSize[], int n) {
int allocation[20];
int worstIdx =
-1;
for ( i = 0; i < n; i++) {
allocation[i] =
-1;}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (worstIdx ==
-1 || blockSize[worstIdx] < blockSize[j])
{
worstIdx = j;}}}
if (worstIdx !=
-1) {
allocation[i] = worstIdx;
blockSize[worstIdx]
-= processSize[i];}}
printf("
Worst Fit Allocation:
");
printf("Process No.
\tProcess Size
\tBlock No.
");
for (i = 0; i < n; i++) {
printf("%d
\
t
\t%d
\
t
\t", i + 1, processSize[i]);
if (allocation[i] !=
-1)
printf("%d
", allocation[i] + 1);
else
printf("Not Allocated
");}
}
int main() {
int blockSize[MAX], processSize[MAX];
int m, n;
printf("Enter the number of memory blocks: ");
scanf("%d", &m);
printf("Enter the size of each block:
");
for (i = 0; i < m; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blockSize[i]);}
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the size of each process:
");
for (i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processSize[i]);}
firstFit(blockSize, m, processSize, n);
// Reset block sizes for the next strategy
for (i = 0; i < m; i++) {
printf("Enter the size of block %d again: ", i + 1);
scanf("%d", &blockSize[i]);}
bestFit(blockSize, m, processSize, n);
// Reset block sizes for the next strategy
for (i = 0; i < m; i++) {
printf("Enter the size of block %d again: ", i + 1);
scanf("%d", &blockSize[i]);}
worstFit(blockSize, m, processSize, n);
return 0; }SIMULATE PAGING TECHNIQUE OF MEMORY
MANAGEMENT
#include<stdio.h>
#define MAX 50
int main()
{
int page[MAX],i,n,f,ps,off,pno;
int choice=0;
printf("
Enter the no of peges in memory: ");
scanf("%d",&n);
printf("
Enter page size: ");
scanf("%d",&ps);
printf("
Enter no of frames: ");
scanf("%d",&f);
for(i=0;i<n;i++)
page[i]=-1;
printf("
Enter the page table
");
printf("(Enter frame no as -1 if that page is not present in any
frame)
");
printf("
pageno\tframeno
-------\t-------");
for(i=0;i<n;i++)
{
printf("
%d\t\t",i);
scanf("%d",&page[i]);
}
do
{
printf("
Enter the logical address(i.e,page no & offset):");
scanf("%d%d",&pno,&off);
if(page[pno]-1)
printf("
The required page is not available in any of
frames");
else
printf("
Physical address(i.e,frame no &
offset):%d,%d",page[pno],off);
printf("
Do you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice1);
return 1;
}SIMULATE PAGE REPLACEMENT ALGORITHM
#include<stdio.h>
int main()
{
int incomingStream[] =
{7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages =
sizeof(incomingStream)/sizeof(incomingStream[0]);
printf("Incoming \t Frame 1 \t Frame 2 \t Frame 3");
int temp[3];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(incomingStream[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;if((pageFaults <= frames) && (s == 0))
{
temp[m] = incomingStream[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}printf("
");
printf("%d\t\t\t",incomingStream[m]);
for(n = 0; n < frames; n++)
{
if(temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
}
printf("
Total Page Faults:\t%d
", pageFaults);
return 0;
}2.a Write a shell script to compare two string.
echo “Enter two string”
read a
read b
if [ -z $a ]
then
echo “ First String is empty: Null String”
fi
if [ -z $b ]
then
echo “ Second String is empty: Null String”
fi
if [ $a = $b ]
then
echo “Strings are equal: strings Matched”
else
echo “Strings are not equal: Strings not match”
fi2.b Write a shell script to extract the first and last
character from a string.
a="abcdef"
first="${a:0:1}"
last="${a: -1}"
echo "$first"
echo "$last"2.c Write a shell script to check whether the number is
palindrome or not.
echo "Enter the number: "
read n
num=0
a=$n
while [ $n -gt 0 ]
do
num=expr $num \* 10
k=expr $n % 10
num=expr $num + $k
n=expr $n / 10
done
if [ $num -eq $a ]
then
echo "It's a Palindrome"
else
echo "Not a Palindrome"
fi3.a write a shell script to find the factorial of a given
number.
echo "Enter the number: "
read n
fact=1
for((i=2;i<=n;i++))
do
fact=$((fact * i))
done
echo "Factorial = $fact"3.b Write a shell script to find the sum of n numbers using
while loop.
echo “enter the number :”
read n
i=1
s=0
echo “enter the numbers”
while [ $i -le $n]
do
read num
s=$((s + num))
i=$((i+1))
done
echo “sum = $s”3.c Write a shell script to implement menu driven
program to perform all arithmetic operation using case
statement.
echo “enter two integer values”
read a
read b
echo –e “Menu
1 for Addition
2 for Substraction
3 for
Multiplication
4 for
Division
5 for Remainder”
echo “enter choice”
read ch
case $ch in
1) echo “Sum=$(expr $a + $b)”;;
2) echo “Substraction=$(expr $a - $b)”;;
3) echo “Multiplication=$(expr $a * $b)”;;
4) echo “Division=$(expr $a / $b)”;;
5) echo “Remainder=$(expr $a % $b)”;;
6) echo “invalid Choice:Try Again!”
esacNo 3.d Write a shell script to print following pattern.
echo “ enter number of rows”
read n
i=1
while [ $i –le $n ]
do
j=1
while [ $j –le $i ]
do
echo –n “*”
j=$((j + 1))
done
echo
i=$((i + 1))
done4.a Converting File names from Uppercase to Lowercase
varname="THIS IS a TEST"
echo "$VARNAME" | tr '[:upper:]' '[:lower:]'
name="sathyabama"
echo "$name" | tr '[:lower:]' '[:upper:]'4.b Write a shell script to count the number of
characters, words and lines in the file.
Create a file :
cat > filename
My university name is Sathyabama
My Department is Computer Science
This is Chennai
Program:
echo "enter file name"
read file
c=cat $file | wc -c
w=cat $file | wc -w
l=grep -c "." $file
echo "Number of characters is $c"
echo "Number of words is $w"
echo "Number of Lines is $l"4.c Write a shell script to read and check the file exists or
not, if not create the file.
echo “enter name of file”
read filename
if [ -f $filename ]
then
echo “File $filename Exits!”
else
touch $filename
fi5 Write a shell script to Manipulate Date/Time/Calendar.
echo "Date in various forms"
echo $(date)
echo "Today is $(date +'%m/%d/%y')"
echo "Today is $(date +'%Y-%m-%d')"
echo "Calender is various form"
echo $(cal 9 2024)
echo $(cal 2024)
echo $(cal -m May)
echo "Time in various formats"
echo $(date +"%T")
echo $(date +"%r")
echo $(date +"%I:%S:%M")No. 6 Write a shell script Showing various system
information
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 O
There's nothing here yet (well, except for this message), but clicking on the "+" button in the menu above should change that. Have fun! :)