0% found this document useful (0 votes)
28 views

OS all valid programs

Uploaded by

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

OS all valid programs

Uploaded by

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

Program 1:

//Fork() system call

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

int main()

pid_t pid=fork();

if(pid==0)

printf("I am the child process\n");

if(pid>0)

printf("I am the parent process,the child process is:%d\n",pid);

if(pid<0)

perror("In fork():");

exit(0);

}
/*This is a program to exec() system call

,this is Newfile1.c file*/

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

int main(int argc,char*argv[])

printf("PID of Newfile1 is:%d",getpid());

char*args[]={"Hi","welcome","toMVJCE",NULL};

execv("./Newfile2",args);

printf("\nBack to Newfile1");

return 0;

/*This is a program to exec() system call

,this is Newfile2.c file*/

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

int main(int argc,char*arv[])

printf("We are in Newfile2.c");

printf("PID of Newfile2.c is:%d",getpid());

return 0;

}
//This is a program for wait()system call

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<sys/wait.h>

int main()

pid_t pid=fork();

if(pid==-1)

perror("In fork():");

exit(EXIT_FAILURE);

else if(pid==0)

printf("The child process(pid:%d) is executing.",getpid());

sleep(2);

printf("\nThe child process(pid:%d) is finished its execution",getpid());

exit(EXIT_SUCCESS);

else

printf("\nThe parent process(pid:%d) is waiting for the child process to finish its execution",getpid());

if(wait(NULL)==-1)

{
perror("\nFailed");

exit(EXIT_FAILURE);

printf("\nThe parent process(pid:%d) has detected that child process successfully finished its
execution",getpid());

return 0;

Program 2:

/*These are the programs for CPU scheduling algorithms*/

//FCFS//

#include<stdio.h>

void main()

int bt[20],wt[20],tat[20],i,n;

float awt,atat;

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

scanf("%d",&n);

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

printf("\nEnter the burst time for process%d",i);

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

wt[0]=awt=0;

tat[0]=atat=bt[0];
for(i=1;i<n;i++)

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

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

awt=wt[i]+awt;

atat=tat[i]+atat;

printf("\tProcess\tBurst time\tWaiting time\tTurnaround time\n");

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

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

printf("\nThe average waiting time is:%2f",awt/n);

printf("\nThe average turnaround time is:%2f",atat/n);

//priority//
#include<stdio.h>

void main()

int p[20],pri[20],bt[20],wt[20],tat[20],i,k,n,temp;

float awt,atat;

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

scanf("%d",&n);

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

{
p[i]=i;

printf("\nEnter the burst time and priority for process%d--",i);

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

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

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

if(pri[i]>pri[k])

temp=p[i];

p[i]=p[k];

p[k]=temp;

temp=pri[i];

pri[i]=pri[k];

pri[k]=temp;

temp=bt[i];

bt[i]=bt[k];

bt[k]=temp;

wt[0]=awt=0;

tat[0]=atat=bt[0];

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

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

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

awt=wt[i]+awt;

atat=tat[i]+atat;

printf("Process\tPriority\tBurst time\tWaiting time\tTurnaround time\n");

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

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

printf("\nThe average waiting time is:%2f",awt/n);

printf("\nThe average turnaround time is:%2f",atat/n);

//SJF//
#include<stdio.h>

void main()

int p[20],bt[20],wt[20],tat[20],i,k,n,temp;

float awt,atat;

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

scanf("%d",&n);

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

p[i]=i;
printf("\nEnter the burst time for process%d",i);

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

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

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

if(bt[i]>bt[k])

temp=bt[i];

bt[i]=bt[k];

bt[k]=temp;

temp=p[i];

p[i]=p[k];

p[k]=temp;

wt[0]=awt=0;

tat[0]=atat=bt[0];

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

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

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

awt=wt[i]+awt;
atat=tat[i]+atat;

printf("Process\tBurst time\tWaiting time\tTurnaround time\n");

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

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

printf("\nThe average waiting time is:%2f",awt/n);

printf("\nThe average turnaround time is:%2f",atat/n);

//Round Robin//
#include<stdio.h>

void main()

int n, i, qt, temp, sq = 0, count = 0;

int bt[20], wt[20], tat[20], rem_bt[20];

float awt = 0, atat = 0;

// Input number of processes

printf("Enter the number of processes: ");

scanf("%d", &n);

// Input burst times for processes

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

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

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

rem_bt[i] = bt[i]; // Initialize remaining burst time

// Input time quantum

printf("\nEnter the time slice for each process: ");

scanf("%d", &qt);

// Round Robin Scheduling Logic

while(1)

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

temp = qt;

if(rem_bt[i] == 0)

count++;

continue;

if(rem_bt[i] > qt)

rem_bt[i] -= qt;
}

else

temp = rem_bt[i];

rem_bt[i] = 0;

sq += temp;

tat[i] = sq; // Turnaround time = completion time

if(count == n) // All processes are complete

break;

// Calculate waiting time and display results

printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");

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

wt[i] = tat[i] - bt[i]; // Waiting time = turnaround time - burst time

awt += wt[i];

atat += tat[i];

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

}
// Display average waiting time and turnaround time

printf("\nThe average waiting time is: %.2f", awt / n);

printf("\nThe average turnaround time is: %.2f\n", atat / n);

//PRODUCER-CONSUMER OF SHARED MEMORY SYSTEM//


#include<stdio.h>

void main()

int produce,consume,in=0,out=0,choice=0,buffer[10],bufsize=10;

while(choice!=3)

printf("\n1.Produce\t2.Consume\t3.Exit\n");

printf("\nEnter your choice:");

scanf("%d",&choice);

switch(choice)

case 1:if((in+1)%bufsize==out)

printf("\nBuffer is full");

else

printf("\nEnter the value:");

scanf("%d",&produce);

buffer[in]=produce;
in=(in+1)%bufsize;

printf("\nThe produced value is:%d",produce);

break;

case 2:if(in==out)

printf("\nBuffer is empty");

else

consume=buffer[out];

printf("\nThe consumed value is:%d",consume);

out=(out+1)%bufsize;

break;

case 3:printf("\nExiting...");

break;

default:printf("\nInvalid choice!please try again");

}
//Writer process//
#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<unistd.h>

#include<fcntl.h>

int main()

int fd;

char buf[1024];

char *myfifo="/tmp/myfifo";

mkfifo(myfifo,0666);

fd=open(myfifo,O_WRONLY);

printf("\nRun reader process to read FIFO file\n");

read(fd,"Hi",sizeof("Hi"));

close(fd);

unlink(fd);

//Reader process//
#include<stdio.h>

#include<unistd.h>

#include<fcntl.h>

#include<sys/types.h>

#include<sys/stat.h>

#define max_buf 1024


int main()

int fd;

char buf[max_buf];

char *myfifo="/tmp/myfifo";

fd=open(myfifo,O_RDONLY);

read(fd,buf,max_buf);

printf("Writer:%s",buf);

close(fd);

return 0;

Banker’s Algorithm
#include<stdio.h>

int main()

int available[3],work[5],max[5][3],allocation[5][3],need[5][3],safe[5],totalres[5];

char finish[5];

int i,j,k,totalloc=0,state,value=0;

printf("Enter Instances of each Resource");

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

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

printf("Enter Maximum resources for each processes");

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

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

printf("\n Enter process %d Resource %d",i,(j+1));

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

printf("Enter number of resources allocated to each Process");

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

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

printf("\n Enter the resource of R%d allocated to process %d",(j+1),i);

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

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

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

need[i][j]=max[i][j]-allocation[i][j];

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

{
finish[i]='f';

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

totalloc=0;

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

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

available[i]=totalres[i]-totalloc;

work[i]=available[i];

printf("\n Allocated Resources \n");

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

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

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

printf("\n");

printf("\n Maximum Resources \n");

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

for(j=0;j<3;j++)
{

printf("%d",max[i][j]);

printf("\n");

printf("\n Needed Reources \n");

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

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

printf("%d",need[i][j]);

printf("\n");

printf("\n Available Reources");

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

printf("%d",available[i]);

printf("\n");

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

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

if((finish[i]=='f')&&(need[i][j]<=work[j]))
{

state=1;

else

state=0;

break;

} // end of innermost loop

if(state==1)

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

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

finish[i]='t';

safe[value]=i;

++value;

if(i==4)

if(value==5)

break;

}
else

i=-1;

} // end of outermost loop

printf("\n Safe States are");

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

printf("P%d",safe[i]);

return 0;

//Best fit-memory allocation technique//


#include<stdio.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];

printf("Enter the number of blocks:");

scanf("%d",&nb);

printf("\nEnter the number of files:");

scanf("%d",&nf);

printf("\nEnter the sizes of blocks:");


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

printf("\nBlock%d:",i);

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

printf("\nEnter the sizes of files:");

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

printf("\nfile%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\n");

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]);

//worst fit-memory allocation technique//


#include<stdio.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];

printf("\nMemory allocation scheme-Worst Fit");

printf("\nEnter the number of blocks:");

scanf("%d",&nb);

printf("\nEnter the number of files:");


scanf("%d",&nf);

printf("\nEnter the sizes of blocks:");

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

printf("\nBlock%d:",i);

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

printf("\nEnter the sizes of files:");

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

printf("\nfile%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(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\tFragment\n");

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]);

//FIRST FIT //
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int
frag[max],b[max],f[max],i,j,nb,nf,t
emp; 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]);
Page 25 }
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();
}

//FIFO//
#include<stdio.h>

int main() {

int i, j, n, a[50], frame[10], no, k, avail, count = 0;

// Input the number of pages


printf("\nENTER THE NUMBER OF PAGES:\n");

scanf("%d", &n);

// Input the page reference string

printf("\nENTER THE PAGE NUMBER:\n");

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

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

// Input the number of frames

printf("\nENTER THE NUMBER OF FRAMES:\n");

scanf("%d", &no);

// Initialize all frames to -1

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

frame[i] = -1;

j = 0; // Index for frame replacement

printf("\tRef String\tPage Frames\n");

// Process each page in the reference string

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

printf("%d\t\t", a[i]); // Print the current page being referenced

avail = 0;
// Check if the page is already in the frame

for (k = 0; k < no; k++) {

if (frame[k] == a[i]) {

avail = 1;

break;

// If the page is not in the frame, replace a page

if (avail == 0) {

frame[j] = a[i];

j = (j + 1) % no; // Move to the next frame for replacement

count++; // Increment page fault count

// Print the current state of frames

for (k = 0; k < no; k++) {

if (frame[k] != -1) {

printf("%d\t", frame[k]);

} else {

printf("-\t");

printf("\n");
}

// Print the total number of page faults

printf("Page Faults: %d\n", count);

return 0;

You might also like