CSE2005-Operating Systems Lab Assessment-2: Questions 1
CSE2005-Operating Systems Lab Assessment-2: Questions 1
CSE2005-Operating Systems Lab Assessment-2: Questions 1
Lab Assessment-2
Questions 1
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
int fact=1;
fact=fact*i;
int main() {
scanf("%d",&n);
pthread_t tid;
pthread_create (&tid,NULL,Factorial,(void*)n);
pthread_join(tid,NULL);
pthread_exit(NULL);
exit(0);
}
(c) Assume that two processes named client and server running in
the system. It is required that these two processes should
communicate with each other using shared memory concept. The
server writes alphabets from a..z to the shared memory .the client
should read the alphabets from the shared memory and convert it to
A…Z. Write a program to demonstrate the above mentioned
scenario. (Medium)
Code:-
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
main()
{
char c;
int shmid;
key_t key;
char *shm,
*s;
key = 5678;
if ((shmid = shmget(key, SHMSZ, IPC_CREAT |0666)) < 0)
{perror("shmget"); exit(1);
}
if ((shm = shmat(shmid, NULL, 0)) ==(char *) -1)
{
perror("shmat");
exit(1);
}
s = shm;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = NULL;
while (*shm != '*')
sleep(1);
exit(0)
}
#include<stdio.h>
#include<pthread.h>
int arr[50],n,i;
void *th()
{
int sum=0;
float average;
printf("enter your number :=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
average=sum/n;
printf("The average value is:%f",average);
}
void *th1()
{
int temp=arr[0];
for(int i=1;i<n;i++)
{
if(temp>arr[i])
{
temp=arr[i];
}
}
printf("\nThe Minimum value is:=%d",temp);
}
void *th2()
{
int temp=arr[0];
for(int i=1;i<n;i++)
{
if(temp<arr[i])
{
temp=arr[i];
}
}
printf("\nThe Maximum value is:=%d",temp);
}
int main()
{
int n,i;
pthread_t t1;
pthread_t t2;
pthread_t t3;
n=pthread_create(&t1,NULL,&th,NULL);
pthread_join(t1,NULL);
//printf("\n done and my value is %d",n);
n=pthread_create(&t2,NULL,&th1,NULL);
pthread_join(t2,NULL);
//printf("\n done and my value is %d",n);
n=pthread_create(&t3,NULL,&th2,NULL);
pthread_join(t3,NULL);
//printf("\n done and my value is %d",n);
}
Question 2 a
Implement the various process scheduling algorithms
such as FCFS, SJF, Priority (Non Preemptive)
a. FCFS
Code:-
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
struct Process {
int p_id;
int AT;
int BT;
int CT;
int TAT;
int WT;
};
void display(struct Process Process_Array[], int n){
for (int i=0; i<n;i++) {
int Pno=i+1;
printf("\n\nProcess %d\n",Pno);
printf("p_id=%d\n",Process_Array[i].p_id);
printf("AT=%d\n",Process_Array[i].AT);
printf("BT=%d\n",Process_Array[i].BT);
printf("CT=%d\n",Process_Array[i].CT);
printf("TAT=%d\n",Process_Array[i].TAT);
printf("WT=%d\n",Process_Array[i].WT);
}
}
void getStats(struct Process Process_Array[], int n){
printf("\nEnter the details of every process in increasing order of their arrival times.\n");
for (int i=0; i<n;i++) {
printf("Enter PID of Process %d = ",i+1);
scanf("%d",&Process_Array[i].p_id);
printf("Enter Arrival Time of Process %d = ",i+1);
scanf("%d",&Process_Array[i].AT);
printf("Enter Burst Time of Process %d = ",i+1);
scanf("%d",&Process_Array[i].BT);
}
}
void calcCT(struct Process Process_Array[],int n){
int timeline=0;
for (int i=0; i<n;i++) {
if (timeline<Process_Array[i].AT) {
timeline=Process_Array[i].AT;
}
timeline = timeline + Process_Array[i].BT;
Process_Array[i].CT = timeline;
}
}
void calcTAT(struct Process Process_Array[],int n){
for (int i=0; i<n;i++) {
Process_Array[i].TAT = Process_Array[i].CT - Process_Array[i].AT;
}
}
void calcWT(struct Process Process_Array[],int n){
for (int i=0; i<n;i++) {
Process_Array[i].WT = Process_Array[i].TAT - Process_Array[i].BT;
}
}
void calcAvgTAT(struct Process Process_Array[],int n){
float sumTAT=0;
for (int i=0; i<n;i++) {
sumTAT = sumTAT + Process_Array[i].TAT;
}
printf("\n\nAverage Turnaround time= %.3f", (sumTAT/n));
}
void calcAvgWT(struct Process Process_Array[],int n){
float sumWT=0;
for (int i=0; i<n;i++) {
sumWT = sumWT + Process_Array[i].WT;
}
printf("\n\nAverage Waiting time= %.3f\n", (sumWT/n));
}
int main(){
int n;
printf("\nEnter the number of processes in the system:");
scanf ("%d",&n);
struct Process Process_Array[100];
getStats(Process_Array, n);
calcCT(Process_Array, n);
calcTAT(Process_Array, n);
calcWT(Process_Array, n);
display(Process_Array, n);
calcAvgTAT(Process_Array, n);
calcAvgWT(Process_Array, n);
return 0;
}
b. PRIORITY
Code:-
#include<stdio.h>
int main()
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Priority:"); scanf("%d",&pr[i]);
//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{ pos=i;
for(j=i+1;j<n;j++)
if(pr[j]<pr[pos])
pos=j;
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
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];
tat[i]=bt[i]+wt[i];
avg_tat=total/n;
return 0;
c. SJF
Code:-
#include<stdio.h>
int main()
scanf("%d", &limit);
scanf("%d", &arrival_time[i]);
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
burst_time[9] = 9999;
smallest = 9;
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] >0)
smallest = i;
burst_time[smallest]--;
if(burst_time[smallest] == 0)
count++;
end = time + 1;
}
}
return 0;
Question 2 b
Implement the various process scheduling algorithms
such as Priority, Round Robin (preemptive)
ROUND ROBIN
Code:-
#include<stdio.h>
int main()
int count,j,n,time,remain,flag=0,time_quantum;int
wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
printf("Enter Arrival Time and Burst Time for Process Process Number %d:",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
scanf("%d",&time_quantum);
for(time=0,count=0;remain!=0;)
time+=rt[count];
rt[count]=0;
flag=1;
else if(rt[count]>0)
rt[count]-=time_quantum;
time+=time_quantum;
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
return 0;
}
b. PRIORITY
Code:-
#include<stdio.h>
int main()
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Priority:"); scanf("%d",&pr[i]);
//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{ pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
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];
tat[i]=bt[i]+wt[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
avg_tat=total/n;
return 0;
#include<string.h>
void main()
{int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
char ch;
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
scanf("%d",&n);
printf("Enter patient name, arrival time, execution time, priority :"); scanf("%s%d
%d%d",pn[i],&at[i],&et[i],&p[i]);
for(j=0; j<n; j+
+)
{
if(p[i]>p[j])
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
if(i==0)
st[i]=at[i];wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\t\t");
scanf("%d",&resource); //No. of Resources
int
avail[resource],max[process][resource],allot[process][resource],need[process][resource],co
mpleted[process];
for(i=0;i<process;i++)
completed[i]=0; //Setting Flag for uncompleted Process
for(i=0;i<resource;i++)
{
printf("\t\t");
scanf("%d",&instanc);
avail[i]=instanc; // Storing Available instances
}
for(i=0;i<process;i++)
{
printf("\n\t For P[%d]",i);
for(j=0;j<resource;j++)
{
printf("\t");
scanf("%d",&instanc);
max[i][j]=instanc;
}
}
printf("\n\t Enter no. of instances already allocated to process of a resource:\n");
for(i=0;i<process;i++)
{
printf("\n\t For P[%d]\t",i);
for(j=0;j<resource;j++)
{
printf("\t\t");
scanf("%d",&instanc);
allot[i][j]=instanc;
need[i][j]=max[i][j]-allot[i][j]; //calculating Need of each process
}
}
printf("\n\t Safe Sequence is:- \t");
while(count1!=process)
{
count2=count1;
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
{
if(need[i][j]<=avail[j])
{
k++;
}
}
if(k==resource && completed[i]==0 )
{
printf("P[%d]\t",i);
completed[i]=1;
for(j=0;j<resource;j++)
{
avail[j]=avail[j]+allot[i][j];
}
count1++;
}
k=0;
}
if(count1==count2)
{
printf("\t\t Stop ..After this.....Deadlock \n");
break;
}
}
getch();
}