Professional Documents
Culture Documents
Os Lab Manual 26-04-2023
Os Lab Manual 26-04-2023
Date :
AIM :
CONTENT:
Syn:$date
Syn:$cal 2 2009
Creation: Syn:$cat>filename
b)grep–used to search a particular word or pattern related to that word from the file.
Eg:$grepanu student
Syn:$rm filename
Syn:$cut <option><filename>
$cut–c1-10emp
$cut–f 3,6emp
-c cutting columns
-f cutting fields
Eg:$head student
Syn:$head-2student
Syn:$tail filename
Eg:$tail student
$chmodu+rw,g+rwstudent
$chmodg=rwx student
Assigns absolute permission for groups of all read, write and execute permissions
k)wc–it counts the number of lines, words, character in a specified file(s) with the options as –
l,-w,-c
$wc –w filename
$wc–c filename
RESULT :
Ex.no : 2 SHELL PROGRAMMING (using looping, control constructs etc.,)
Date :
AIM :
ALGORITHM :
Step 1- Open vi editor and start the program and go to the insert mode for entering the
code.
Step 2- Read the inputs file use conditional statement like while ,if,if then, switch case.
Step 3- Print the output by checking whether the condition is true or false
Step 4 – Enter into the escape mode for the execution of the result and verify the output.
PROGRAMS :
#!/bin/bash
echo "Enter the a value"
read a
echo " Enter the b value"
read b
c=$ (expr "$a" + "$b")
echo "sum: $c"
d = & (expr "$a" - "$b")
echo "Sub: $d"
e=$ (expr "$a" /* "$b")
echo "mul: $e"
f = $ (expr "$a" % "b")
echo "div: $f"
ii) While Statement
#!/bin/bash
a=1
while [ "$a" -lt 11]
do
Echo "$a"
a = $(expr "$a" + 1)
done
iii) IF Statement
#!/bin/bash
a=10
b=20
if [ "$a" =="$b"]
then
echo "a is equal to b"
fi
if [ "$a" != "$b"]
Then
echo "a is not equal to b"
fi
iv) Switch Statement
#!/bin/bash
CARS = " bmw"
case "$CARS" in
# case 1
"mercedes") echo "headquatres-affaliserbach,germany" ;;
# case 2
"audi") echo "headquatres - Ingelstak, germary" ;;
# case 3
"bmw") echo "headquatres -chennai, Tamilnadu, India";;
Esac
v) IF-THEN Statement
#!/bin/bash
a = 20
b=20
if [ "$a" == "$b"]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
RESULT:
Ex.No :3a SYSTEM CALLS PROCESS CREATION
Date :
AIM :
ALGORITHM:
STEP 4: Check pid is less than 0 then print error else if pid is equal to 0 then execute
command else parent process wait for child process.
PROGRAM:
void main()
{
intpid;
pid=fork();
if(pid<0)
{
Printf("cannot create the file");
Exit(-1);
}
if(pid==0)
{
Printf("child process");
Exit(0);
}
else
{
Printf("parent process");
}
}
RESULT:
Ex.No : 3b EXECUTING A COMMAND
Date :
AIM :
ALGORITHM :
PROGRAM :
echo welcome
ps
exec wc el
RESULT :
Ex.No : 3c SLEEP COMMAND USING GETPID
Date :
AIM:
ALGORITHM:
STEP 1: Start the execution and create a process using fork() command.
STEP 3:In the child process print it pid and it corresponding pid.
STEP 6: After making the sleep for the parent process for 10 seconds print it pid.
PROGRAM:
Void main ()
{
intpid;
pid=fork();
if (pid==0)
{
printf("\nChild process\n");
printf("\nChild process id is %d",getpid());
printf("\nIts parent process 1 is %d",getppid());
sleep (5);
printf("\nChild process after sleep=5\n");
printf("\nChild process id is %d", getpid());
Printf("\nIts parent process id is %d",getppid());
}
else
{
printf("\nparent process");
sleep (10);
printf("\nChild process id is %d", getpid());
printf("\nIts parent process id is %d", getppid());
printf("\nParent terminates");
}
}
RESULT:
Ex.No : 4a READING FROM A FILE
Date :
AIM:
ALGORITHM:
2.Open a file.
PROGRAM :
#include<stdio.h>
int main()
{
charstr[100];
FILE *fp;
fp=fopen("file1.dat","r");
while(!feof(fp))
{
fscanf(fp,"%s",str);
printf("%s",str);
}
fclose(fp);
}
RESULT:
Ex.No : 4b WRITING INTO A FILE
Date :
AIM:
ALGORITHM:
Step2.Open a file.
PROGRAM:
#include<stdio.h>
int main ()
charstr[100];
FILE *fp;
printf ("enter the string");
gets (str);
fp=fopen("filel.dat", "w+");
while (!feof (fp))
{
fscanf(fp,"%s", str);
}
fprintf(fp,"%s", str);
return 0;
}
RESULT:
Ex.No : 4c FILE CREATION
Date :
AIM:
ALGORITHM:
Step2:Create the file using create function and assign a variable to it.
Step3:If the value of the variable is less then print file cannot be created ,otherwise print
file is created.
PROGRAM :
Void main ()
{
int id;
if (id=creat ("z.txt",0)==-1)
{
printf ("cannot create the file");
exit (1);
}
else
{
printf("file is create");
exit (1);
}}
RESULT:
Ex.No : 5a FIRST COME FIRST SERVE (FCFS)
Date :
AIM :
ALGORITHM :
STEP 3 : Initially, waiting time of first process is zero and total time for first process is
starting
STEP 4 : Calculate the total time and processing time for remaining process.
STEP 5 : Waiting time of one process is the total time of previous process.
STEP 6 : Total time of process is calculated by adding waiting time for lack process.
STEP 7 : Calculate average waiting time by dividing total waiting time by total no of
process.
STEP 8 : Calculate average turnaround time by dividing total turnaround by total no.of
process
PROGRAM :
#include<stdio.h>
// Function to find the waiting time for all processes
intwaitingtime(intproc[],int n,
intburst_time[],intwait_time[]){
// waiting time for first process is 0
wait_time[0]=0;
// calculating waiting time
for(int i =1; i < n ; i++)
wait_time[i]=burst_time[i-1]+wait_time[i-1];
return0;
}
// Function to calculate turn around time
intturnaroundtime(intproc[],int n,
intburst_time[],intwait_time[],int tat[]){
// calculating turnaround time by adding
// burst_time[i] + wait_time[i]
int i;
for( i =0; i < n ; i++)
tat[i]=burst_time[i]+wait_time[i];
return0;
}
//Function to calculate average time
intavgtime(intproc[],int n,intburst_time[]){
intwait_time[n], tat[n],total_wt=0,total_tat=0;
int i;
//Function to find waiting time of all processes
waitingtime(proc, n,burst_time,wait_time);
//Function to find turn around time for all processes
turnaroundtime(proc, n,burst_time,wait_time, tat);
//Display processes along with all details
printf("Processes Burst Waiting Turn around \n");
// Calculate total waiting time and total turn
// around time
for( i=0; i<n; i++){
total_wt=total_wt+wait_time[i];
total_tat=total_tat+ tat[i];
printf(" %d\t %d\t\t %d \t%d\n", i+1,burst_time[i],wait_time[i], tat[i]);
}
printf("Average waiting time = %f\n",(float)total_wt/(float)n);
printf("Average turn around time = %f\n",(float)total_tat/(float)n);
return0;
}
// main function
int main(){
//process id's
intproc[]={1,2,3};
int n =sizeofproc/sizeofproc[0];
//Burst time of all processes
intburst_time[]={5,8,12};
avgtime(proc, n,burst_time);
return0;
}
RESULT:
Ex.No : 5b IMPLEMENTATION OF SJF SCHEDULING
Date :
AIM :
To write a C program to implement CPU Scheduling algorithm for Shortest Job First.
ALGORITHM :
STEP 3 : Initially, waiting time of first process is zero and total time for first process is
starting
STEP 4 : Calculate the total time and processing time for remaining process.
STEP 5 : Waiting time of one process is the total time of previous process.
STEP 6 : Total time of process is calculated by adding waiting time for lack process.
STEP 7 : Calculate average waiting time by dividing total waiting time by total no of
process.
STEP 8 : Calculate average turnaround time by dividing total turnaround by total no.of
process
PROGRAM :
#include<stdio.h>
int main()
{
intbt[20],p[20],wt [20], tat [20],i,j,n, total=0,pos, temp;
floatavg_wt, avg_tat;
printf("enter number of process :");
scanf("%d", &n);
printf("\nEnter burst time:n");
for (i=0;i<n;i++)
{
printf("p%d: ",i+1);
scanf("%d", &bt [i]);
p[i]=i+1;
}
for (i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n; j++)
{
if (bt [j]<bt [pos])
pos=j;
}
temp=bt [i];
bt [i]=bt [pos];
bt [pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt [0]=0;
for (i-1;i<n;i++)
{
wt [i]=0;
for (j=0;j<i;j++)
wt[i]+=bt [j];
total+ =wt [i];
}
avg_wt=(float) total/n:
total=0;
printf("\nprocessbursttimewaitingtimeturnaroundtime");
for (i=0;i<n;i++)
{
tat [i]=bt [i]+wt [i];
total +=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t %d",p[i], bt[i], wt [i],tat[i]);
avg_tat=(float) total/n;
printf("\nAverage waiting time=%f",avg_wt);
printf("\nAverageturnround time=%f",avg_tat);
}
RESULT:
Ex.No : 6a IMPLEMENTATION OF PRIORITY SCHEDULING
Date :
AIM:
ALGORITHM :
STEP 3: For each process in the ready Q, assign the process id and accept the CPU
burst time
STEP 5: Set the waiting of the first process as „0‟ and its burst time as its turn around
time
(e) Waiting time for process(n)= waiting time of process (n-1) + Burst time of
process(n-1)
(f) Turn around time for Process(n)= waiting time of Process(n)+ Burst time for
process(n)
STEP 7: Calculate
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
inti,j,n,bt[10],p[10],compt[10], wt[10],tat[10],temp1,temp2;
floatsumwt=0.0,sumtat=0.0,avgwt,avgtat;
clrscr();
printf("Enter number of processes: ");
scanf("%d",&n);
printf("Enter the burst time of %d process\n", n);
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
printf("Enter the priority of %d process\n", n);
for(i=0;i<n;i++)
scanf("%d",&p[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(p[i]>p[j])
{
temp1=bt[i];
bt[i]=bt[j];
bt[j]=temp1;
temp2=p[i];
p[i]=p[j];
p[j]=temp2;
}
compt[0]=bt[0]; wt[0]=0;
for(i=1;i<n;i++)
compt[i]=bt[i]+compt[i-1];
for(i=0;i<n;i++)
{
tat[i]=compt[i];
wt[i]=tat[i]-bt[i];
sumtat+=tat[i];
sumwt+=wt[i];
}
avgwt=sumwt/n; avgtat=sumtat/n;
printf("------------------------------\n");
printf("Bt\tCt\tTat\tWt\n");
printf("------------------------------\n");
for(i=0;i<n;i++)
{
printf("%2d\t%2d\t%2d\t%2d\n",bt[i],compt[i],tat[i],wt[i]);
}
printf("------------------------------\n");
printf(" Avgwt = %.2f\tAvgtat = %.2f\n",avgwt,avgtat);
printf("-------------------------------\n");
getch();
}
RESULT :
Ex.No : 6b IMPLEMENTATION OF RR SCHEDULING
Date :
AIM:
ALGORITHM :
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time
slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Calculate the no. of time slices for each process where
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
(a) Waiting time for process(n) = waiting time of process(n-1)+ burst time of process(n-
1 ) + the time difference in getting the CPU from process(n-1)
(b) Turn around time for process(n) = waiting time of process(n) + burst time of
process(n)+ the time difference in getting CPU from process(n).
Step 7: Calculate
PROGRAM :
#include<stdio.h>
#include<conio.h>
struct process
{
charpn[10];
intbt,ct,time;
}p[10];
void main()
{
inti,full,n,tq,wt[10],tat[10], time1=0;
floatavgwt=0.0;
clrscr();
printf("Enter number of processes:");
scanf("%d",&n);
printf("Enter process name and burst time of %d process\n", n);
for(i=0;i<n;i++)
{
scanf("%s%d",&p[i].pn,&p[i].bt);
p[i].time=p[i].bt;
}
printf("Enter quantum:");
scanf("%d",&tq);
full=n;
while(full)
{
for(i=0;i<n;i++)
{
if(p[i].bt>=tq)
{
p[i].bt-=tq;
time1=time1+tq;
}
else if(p[i].bt!=0)
{
time1+=p[i].bt;
p[i].bt=0;
}
else
continue;
if(p[i].bt==0)
{
full=full-1;
tat[i]=time1;
}
}
}
for(i=0;i<n;i++)
{
p[i].ct=tat[i];
wt[i]=tat[i]-p[i].time;
}
printf("----------------------------------\n");
printf("PN\tBt\tCt\tTat\tWt\n");
printf("----------------------------------\n");
for(i=0;i<n;i++)
{
printf("%2s\t%2d\t%2d\t%2d\t%2d\n",p[i].pn,p[i].time,p[i].ct,tat[i],wt[i]);
avgwt+=wt[i];
}
printf("----------------------------------\n");
avgwt=avgwt/n;
printf(" Average waiting time = %.2f\n",avgwt);
printf("-----------------------------------\n");
getch();
}
RESULT :
Ex.No : 7 PRODUCER- CONSUMER PROBLEM USING
Date : SEMAPHORES
AIM:
ALGORITHM :
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#include<stdlib.h>
#define buffersize 10
pthread_mutex_tmutex;
pthread_ttidP[20],tidC[20];
sem_tfull,empty; int counter; int buffer[buffersize];
void initialize()
{
pthread_mutex_init(&mutex,NULL);
sem_init(&full,1,0);
sem_init(&empty,1,buffersize);
counter=0;
}
void write(int item)
{
buffer[counter++]=item;
}
int read()
{
return(buffer[--counter]);
}
void * producer (void * param)
{
intwaittime,item,i;
item=rand()%5; waittime=rand()%5;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
printf("\nProducer has produced item: %d\n",item);
write(item);
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
void * consumer (void * param)
{
intwaittime,item;
waittime=rand()%5;
sem_wait(&full);
pthread_mutex_lock(&mutex);
item=read();
printf("\nConsumer has consumed item: %d\n",item);
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
int main()
{
int n1,n2,i; initialize();
printf("\nEnter the no of producers: ");
scanf("%d",&n1);
printf("\nEnter the no of consumers: ");
scanf("%d",&n2);
for(i=0;i <n1;i++)
pthread_create(&tidP[i],NULL,producer,NULL);
for(i=0;i<n2;i++)
pthread_create(&tidC[i],NULL,consumer,NULL);
for(i=0;i<n1;i++)
pthread_join(tidP[i],NULL);
for(i=0;i<n2;i++)
pthread_join(tidC[i],NULL);
//sleep(5);
exit(0); }
RESULT :
Ex.No : 8 IMPLEMENTATION OF BANKER’S ALGORITHM
Date :
AIM:
ALGORITHM:
Step-3: Read the number of process, resources, allocation matrix and available matrix.
Step-4: Compare each and every process using the banker‟s algorithm.
Step-5: If the process is in safe state then it is a not a deadlock process otherwise it is a
deadlock process
PROGRAM:
#include <stdio.h>
int current[5][5], maximum_claim[5][5], available[5];
int allocation[5] = {0, 0, 0, 0, 0};
intmaxres[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);
printf("\nAllocated resources:");
for (i = 0; i < resources; i++)
{
printf("\t%d", allocation[i]);
}
printf("\nAvailable resources:");
for (i = 0; i < resources; i++)
{
printf("\t%d", available[i]);
}
printf("\n");
while (counter != 0)
{
safe = 0;
for (i = 0; i < processes; i++)
{
if (running[i])
{
exec = 1;
for (j = 0; j < resources; j++)
{
if (maximum_claim[i][j] - current[i][j] >available[j])
{
exec = 0;
break;
} }
if (exec)
{
printf("\nProcess%d is executing\n", i + 1);
running[i] = 0;
counter--;
safe = 1;
printf("\n");
}
}
return 0;
}
RESULT:
Ex.No : 9a IMPLEMENTATION OF MEMORY MANAGEMENT SCHEMES
Date :
AIM:
ALGORITHM :
Step 1: START.
Step 2: At first get the no of processes and blocks.
Step 3: Allocate the process by if(size of block>=size of the process) then allocate the
process else move to the next block.
Step 4: Now Display the processes with blocks and allocate to respective process.
Step 5: STOP.
PROGRAM
#include<stdio.h>
void main()
{
intbsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
for(i = 0; i < 10; i++)
{
flags[i] = 0;
allocation[i] = -1;
}
printf("Enter no. of blocks: ");
scanf("%d", &bno);
printf("\nEnter size of each block: ");
for(i = 0; i <bno; i++)
scanf("%d", &bsize[i]);
printf("\nEnter no. of processes: ");
scanf("%d", &pno);
printf("\nEnter size of each process: ");
for(i = 0; i <pno; i++)
scanf("%d", &psize[i]);
for(i = 0; i <pno; i++) //allocation as per first fit
for(j = 0; j <bno; j++)
if(flags[j] == 0 &&bsize[j] >= psize[i])
{
allocation[j] = i;
flags[j] = 1;
break;
}
//display allocation details
printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");
for(i = 0; i <bno; i++)
{
printf("\n%d\t\t%d\t\t", i+1, bsize[i]);
if(flags[i] == 1)
printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);
else
printf("Not allocated");
}
}
RESULT :
Ex.No : 9b IMPLEMENTATION OF MEMORY MANAGEMENT SCHEMES
AIM :
ALGORITHM :
6. If not start at the hole that is sharing the pervious first fit search end
PROGRAM :
#include<stdio.h>
#include<process.h>
void main()
{
int a[20],p[20],i,j,n,m;
printf("Enter no of Blocks.\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst Block size:",i);
scanf("%d",&a[i]);
}
printf("Enter no of Process.\n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst Process:",i);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[j]<=a[i])
{
printf("The Process %d allocated to %d\n",j,a[i]);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
{
if(p[j]!=10000)
{
printf("The Process %d is not allocated\n",j);
}
}
}
RESULT :
Ex.No : 9c IMPLEMENTATION OF MEMORY MANAGEMENT SCHEMES
AIM :
ALGORITHM:
Step 3: Initialize by selecting each process to find the maximum block size that can be
assigned to the current process.
Step 4: If the condition does not fulfill, they leave the process.
Step 5: If the condition is not fulfilled, then leave the process and check for the next
process.
Step 6: Stop.
PROGRAM :
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
RESULT :
Ex.No : 10a IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHM
Date : (FIFO)
AIM :
ALGORITHM:
Step 2. If the memory holds fewer pages, then the capacity else goes to step 5.
Step 3. Push pages in the queue one at a time until the queue reaches its maximum
capacity or all page requests are fulfilled.
Step 5. Else, pop the topmost page from the queue as it was inserted first.
Step 6. Replace the topmost page with the current page from the string.
Step 8. Stop
PROGRAM :
#include <stdio.h>
int main()
{
int incomingStream[] = {4, 1, 2, 4, 5};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
printf("\n");
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");
}
}
RESULT :
Ex.No : 10b IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHM
Date : (LRU)
AIM :
ALGORITHM:
1. Start
6. Allocate the pages in to frames by selecting the page that has not been used for the
longest period of time.
8. stop
PROGRAM :
#include<stdio.h>
#include<coniio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
{
clrscr(); printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames....");
scanf("%d",&nof);
printf(" Enter no.of reference string..");
scanf("%d",&nor); printf("\n Enter reference string..");
for(i=0;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
printf("\n\t Reference NO %d->\t",ref[i]);
for(j=0;j<nof;j++)
{ if(frm[j]==ref[i])
{ flag=1; break; }
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else victim=lruvictim();
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
}
recent[ref[i]]=i;
}
printf("\n\n\t No.of page faults...%d",pf); getch();
}
int lruvictim()
{
int i,j,temp1,temp2;
for(i=0;i<nof;i++)
{
temp1=frm[i];
lrucal[i]=recent[temp1];
}
temp2=lrucal[0];
for(j=1;j<nof;j++)
{
if(temp2>lrucal[j])
temp2=lrucal[j];
}
for(i=0;i<nof;i++)
if(ref[temp2]==frm[i])
return i;
return 0;
}
RESULT: