Your SlideShare is downloading. ×
1. Shortest job first (non- preemptive):
#include<stdio.h>
#include<unistd.h>
#include<string.h>
void main()
{
float avw=0...
int tbt=0;
int minimum1();
int main()
{
int minv,locv,mins,locs;
printf("nenter the number of processes:");
scanf("%d",&n)...
printf("n%d %d %d %d
%d",p1[i].pid,p1[i].at,p1[i].bt,p1[i].wt,p1[i].tat);
}
}
int minimum1()
{
int loc,z;
int mini;
mini=9...
{
p[i].w=p[i-1].w+p[i-1].b;
p[i].ta=p[i].w+p[i].b;
avgw+=p[i].w;
avgt+=p[i].ta;
printf("n%stt%dtt%dtt%dtt%d",p[i].name,p[i...
p[i].ta=p[i].w+p[i].b;
avgw+=p[i].w;
avgt+=p[i].ta;
printf("n%stt%dtt%dtt%d",p[i].name,p[i].b,p[i].w,p[i].ta);
}
printf("n...
{
avail[i]=ins[i]-tmp[i];
printf("t%d",avail[i]);
}
//main logic starts:P
while(count
{ //if finish array has all true's(a...
Enter total no of resources:3
Randomly Generated Allocation Matrix:
3 1 2
0 3 0
1 2 4
1 2 2
0 4 3
1 0 1
2 1 1
Randomly Gen...
{
avgwait+=wait[i];
avgturn+=turn[i];
}
avgwait/=n;
avgturn/=n;
printf("nArraival CPU Burst Start Finish Wait Turnn");
for...
for(k=j+1;k<12;k++)
{
if(fr[i]==p[k])
{
lg[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<frsize;i++)
{
if(lg[i]==0)
{
index=i;
f...
j=0;
printf("tref stringt page framesn");
for(i=1;i<=n;i++)
{
printf("%dtt",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]=...
{
printf("%d",buffer[i]);
}
getch();
}
To implement the c program for shortest job first scheduling algorithm
ALGORITHM
1....
{
for(j=i;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
temp1=p[i];
b[i]=b[j];
p[i]=p[j];
b[j]=temp;
p[j]=temp1;
}
}
}
w[0]=0;
for...
scanf("%d",&n);
pthread_t tid[n];
for(i=0;i<n;i++)
{
res=pthread_create(&tid[i],NULL,multithread,(void *)i);
if(res!=0)
{
...
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %dn",iret1);
printf("Thread 2 return...
Algorithm | Source Programming
1. Start the program.
2. Get the no of jobs from the user.
3. When job1 is processing, job ...
}
w[0]=0;
for(i=1;i<n;i++)
{
w[i]=w[i-1]+b[i-1];
}
t[0]=b[0];
for(i=1;i<n;i++)
{
t[i]=t[i-1]+b[i];
}
printf("n processes t...
for(i=0;i<count;i++)
{
for(j=0;j<n;j++)
{
if(s[j]>tq)
{
temp1=temp;
temp=temp+tq;
printf("process p%d exectued from %d to ...
for(j=i+1;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
temp=q[i];
q[i]=q[j];
q[j]=temp;
}
}
}
w[0]=0;
for(i...
scanf("%d",&a[i].b);
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(a[i].b>a[j].b)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
pr...
{
printf("n%d %d %d",p1[i].pid,p1[i].at,p1[i].bt);
}
minv=p1[0].at;
locv=0;
for(i=1;i<n;i++)
{
if(p1[i].at<minv)
{
locv=i;...
}
3. Priority(non-preemptive)
#include<stdio.h>
#include<unistd.h>
void main()
{
int n,i,j;
float avgw,avgt;
struct proces...
}p[10];
int i,n,q,tbt=0,ct=0;
float avgt=0,avgw=0;
printf("nnumber of processes:");
scanf("%d",&n);
printf("nenter the det...
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("nnEnte...
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}while(count != p &...
write(pipefds[1], "Hi Mom!", 7);
read(pipefds2[0], instring2, 7);
cout << "Just received a message that says: " << instrin...
switch(ch)
{
case 1:
// child to parent communication
if (pid == 0) /* child : sends message to parent*/
{
/* close unused...
return 0;
}
Upcoming SlideShare
Loading in...5
×

Programs for Operating System

663

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
663
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
44
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Programs for Operating System"

  1. 1. 1. Shortest job first (non- preemptive): #include<stdio.h> #include<unistd.h> #include<string.h> void main() { float avw=0,avt=0; struct pro { char name[20]; int b,w,ta; }a[10],temp; int n,i,j; printf("number of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("nenter name of process %d:",i+1); scanf("%s",a[i].name); printf("n enter the brust time of process %d:",i+1); scanf("%d",&a[i].b); } for(i=0;i<n;i++) { for(j=i;j<n;j++) { if(a[i].b>a[j].b) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("nnametB.TtW.TtTAT"); for(i=0;i<n;i++) { if(i==0) { a[i].w=0; a[i].ta=a[i].b; } else { a[i].w=a[i-1].w+a[i-1].b; a[i].ta=a[i].w+a[i].b; } avw+=a[i].w; avt+=a[i].ta; printf("n%st%dt%dt%d",a[i].name,a[i].b,a[i].w,a[i].ta); } avt/=n; avw/=n; printf("naverage waiting time:%fn average true around time:%f",avw,avt);} 2. Shortest job first ( preemptive) #include<stdio.h> struct proc { int pid; int at,bt,wt,tat,rbt; }; struct proc p1[10]; int i,j,k,n,no,m; float atat=0.0,awt=0.0;
  2. 2. int tbt=0; int minimum1(); int main() { int minv,locv,mins,locs; printf("nenter the number of processes:"); scanf("%d",&n); printf("nenter the proc information:"); printf("npid at bt"); for(i=0;i<n;i++) { p1[i].wt=0; p1[i].tat=0; scanf("%d%d%d",&p1[i].pid,&p1[i].at,&p1[i].bt); tbt+=p1[i].bt; p1[i].rbt=p1[i].bt; } printf("nthe proc information:"); printf("npid at bt"); for(i=0;i<n;i++) { printf("n%d %d %d",p1[i].pid,p1[i].at,p1[i].bt); } minv=p1[0].at; locv=0; for(i=1;i<n;i++) { if(p1[i].at<minv) { locv=i; //tells min at process in locv minv=p1[i].at; } } printf("ngantt chart:"); for(i=minv;i<tbt+minv;i++) { no=minimum1(); printf("%d p[%d]",i,p1[no].pid); p1[no].rbt=p1[no].rbt-1; for(k=0;k<n;k++) { if(p1[k].rbt>0&&p1[k].at<=i&&k!=no) { p1[k].wt++; } } } printf("%d",tbt+minv); for(i=0;i<n;i++) { awt+=p1[i].wt; } awt=awt/n; for(i=0;i<n;i++) { p1[i].tat=p1[i].wt+p1[i].bt; atat+=p1[i].tat; } atat=atat/n; printf("n average wt=%f, average tat=%f",awt,atat); printf("nthe proc information:"); printf("npid at bt wt tat"); for(i=0;i<n;i++) {
  3. 3. printf("n%d %d %d %d %d",p1[i].pid,p1[i].at,p1[i].bt,p1[i].wt,p1[i].tat); } } int minimum1() { int loc,z; int mini; mini=99; loc=-1; for(z=0;z<n;z++) { if(p1[z].rbt>0&&p1[z].at<=i&&p1[z].rbt<mini) { mini=p1[z].rbt; loc=z; } } return loc; } 3. Priority(non-preemptive) #include<stdio.h> #include<unistd.h> void main() { int n,i,j; float avgw,avgt; struct process { char name[20]; int b,w,ta,p; }p[10],temp; printf("nnumber of processes:"); scanf("%d",&n); printf("nenter the details of the processes"); for(i=0;i<n;i++) { printf("nenter the name of process %d:",i+1); scanf("%s",p[i].name); printf("nbrust time of the process %d:",i+1); scanf("%d",&p[i].b); printf("npriority of the process %d:",i+1); scanf("%d",&p[i].p); } for(i=0;i<n;i++) { for(j=i;j<n;j++) { if(p[i].p>p[j].p) { temp=p[i]; p[i]=p[j]; p[j]=temp; } } } p[0].w=0; p[0].ta=p[0].b; avgw=p[0].w; avgt=p[0].ta; printf("nnamettbrust-time waiting-time priority turnaround-time"); printf("n%stt%dtt%dtt%dtt%d",p[0].name,p[0].b,p[0].w,p[0].p,p[0].ta); for(i=1;i<n;i++)
  4. 4. { p[i].w=p[i-1].w+p[i-1].b; p[i].ta=p[i].w+p[i].b; avgw+=p[i].w; avgt+=p[i].ta; printf("n%stt%dtt%dtt%dtt%d",p[i].name,p[i].b,p[i].w,p[i].p,p[i].ta); } printf("naverage of waiting time of the processes is:%f",avgw/n); printf("naverage turn-arount time of the processes is:%f",avgt/n); } 4. Round-robin #include<stdio.h> #include<unistd.h> void main() { struct process { char name[20]; int b,w,ta,r,la; }p[10]; int i,n,q,tbt=0,ct=0; float avgt=0,avgw=0; printf("nnumber of processes:"); scanf("%d",&n); printf("nenter the details of the processes"); for(i=0;i<n;i++) { printf("nenter the name of process %d:",i+1); scanf("%s",p[i].name); printf("nbrust time of the process %d:",i+1); scanf("%d",&p[i].b); tbt+=p[i].b; p[i].w=0; p[i].r=p[i].b; p[i].la=0; } printf("enter the value of the quanta:"); scanf("%d",&q); while(tbt!=ct) { for(i=0;i<n;i++) { if(p[i].r<q&&p[i].r!=0) { printf("%s---->%dn",p[i].name,p[i].r); p[i].w+=ct-p[i].la; ct+=p[i].r; p[i].la=ct; p[i].r=0; } else if(p[i].r!=0) { printf("%s---->%dn",p[i].name,q); p[i].w+=ct-p[i].la; ct+=q; p[i].la=ct; p[i].r-=q; } } } printf("nnamettbrust-time waiting-time turnaround-time"); for(i=0;i<n;i++) {
  5. 5. p[i].ta=p[i].w+p[i].b; avgw+=p[i].w; avgt+=p[i].ta; printf("n%stt%dtt%dtt%d",p[i].name,p[i].b,p[i].w,p[i].ta); } printf("naverage of waiting time of the processes is:%fnaverage turn-arount time of the processes is:%f",avgw/n,avgt/n); } Program:Implenment Deadlock Detection algorithm(Simulation) in C language */ #include #include //We are using rand() function:) int main() { int alloc[10][10],req[10][10],ins[10],avail[10],tp,tr,i,j; int tmp[10]={0},count=0; bool finish[10]={false},flag; printf("Enter total no of processes:"); scanf("%d",&tp); printf("Enter total no of resources:"); scanf("%d",&tr); printf("Randomly Generated Allocation Matrix:n"); //itz tp x tr order matrix for(i=0;i { for(j=0;j { alloc[i][j]=rand()%5; printf("t%d",alloc[i][j]); } printf("n"); } printf("Randomly Generated Request Matrix:n"); for(i=0;i { for(j=0;j { req[i][j]=rand()%5; printf("t%d",req[i][j]); } printf("n"); } printf("Randomly Generated Resource Instance Vetctor:n"); for(i=0;i { ins[i]=rand()%10+tr+tp; //Just to increase resource instances printf("t%d",ins[i]); } //To calculate resource availability vector for(i=0;i { for(j=0;j { tmp[i]+=alloc[j][i]; //sum calculated columnwise for allocation matrix } } printf("nCalculated Availability Vector:n"); for(i=0;i
  6. 6. { avail[i]=ins[i]-tmp[i]; printf("t%d",avail[i]); } //main logic starts:P while(count { //if finish array has all true's(all processes to running state) //deadlock not detected and loop stops! for(i=0;i { count=0; //To check whether resources can be allocated any to blocked process if(finish[i]==false) { for(j=0;j { if(req[i][j]<=avail[j]) { count++; } } flag=false; if(count==tr) { for(j=0;j { avail[j]+=alloc[i][j]; //allocated reources are released and added to available! } finish[i]=true; printf("nProcess %d is transferred to running state and assumed finished",i+1); } else flag=true; } } count=0; for(j=0;j { if(finish[j]==true) { count++; } } } for(i=0;i { if(finish[i]==false) { printf("n Oops! Deadlock detected and causing process is:process(%d)n",i+1); break; } } i=i-1; if(finish[i]==true) printf("nHurray! Deadlock not detected:-)n"); return 0; } /* Sample Output: Enter total no of processes:7
  7. 7. Enter total no of resources:3 Randomly Generated Allocation Matrix: 3 1 2 0 3 0 1 2 4 1 2 2 0 4 3 1 0 1 2 1 1 Randomly Generated Request Matrix: 3 2 4 2 0 2 3 2 0 4 2 2 3 4 2 3 1 1 2 4 3 Randomly Generated Resource Instance Vetctor: 11 19 14 Calculated Availability Vector: 3 6 1 Process 3 is transferred to running state and assumed finished Process 4 is transferred to running state and assumed finished Process 5 is transferred to running state and assumed finished Process 6 is transferred to running state and assumed finished Process 7 is transferred to running state and assumed finished Process 1 is transferred to running state and assumed finished Process 2 is transferred to running state and assumed finished Hurray! Deadlock not detected:-)*/ FCFS: #include<stdio.h> #include<stdlib.h> main() { int n,i,j,sum=0; int arrv[10],burst[10],start[10]; int finish[10],wait[10],turn[10]; float avgturn=0.0,avgwait=0.0; start[0]=0; printf("Enter the number of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the Arrival and CPU Burst time of %d process:",i+1); scanf("%d%d",&arrv[i],&burst[i]); } for(i=0;i<n;i++) { sum=0; for(j=0;j<i;j++) sum=sum+burst[j]; start[i]=sum; } for(i=0;i<n;i++) { finish[i]=burst[i]+start[i]; wait[i]=start[i]; turn[i]=burst[i]+wait[i]; } for(i=0;i<n;i++)
  8. 8. { avgwait+=wait[i]; avgturn+=turn[i]; } avgwait/=n; avgturn/=n; printf("nArraival CPU Burst Start Finish Wait Turnn"); for(i=0;i<n;i++) printf("%dt %dt %dt %dt %dt %dn",arrv[i],burst[i],start[i], finish[i],wait[i],turn[i]); printf("nAverage waiting time=%f",avgwait); printf("nAverage turn around time=%f",avgturn); } Optimal Page Replacement Algo: #include<stdio.h> #include<conio.h> int fr[3]; void main() { void display(); int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3]; int max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0,frsize=3; clrscr(); for(i=0;i<3;i++) { fr[i]=-1; } for(j=0;j<12;j++) { flag1=0; flag2=0; for(i=0;i<3;i++) { if(fr[i]==p[j]) { flag1=1; flag2=1; break; } } if(flag1==0) { for(i=0;i<3;i++) { if(fr[i]==-1) { fr[i]=p[j]; flag2=1; break; } } } if(flag2==0) { for(i=0;i<3;i++) lg[i]=0; for(i=0;i<frsize;i++) {
  9. 9. for(k=j+1;k<12;k++) { if(fr[i]==p[k]) { lg[i]=k-j; break; } } } found=0; for(i=0;i<frsize;i++) { if(lg[i]==0) { index=i; found=1; break; } } if(found==0) { max=lg[0]; index=0; for(i=1;i<frsize;i++) { if(max<lg[i]) { max=lg[i]; index=i; } } } fr[index]=p[j]; pf++; } display(); } printf("n no of page faults:%d",pf); getch(); } void display() { int i; printf("n"); for(i=0;i<3;i++) printf("t%d",fr[i]); } Page Replacement by FIFO #include<stdio.h> int main() { int i,j,n,a[50],frame[10],no,k,avail,count=0; printf("n ENTER THE NUMBER OF PAGES:n"); scanf("%d",&n); printf("n ENTER THE PAGE NUMBER :n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("n ENTER THE NUMBER OF FRAMES :"); scanf("%d",&no); for(i=0;i<no;i++) frame[i]= -1;
  10. 10. j=0; printf("tref stringt page framesn"); for(i=1;i<=n;i++) { printf("%dtt",a[i]); avail=0; for(k=0;k<no;k++) if(frame[k]==a[i]) avail=1; if (avail==0) { frame[j]=a[i]; j=(j+1)%no; count++; for(k=0;k<no;k++) printf("%dt",frame[k]); } printf("n"); } printf("Page Fault Is %d",count); return 0; } Page Replacement by LRU: #include<stdio.h> #include,conio.h> main() { int buffer[3],k,a[10],i,j,c[10]; count=0,n; clrscr(); printf("Enter the elements"); for(i=1;i<=10;i++) { scanf("%d",&a[i]; [i]=a[i]; } for(i=1;i<=7;i++) { count=0; for(j=1;j<=3;j++) { if(buffer[j]==a[i]) { break; } count=count++; } if(count==3) { for(j=1;j<=3;j++) { if(buffer[j]==[i]) { buffer[j]=a[i]; break; }}} for(j=i-2;k=1;k<1;k<j;j++;k++) c[k]=a[j]; }} printf("The present value of buffer"); for(i=1;i<=3;i++)
  11. 11. { printf("%d",buffer[i]); } getch(); } To implement the c program for shortest job first scheduling algorithm ALGORITHM 1. Start the process 2. Declare the array size 3. Get the number of elements to be inserted 4. Select the process which have shortest burst will execute first 5. If two process have same burst length then FCFS scheduling algorithm used 6. Make the average waiting the length of next process 7. Start with the first process from it’s selection as above and let other process to be in queue 6. Calculate the total number of burst time 7. Display the values 8. Stop the process PROGRAM: #include<stdio.h> int main() { int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i; float att=0,awt=0; for(i=0;i<10;i++) { b[i]=0;w[i]=0; } printf("enter the number of process"); scanf("%d",&n); printf("enter the burst times"); for(i=0;i<n;i++) { scanf("%d",&b[i]); p[i]=i; } for(i=0;i<n;i++)
  12. 12. { for(j=i;j<n;j++) { if(b[i]>b[j]) { temp=b[i]; temp1=p[i]; b[i]=b[j]; p[i]=p[j]; b[j]=temp; p[j]=temp1; } } } w[0]=0; for(i=0;i<n;i++) w[i+1]=w[i]+b[i]; for(i=0;i<n;i++) { t[i]=w[i]+b[i]; awt=awt+w[i]; att=att+t[i]; } awt=awt/n; att=att/n; printf("nt process t waiting time t turn around time n"); for(i=0;i<n;i++) printf("t p[%d] t %d tt %d n",p[i],w[i],t[i]); printf("the average waitingtimeis %fn",awt); printf("the average turn around time is %fn",att); return 1; } OUTPUT: enter the number of process 5 enter the burst times 2 4 5 6 8 process waiting time turn around time p[0] 0 2 p[1] 2 6 p[2] 6 11 p[3] 11 17 p[4] 17 25 the average waitingtime is 7.200000 the average turn around time is 12.200000 Thread: #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<pthread.h> #include<sys/types.h> #include<math.h> void *multithread(void *arg); int main() { int i,res,n; void *result; printf("nEnter the no of thread you want create:");
  13. 13. scanf("%d",&n); pthread_t tid[n]; for(i=0;i<n;i++) { res=pthread_create(&tid[i],NULL,multithread,(void *)i); if(res!=0) { perror("Thread creation failedn"); exit(EXIT_FAILURE); } sleep(1); } printf("nWaiting for thread to finish.......n"); for(i=n-1;i>=0;i--) { res=pthread_join(tid[i],&result); if(res==0) { printf("Picked up a thread %dn",(int *)result); } else { perror("Thread join failed"); } } printf("All donen"); exit(EXIT_SUCCESS); } void *multithread(void *arg) { int *j=(int *)arg; //int l; printf("nThread funtion is running argument was %dn",(int *)j); //l=1+(int)(9.0*rand()/(RAND_MAX+1.0)); //sleep(l); printf("Bye from %dn",(int *)j); pthread_exit(j); } Thread2: #include <stdio.h> #include <stdlib.h> #include <pthread.h> void *print_message_function( void *ptr ); main() { pthread_t thread1, thread2; char *message1 = "Thread 1"; char *message2 = "Thread 2"; int iret1, iret2; /* Create independent threads each of which will execute function */ iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2); /* Wait till threads are complete before main continues. Unless we */ /* wait we run the risk of executing an exit which will terminate */ /* the process and all threads before the threads have completed. */
  14. 14. pthread_join( thread1, NULL); pthread_join( thread2, NULL); printf("Thread 1 returns: %dn",iret1); printf("Thread 2 returns: %dn",iret2); exit(0); } void *print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf("%s n", message); } C Program to demonstrate dynamic memory allocation using malloc() #include<stdio.h> #include<stdlib.h> int main() { int* grades; int sum = 0, i, numberOfStudents; float average; printf("Enter the number of students: "); scanf("%d", &numberOfStudents); getchar(); if((grades = (int*) malloc(numberOfStudents * sizeof(int))) == NULL) { printf("nError: Not enough memory to allocate grades arrayn"); exit(1); } printf("nEnter the grades of %d students (in separate lines):n", numberOfStudents); for(i = 0; i < numberOfStudents; i++) { scanf("%d", &grades[i]); getchar(); } /* calculate sum */ for(i = 0; i < numberOfStudents; i++) sum += grades[i]; /* calculate the average */ average = (float) sum / numberOfStudents; printf("nThe average of the grades of all students is %.2f", average); getchar(); return(0); } To write a C program to implement Semaphore.
  15. 15. Algorithm | Source Programming 1. Start the program. 2. Get the no of jobs from the user. 3. When job1 is processing, job 2 is also starts processing. 4. When job 1 enters critical section, next job starts processing. 5. When job1 comes out of the critical section, the other job enters the critical section. 6. The above 3 steps are performed for various programs. 7. End the program. Example Source code programming in C Program #include<stdio.h> main() { int i,a=1,h=2,n; printf("n Enter the no of jobs"); scanf("%d",&n); for(i=0;i<n;i++) { if(a==1) { printf("processing %d......! n", i+1); a++; } if(h>1) { if(i+2<=n) { printf("n processing %d.....! n",i+2); } printf("n Process %d Enters Critical section", i+1); printf("n Process %d Leaves Critical section", i+1); } h+1; } } Example Output Result "semaphore.c" 25L, 359C written [staff@linux-router staff]$ cc semaphore.c [staff@linux-router staff]$ gcc semaphore.c [staff@linux-router staff]$ ./a.out Enter the no of jobs 2 processing 1......! processing 2.....! Process 1 Enters Critical section Process 1 Leaves Critical section Process 2 Enters Critical section Process 2 Leaves Critical section // fcfs simulation #include<conio.h> void main() { int n,b[100],w[100],t[100],i; float aw=0,at=0; printf("enter no: of processesn"); scanf("%d",&n); printf("enter processes burst timen"); for(i=0;i<n;i++) { scanf("%d",&b[i]);
  16. 16. } w[0]=0; for(i=1;i<n;i++) { w[i]=w[i-1]+b[i-1]; } t[0]=b[0]; for(i=1;i<n;i++) { t[i]=t[i-1]+b[i]; } printf("n processes tt burst times tt waiting time tt turnaround time n"); for(i=0;i<n;i++) { printf("p%dtt %dtt %dtt %dn",i,b[i],w[i],t[i]); } for(i=0;i<n;i++) { aw+=w[i]; at+=t[i]; } aw=aw/n; at=at/n; printf("avg waiting time=%fn",aw); printf("avg turnaround time=%fn",at); getch(); } Round Robin: #include<stdio.h> #include<conio.h> void main() { int i,n,j,temp1=0,temp=0,b[100],s[100],w[100],t[100],max=0,tq,count=0; float aw=0,at=0; clrscr(); printf("enter no of processen"); scanf("%d",&n); printf("enter burst time of the proceessesn"); for(i=0;i<n;i++) { scanf("%d",&b[i]); s[i]=b[i]; } printf("enter quntum timen"); scanf("%d",&tq); for(i=0;i<n;i++) { if(max<b[i]) { max=b[i]; } } if(max%tq==0) { count=max/tq; } else { count=(max/tq)+1; }
  17. 17. for(i=0;i<count;i++) { for(j=0;j<n;j++) { if(s[j]>tq) { temp1=temp; temp=temp+tq; printf("process p%d exectued from %d to %dn",j,temp1,temp); s[j]=s[j]-tq; } else if(( s[j]>=0) && (s[j]<=tq)) { temp1=temp; temp=temp+tq; printf("process p%d exectued from %d to %dn",j,temp1,temp); t[j]=temp; s[j]=-1; } } } for(i=0;i<n;i++) { w[i]=t[i]-b[i]; } printf("process t bursttime t waitingtime t turnaroundtimen"); for(i=0;i<n;i++) { printf("p%d tt %d tt %d tt %dn",i,b[i],w[i],t[i]); } for(i=0;i<n;i++) { aw+=w[i]; at+=t[i]; } aw=aw/n; at=at/n; printf("avg waiting time=%fn",aw); printf("avg turnaroundtime=%f",at); getch(); } //SJF #include<conio.h> void main() { int i,j,b[100],q[100],t[100],n,w[100],temp; float aw=0,at=0; clrscr(); printf("enter no of processesn"); scanf("%d",&n); printf("enter process burst timesn"); for(i=0;i<n;i++) { scanf("%d",&b[i]); q[i]=i; } for(i=0;i<n;i++) {
  18. 18. for(j=i+1;j<n;j++) { if(b[i]>b[j]) { temp=b[i]; b[i]=b[j]; b[j]=temp; temp=q[i]; q[i]=q[j]; q[j]=temp; } } } w[0]=0; for(i=1;i<n;i++) { w[i]=w[i-1]+b[i-1]; } t[0]=b[0]; for(i=1;i<n;i++) { t[i]=t[i-1]+b[i]; } for(i=0;i<n;i++) { aw+=w[i]; at+=t[i]; } aw=aw/n; at=at/n; printf("nProcesstBurst timetwaiting timetTurnaround timen"); for(i=0;i<n;i++) { printf("P%dtt%dtt%dtt%dn",q[i],b[i],w[i],t[i]); } printf("avg waiting time=%fn",aw); printf("avg turn around time=%fn",at); getch(); } 1. Shortest job first (non- preemptive): #include<stdio.h> #include<unistd.h> #include<string.h> void main() { float avw=0,avt=0; struct pro { char name[20]; int b,w,ta; }a[10],temp; int n,i,j; printf("number of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("nenter name of process %d:",i+1); scanf("%s",a[i].name); printf("n enter the brust time of process %d:",i+1);
  19. 19. scanf("%d",&a[i].b); } for(i=0;i<n;i++) { for(j=i;j<n;j++) { if(a[i].b>a[j].b) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("nnametB.TtW.TtTAT"); for(i=0;i<n;i++) { if(i==0) { a[i].w=0; a[i].ta=a[i].b; } else { a[i].w=a[i-1].w+a[i-1].b; a[i].ta=a[i].w+a[i].b; } avw+=a[i].w; avt+=a[i].ta; printf("n%st%dt%dt%d",a[i].name,a[i].b,a[i].w,a[i].ta); } avt/=n; avw/=n; printf("naverage waiting time:%fn average true around time:%f",avw,avt);} 2. Shortest job first ( preemptive) #include<stdio.h> struct proc { int pid; int at,bt,wt,tat,rbt; }; struct proc p1[10]; int i,j,k,n,no,m; float atat=0.0,awt=0.0; int tbt=0; int minimum1(); int main() { int minv,locv,mins,locs; printf("nenter the number of processes:"); scanf("%d",&n); printf("nenter the proc information:"); printf("npid at bt"); for(i=0;i<n;i++) { p1[i].wt=0; p1[i].tat=0; scanf("%d%d%d",&p1[i].pid,&p1[i].at,&p1[i].bt); tbt+=p1[i].bt; p1[i].rbt=p1[i].bt; } printf("nthe proc information:"); printf("npid at bt"); for(i=0;i<n;i++)
  20. 20. { printf("n%d %d %d",p1[i].pid,p1[i].at,p1[i].bt); } minv=p1[0].at; locv=0; for(i=1;i<n;i++) { if(p1[i].at<minv) { locv=i; //tells min at process in locv minv=p1[i].at; } } printf("ngantt chart:"); for(i=minv;i<tbt+minv;i++) { no=minimum1(); printf("%d p[%d]",i,p1[no].pid); p1[no].rbt=p1[no].rbt-1; for(k=0;k<n;k++) { if(p1[k].rbt>0&&p1[k].at<=i&&k!=no) { p1[k].wt++; } } } printf("%d",tbt+minv); for(i=0;i<n;i++) { awt+=p1[i].wt; } awt=awt/n; for(i=0;i<n;i++) { p1[i].tat=p1[i].wt+p1[i].bt; atat+=p1[i].tat; } atat=atat/n; printf("n average wt=%f, average tat=%f",awt,atat); printf("nthe proc information:"); printf("npid at bt wt tat"); for(i=0;i<n;i++) { printf("n%d %d %d %d %d",p1[i].pid,p1[i].at,p1[i].bt,p1[i].wt,p1[i].tat); } } int minimum1() { int loc,z; int mini; mini=99; loc=-1; for(z=0;z<n;z++) { if(p1[z].rbt>0&&p1[z].at<=i&&p1[z].rbt<mini) { mini=p1[z].rbt; loc=z; } } return loc;
  21. 21. } 3. Priority(non-preemptive) #include<stdio.h> #include<unistd.h> void main() { int n,i,j; float avgw,avgt; struct process { char name[20]; int b,w,ta,p; }p[10],temp; printf("nnumber of processes:"); scanf("%d",&n); printf("nenter the details of the processes"); for(i=0;i<n;i++) { printf("nenter the name of process %d:",i+1); scanf("%s",p[i].name); printf("nbrust time of the process %d:",i+1); scanf("%d",&p[i].b); printf("npriority of the process %d:",i+1); scanf("%d",&p[i].p); } for(i=0;i<n;i++) { for(j=i;j<n;j++) { if(p[i].p>p[j].p) { temp=p[i]; p[i]=p[j]; p[j]=temp; } } } p[0].w=0; p[0].ta=p[0].b; avgw=p[0].w; avgt=p[0].ta; printf("nnamettbrust-time waiting-time priority turnaround-time"); printf("n%stt%dtt%dtt%dtt%d",p[0].name,p[0].b,p[0].w,p[0].p,p[0].ta); for(i=1;i<n;i++) { p[i].w=p[i-1].w+p[i-1].b; p[i].ta=p[i].w+p[i].b; avgw+=p[i].w; avgt+=p[i].ta; printf("n%stt%dtt%dtt%dtt%d",p[i].name,p[i].b,p[i].w,p[i].p,p[i].ta); } printf("naverage of waiting time of the processes is:%f",avgw/n); printf("naverage turn-arount time of the processes is:%f",avgt/n); } 4. Round-robin #include<stdio.h> #include<unistd.h> void main() { struct process { char name[20]; int b,w,ta,r,la;
  22. 22. }p[10]; int i,n,q,tbt=0,ct=0; float avgt=0,avgw=0; printf("nnumber of processes:"); scanf("%d",&n); printf("nenter the details of the processes"); for(i=0;i<n;i++) { printf("nenter the name of process %d:",i+1); scanf("%s",p[i].name); printf("nbrust time of the process %d:",i+1); scanf("%d",&p[i].b); tbt+=p[i].b; p[i].w=0; p[i].r=p[i].b; p[i].la=0; } printf("enter the value of the quanta:"); scanf("%d",&q); while(tbt!=ct) { for(i=0;i<n;i++) { if(p[i].r<q&&p[i].r!=0) { printf("%s---->%dn",p[i].name,p[i].r); p[i].w+=ct-p[i].la; ct+=p[i].r; p[i].la=ct; p[i].r=0; } else if(p[i].r!=0) { printf("%s---->%dn",p[i].name,q); p[i].w+=ct-p[i].la; ct+=q; p[i].la=ct; p[i].r-=q; } } } printf("nnamettbrust-time waiting-time turnaround-time"); for(i=0;i<n;i++) { p[i].ta=p[i].w+p[i].b; avgw+=p[i].w; avgt+=p[i].ta; printf("n%stt%dtt%dtt%d",p[i].name,p[i].b,p[i].w,p[i].ta); } printf("naverage of waiting time of the processes is:%fnaverage turn-arount time of the processes is:%f",avgw/n,avgt/n); } BANKERS ALGORITHM /*<--------------PREPROCESSING STATEMENTS--------------->*/ #include <stdio.h> #include <stdlib.h> /*<--------------MAIN FUNCTION--------------->*/ int main() { int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10]; int p, r, i, j, process, count;
  23. 23. count = 0; printf("Enter the no of processes : "); scanf("%d", &p); for(i = 0; i< p; i++) completed[i] = 0; printf("nnEnter the no of resources : "); scanf("%d", &r); printf("nnEnter the Max Matrix for each process : "); for(i = 0; i < p; i++) { printf("nFor process %d : ", i + 1); for(j = 0; j < r; j++) scanf("%d", &Max[i][j]); } printf("nnEnter the allocation for each process : "); for(i = 0; i < p; i++) { printf("nFor process %d : ",i + 1); for(j = 0; j < r; j++) scanf("%d", &alloc[i][j]); } printf("nnEnter the Available Resources : "); for(i = 0; i < r; i++) scanf("%d", &avail[i]); for(i = 0; i < p; i++) for(j = 0; j < r; j++) need[i][j] = Max[i][j] - alloc[i][j]; do { printf("n Max matrix:tAllocation matrix:n"); for(i = 0; i < p; i++) { for( j = 0; j < r; j++) printf("%d ", Max[i][j]); printf("tt"); for( j = 0; j < r; j++) printf("%d ", alloc[i][j]); printf("n"); } process = -1; for(i = 0; i < p; i++) { if(completed[i] == 0)//if not completed { process = i ; for(j = 0; j < r; j++) { if(avail[j] < need[i][j]) { process = -1; break; } } } if(process != -1) break; } if(process != -1) { printf("nProcess %d runs to completion!", process + 1); safeSequence[count] = process + 1; count++; for(j = 0; j < r; j++) {
  24. 24. avail[j] += alloc[process][j]; alloc[process][j] = 0; Max[process][j] = 0; completed[process] = 1; } } }while(count != p && process != -1); if(count == p) { printf("nThe system is in a safe state!!n"); printf("Safe Sequence : < "); for( i = 0; i < p; i++) printf("%d ", safeSequence[i]); printf(">n"); } else printf("nThe system is in an unsafe state!!"); } //Program for full duplex communication #include <iostream> using namespace std; #include<stdlib.h> #include <stdio.h> #include <unistd.h> int main() { int status, pid, pipefds[2],pipefds2[2],status2; char instring[20],instring2[20]; /* Create the pipe and return 2 file descriptors in the pipefds array */ /* This operation is done before the fork so that both processes will */ /* know about the same pipe, which will allow them to communicate. */ status = pipe(pipefds); status2=pipe(pipefds2); if (status == -1) { perror("Trouble"); exit(1); } if (status2 == -1) { perror("Trouble"); exit(1); } /* create child process; both processes continue from here */ pid = fork(); if (pid == -1) { perror("Trouble"); exit(2); } else if (pid == 0) /* child : sends message to parent*/ { /* close unused end of pipe */ /* because this process only needs to write */ close(pipefds[0]); close(pipefds2[1]); /* send 7 characters in the string, including end-of-string */ cout << "About to send a message: " << endl;
  25. 25. write(pipefds[1], "Hi Mom!", 7); read(pipefds2[0], instring2, 7); cout << "Just received a message that says: " << instring2 << endl; close(pipefds[1]); close(pipefds2[0]); exit(0); } else /* parent : receives message from child */ { /* close unused end of pipe */ /* because this process only needs to read */ close(pipefds[1]); /* read from the pipe */ write(pipefds2[1], "Hi pop!", 7); read(pipefds[0], instring, 7); cout << "Just received a message that says: " << instring << endl; close(pipefds[0]); close(pipefds2[1]); exit(0); } return 0; } //Program for half-duplex communication #include <iostream> using namespace std; #include<stdlib.h> #include <stdio.h> #include <unistd.h> int main() { int status, pid, pipefds[2],ch,i; char instring[20]; for(i=0;i<20;i++) instring[i]='0'; cout<<"-------------Program for half duplex communication-------------n"; cout<<"-------------Press 1 for child to parent communicationn"; cout<<"-------------Press 2 for parent to child communicationn"; scanf("%d",&ch); /* Create the pipe and return 2 file descriptors in the pipefds array */ /* This operation is done before the fork so that both processes will */ /* know about the same pipe, which will allow them to communicate. */ status = pipe(pipefds); if (status == -1) { perror("Trouble"); exit(1); } /* create child process; both processes continue from here */ pid = fork(); if (pid == -1) { perror("Trouble"); exit(2); }
  26. 26. switch(ch) { case 1: // child to parent communication if (pid == 0) /* child : sends message to parent*/ { /* close unused end of pipe */ /* because this process only needs to write */ close(pipefds[0]); /* send 7 characters in the string, including end-of-string */ cout << "About to send a message: " << endl; write(pipefds[1], "Hi father!", 10); close(pipefds[1]); exit(0); } else /* parent : receives message from child */ { /* close unused end of pipe */ /* because this process only needs to read */ close(pipefds[1]); /* read from the pipe */ read(pipefds[0], instring, 10); cout << "Just received a message that says: " << instring << endl; close(pipefds[0]); exit(0); } break; case 2: // parent to child communication if (pid > 0) { /* close unused end of pipe */ /* because this process only needs to write */ close(pipefds[0]); /* send 7 characters in the string, including end-of-string */ cout << "About to send a message: " << endl; write(pipefds[1], "Hi son!", 7); close(pipefds[1]); exit(0); } else { /* close unused end of pipe */ /* because this process only needs to read */ close(pipefds[1]); /* read from the pipe */ read(pipefds[0], instring, 7); cout << "Just received a message that says: " << instring << endl; close(pipefds[0]); exit(0); } break; default: cout<<"This is a problem"; }
  27. 27. return 0; }

×