Os Exp1to10
Os Exp1to10
:1
1.Command -mkdir
2.Command -chmod
3.Command -sort
4.Command -chgrp
5.Command -man
6. Command -rm
7. Command -ls-l
9. Command -gedit
7. Command -whoami
Experiment No.: 4
PROGRAM: Opening a file: open()
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
extern int errno;
int main()
{
//if file does not have in directory
// then file foo.txt is created.
int fd = open("foo.txt", O_RDONLY | O_CREAT); printf("fd =
%d/n", fd);
if (fd ==-1)
}
}
{
// print which type of error have in a code
printf("Error Number %d\n", errno);
// print program detail "Success or failure" perror("Program");
return 0;
OUTPUT:
OUTPUT:
OUTPUT:
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<unistd.h> #include<sys/types.h>
int main() {
printf("Process ID = %d\n",getpid());
printf("Parent Process ID = %d\n",getpid());
return 0;
}
OUTPUT:
Experiment No.:5
PROGRAM: (FCFS)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,n,bt[10],compt[10], at[10], wt[10],tat[10];
float sumwt=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 arrival time of %d process\n", n); for(i=0;i<n;i++)
scanf("%d",&at[i]);
}
compt[0]=bt[0]-at[0];
for(i=1;i<n;i++)
compt[i]=bt[i]+compt[i-1];
for(i=0;i<n;i++)
{
tat[i]=compt[i]-at[i];
wt[i]=tat[i]-bt[i];
sumtat+=tat[i];
sumwt+=wt[i];
}
avgwt=sumwt/n;
avgtat=sumtat/n;
printf(" -----------------------------\n");
printf("PN\tBt\tCt\tTat\t\t\n");
printf(" ------------------------------\n");
for(i=0;i<n;i++)
{
printf("%d\t%2d\t%2d\t%2d\t%2d\n",i,bt[i],compt[i],tat[i],wt[i]);
}
printf(“-------------------------------\n”);
printf(" Avgwt = %.2f\tAvgtat = %.2f\n",avgwt,avgtat\n”);
printf(“---------------------------------\n”);
getch();
}
OUTPUT:
PROGRAM: (SJF)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,n,bt[10],compt[10], wt[10], tat[10],temp;
float sumwt=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]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
}
compt[0]=bt[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\t\t\n");
printf(" -------------------------------\n");
for(i=0;i<n;i++)
{
printf("%2d\t%2d\t%2d\t%d\n",i,bt[i],compt[i],tat[i],wt[i]);
}
printf(" -----------------------------\n");
printf(" Avgwt = %.2f\tAvgtat = %.2f\n", avgwt, avgtat);
printf(“ ------------------------------\n”);
getch();
}
OUTPUT:
Experiment No.: 6
PROGRAM: (Producer-Consumer)
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
}
{
return 0;
int wait(ints)
return (--s);
}
int signal(ints)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
}
mutex=signal(mutex);
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty-signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
}
mutex=signal(mutex);
OUTPUT:
Experiment No.:7
PROGRAM: Banker's Algorithm
{
#include <stdio.h>
int main()
// PO, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = { { 0, 1, 0 }, // PO // Allocation Matrix
{2, 0, 0}, // P1
{3, 0, 2 }, // P2
{2, 1, 1 }, // P3
{0, 0, 2 } }; // P4
int max[5][3] = { {7, 5, 3 }, // PO // MAX Matrix
{3, 2, 2 }, // P1
{9, 0, 2 }, // P2
{2, 2, 2 }, // P3
{4, 3, 3 } }; // P4
int avail[3] = { 3, 3, 2 }; // Available Resources
int f[n], ans[n], ind = 0; for (k = 0; k < n; k++) { f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
}
need[i][j] = max[i][j] - alloc[i][j];
int y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {
int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n = 1; i++)
{
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
return (0);
}
OUTPUT:
Experiment No.:8
PROGRAM: (Worst Fit)
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int
frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - WorstFit");
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 ofthe 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) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
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();
}
OUTPUT:
PROGRAM: First Fit
#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();
}
OUTPUT:
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;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();
}
OUTPUT:
Experiment No.: 9
PROGRAM: (FIFO)
#include <stdio.h>
#include <stdlib.h>
#define MAX_FRAMES 10
int main() {
int num_frames, num_pages, page_faults = 0;
int frames[MAX_FRAMES];
int pages[MAX_FRAMES];
printf("Enter the number of frames: ");
scanf("%d", &num_frames);
printf("Enter the number of pages: ");
scanf("%d", &num_pages);
printf("Enter the page reference string: ");
for (int i = 0; i < num_pages; ++i)
scanf("%d", &pages[i]);
for (int i = 0; i < num_frames; ++i)
frames[i] = -1; // Initialize frames to -1 indicating empty
int frame_index = 0;
printf("\nPage Replacement Steps:\n");
for (int i = 0; i < num_pages; ++i) {
printf("Page %d: ", pages[i]);
int page_found = 0;
for (int j = 0; j < num_frames; ++j) {
if (frames[j] == pages[i]) {
page_found = 1;
break;
}
}
if (!page_found) {
printf("Page fault! ");
if (frames[frame_index] != -1)
printf("Replacing page %d with page %d",
frames[frame_index], pages[i]);
frames[frame_index] = pages[i];
frame_index = (frame_index + 1) % num_frames;
++page_faults;
}
printf("\nCurrent frames: ");
for (int j = 0; j < num_frames; ++j) {
if (frames[j] != -1)
printf("%d ", frames[j]);
else
printf("- ");
}
printf("\n");
}
printf("\nTotal Page Faults: %d\n", page_faults);
return 0;
}
OUTPUT:
PROGRAM: (LRU)
#include<stdio.h>
int main()
{
int i,j,k,l,m,n,p=15,c=0;
int a[20],b[20],q,min=50;
printf("enter no. of reference string: ");
scanf("%d",&n);
printf("enter size of frame: ");
scanf("%d",&m);
printf("enter the elements of ref. string: \n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
for(j=0; j<m; j++)
b[j]=-1; //initialize all frame elements with -1
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(a[i]==b[j]) //check if element already present in frame,if
true then no page fault.
break;
else
continue;
}
if(j==m)
{
for(k=0; k<m; k++)
if(b[k]==-1)
{
b[k]=a[i];//replace the initial value by string value
break;
}
if(k==m)
{
min=50;
for(j=0; j<m; j++)
{
l=i-1;
while(l>=0)
{
if(a[l]==b[j])
{
if(l<min)
{
min=l;
p=j;
}
break;
}
l--;
}
}
}
b[p]=a[i];
c++;
}
printf("\n\n");
for(k=0; k<m; k++)
printf(" %d",b[k]);
}
printf("\n No of page fault is:%d",c);
}
OUTPUT:
Experiment No.:10
PROGRAM: (CSCAN)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20]
,queue2[20], temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
queue[i+1]=0;
for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek
%d\n",queue[j],queue[j+1],diff); }
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seektime is %f\n",avg);
return 0;
}
OUTPUT:
PROGRAM: (FCFS)
#include<stdio.h>
#include<stdlib.h>
int main(){
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
scanf("%d",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++){
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek
%d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seektime is%f\n",avg);
return 0;
}
OUTPUT:
PROGRAM: (SCAN)
#include<stdio.h>
#include<math.h>
int main(){
int queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20]
,queue2[20], temp1=0,temp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++){
scanf("%d",&temp);
if(temp>=head){
queue1[temp1]=temp;
temp1++;
}else{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++){
for(j=i+1;j<temp1;j++){
if(queue1[i]>queue1[j]){
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}
}
}
for(i=0;i<temp2-1;i++){
for(j=i+1;j<temp2;j++){
if(queue2[i]<queue2[j]){
temp-queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
for(i=temp1+2,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[i]=0;
queue[0]=head;
for(j=0;j<=n+1;j++){
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with seek
%d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek); avg=seek/(float)n;
printf("Average seek time is %f\n",avg); return 0;
}
OUTPUT: