0% found this document useful (0 votes)
12 views36 pages

Os Exp1to10

The document outlines a series of experiments involving various Linux commands and C programming examples related to file operations, process management, scheduling algorithms, and memory management techniques. Each experiment includes code snippets demonstrating the implementation of commands like 'mkdir', 'chmod', and algorithms such as First-Come-First-Serve (FCFS), Shortest Job First (SJF), and the Banker's Algorithm. The document serves as a practical guide for understanding system calls and resource management in programming.

Uploaded by

Darsh Jilka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views36 pages

Os Exp1to10

The document outlines a series of experiments involving various Linux commands and C programming examples related to file operations, process management, scheduling algorithms, and memory management techniques. Each experiment includes code snippets demonstrating the implementation of commands like 'mkdir', 'chmod', and algorithms such as First-Come-First-Serve (FCFS), Shortest Job First (SJF), and the Banker's Algorithm. The document serves as a practical guide for understanding system calls and resource management in programming.

Uploaded by

Darsh Jilka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Experiment No.

:1
1.Command -mkdir

2.Command -chmod

3.Command -sort

4.Command -chgrp

5.Command -man

6. Command -rm
7. Command -ls-l

8. Command -ps aux

9. Command -gedit

10. Command -cd

11. Command -chown


Experiment No.:2
1. Command
#top -b -o+%MEM|head -n 22

2. Display processes with highest memory usage


Command: #ps -eo pid,ppid,cmd,%mem,%cpu –sort=-%mem|head

3. Display current logged in user and logname


Experiment No.:3
1. Command -stat

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:

PROGRAM: Reading from a file: read()


// C program to illustrate
// read system Call
#include<stdio.h> #include <fcntl.h> #include<stdlib.h>
#include <unistd.h> int main()
{
int fd, sz;
}
char *c = (char *) calloc(100, sizeof(char));
fd = open("foo.txt", O_RDONLY);
if (fd < 0) { perror("r1"); exit(1); }
sz = read(fd, c, 10);
printf("called read(% d, c, 10). returned that"
%d bytes were read. \n", fd, sz);
c[sz] = '\0';
printf("Those bytes are as follows: %sq", c);

OUTPUT:

PROGRAM: Writing to a File: write()


// C program to illustrate
// write system Call
#include<stdio.h>
#include <fcntl.h> #include<stdlib.h> #include <unistd.h> #include
<string.h>
//I/O system Calls
int main (void)
{
int fd[2];
char buf1[12] = "hello world";
char buf2[12];
// assume foobar.txt is already created fd[0] = open("foobar.txt",
O_RDWR);
fd[1] = open("foobar.txt", O_RDWR); write(fd[0], buf1, strlen(buf1));
write(1,buf2,read(fd[1], buf2, 12));
close(fd[0]);
}
close(fd[1]);
return 0;

OUTPUT:

PROGRAM: Closing a file:close()


#include<stdio.h>
#include<stdlib.h> #include <fcntl.h>
#include <unistd.h> int main()
{
// assume that foo.txt is already created
int fd1 = open("foo.txt", O_RDONLY, 0);
close(fd1);
// assume that baz.tzt is already created
int fd2 = open("baz.txt", O_RDONLY, 0);
printf("fd2 = %d\n", fd2); exit(0);
}

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:

PROGRAM: Best Fit


#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
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\n:",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)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy