CSE2005-Operating Systems Lab Assessment-2: Questions 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

CSE2005-Operating Systems

Lab Assessment-2
Questions 1

(a) Process and Thread Management Write a program to create a


thread and perform the following (Easy)  Create a thread runner
function  Set the thread attributes  Join the parent and thread 
Wait for the thread to complete
Code:-
#include <pthread.h>
#include <unistd.h>
#define NUM_THREADS 5
void *wait(void *t) {
int i;
long tid;
tid = (long)t;
sleep(1);
printf("Sleeping \n");
printf("Thread id : %d\n" ,tid);
pthread_exit(NULL);
}
int main () {
int rc;
int i;
pthread_t threads[NUM_THREADS];
pthread_attr_t attr;
void *status;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for( i = 0; i < NUM_THREADS; i++ ) {
printf("parent creating thread, %d\n",i);
rc = pthread_create(&threads[i], &attr, wait, (void *)i );
if (rc) {
printf("Error:unable to create thread, %d\n",rc);
exit(-1);
}
}
pthread_attr_destroy(&attr);
for( i = 0; i < NUM_THREADS; i++ ) {
rc = pthread_join(threads[i], &status);
if (rc) {
printf ("Error:unable to join, %d\n" ,rc);
exit(-1);
}
printf("Parent Process: completed thread id :%d",i) ;
printf(" exiting with status :%d\n",status);
}
printf ("Parent: program exiting.\n");
pthread_exit(NULL);
}
(b) Write a program to create a thread to find the factorial of a
natural number ‘n’.
Code:-
#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <pthread.h>

void *Factorial(void* var){

printf("\nThis is thread process with pid:%d\n",getpid());

int n = (int *)var;

int fact=1;

for (int i=n; i>0; i--){

fact=fact*i;

printf("Factorial of %d is: %d\n", n, fact);

int main() {

int n; printf("\nThis is parent process with pid:%d",getpid());

printf("\nBefore Thread Creation.\nThread will now be Created...");

printf("\nEnter the number whose Factorial you want to calculate: ");

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

d) Write a multithreaded program that calculates various statistical


values for a list of numbers. This program will be passed a series of
numbers on the command line and will then create three separate
worker threads. One thread will determine the average of the
numbers, the second will determine the maximum value, and the
third will determine the minimum value. For example, suppose your
program is passed the integers 90 81 78 95 79 72 85 , the program
will report the average value as 82. The minimum value as 72. The
maximum value as 95. The variables representing the average,
minimum, and maximum values will be stored globally. The worker
threads will set these values, and the parent thread will output the
values once the workers have exited. (High)
Code:-

#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("Enter Total Number of Process:"); scanf("%d",&n);

printf("\nEnter Burst Time and Priority\n"); for(i=0;i<n;i++)

printf("\nP[%d]\n",i+1); printf("Burst Time:"); scanf("%d",&bt[i]);

printf("Priority:"); scanf("%d",&pr[i]);

p[i]=i+1; //contains process number

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

avg_wt=total/n; //average waiting time total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time"); for(i=0;i<n;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;

printf("\n\nAverage Waiting Time=%d",avg_wt);

printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;

c. SJF
Code:-
#include<stdio.h>

int main()

int arrival_time[10], burst_time[10], temp[10];


int i, smallest, count = 0, time, limit;

double wait_time = 0, turnaround_time = 0, end;

float average_waiting_time, average_turnaround_time;

printf("\nEnter the Total Number of Processes:\t");

scanf("%d", &limit);

printf("\nEnter Details of %d Processesn", limit);

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

printf("\nEnter Arrival Time:\t");

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

printf("Enter Burst Time:\t");

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

temp[i] = burst_time[i];

burst_time[9] = 9999;

for(time = 0; count != limit; time++)

smallest = 9;

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

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;

wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];

turnaround_time = turnaround_time + end - arrival_time[smallest];

}
}

average_waiting_time = wait_time / limit;

average_turnaround_time = turnaround_time / limit;

printf("\n\nAverage Waiting Time:\t%lf\n", average_waiting_time);

printf("Average Turnaround Time:\t%lf\n", average_turnaround_time);

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

printf("Enter Total Process:\t ");

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

printf("Enter Time Quantum:\t");

scanf("%d",&time_quantum);

printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");

for(time=0,count=0;remain!=0;)

if(rt[count]<=time_quantum && rt[count]>0)

time+=rt[count];

rt[count]=0;

flag=1;

else if(rt[count]>0)

rt[count]-=time_quantum;

time+=time_quantum;

if(rt[count]==0 && flag==1)


{

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;

printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);

printf("Avg Turnaround Time = %f",turnaround_time*1.0/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("Enter Total Number of Process:"); scanf("%d",&n);

printf("\nEnter Burst Time and Priority\n"); for(i=0;i<n;i++)

printf("\nP[%d]\n",i+1); printf("Burst Time:"); scanf("%d",&bt[i]);

printf("Priority:"); scanf("%d",&pr[i]);

p[i]=i+1; //contains process number

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

avg_wt=total/n; //average waiting time total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time"); for(i=0;i<n;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;

printf("\n\nAverage Waiting Time=%d",avg_wt);

printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;

(c) Consider a corporate hospital where we have n number of


patients waiting for consultation. The amount of time required to
serve a patient may vary, say 10 to 30 minutes. If a patient arrives
with an emergency,he /she should be attended immediately before
other patients, which may increase the waiting time of other
patients. If you are given this problem with the following algorithms
how would you devise an effective scheduling so that it optimizes
the overall performance such as minimizing the waiting time of all
patients. [Single queue or multi-level queue can be used]. Consider
the availability of single and multiple doctors • Assign top priority for
patients with emergency case, women, children, elders, and
youngsters. • Patients coming for review may take less time than
others. This can be taken into account while using SJF.
1. FCFS
2. SJF (primitive and non-pre-emptive) (High)
Code:-
#include<stdio.h>

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

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

scanf("%d",&n);

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

printf("Enter patient name, arrival time, execution time, priority :"); scanf("%s%d

%d%d",pn[i],&at[i],&et[i],&p[i]);

for(i=0; i<n; 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);

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

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

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

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("\nAverage waiting time is:%f",awt);

printf("\nAverage turnaroundtime is:%f",ata);

(D)Simulate with a program to provide deadlock avoidance of


Banker’s Algorithm including Safe state and additional resource
request (High).
Code:-
#include<stdio.h>
#include<conio.h>
void main()
{
int process,resource,i,j,instanc,k=0,count1=0,count2=0; //count,k variables are taken for
counting purpose
printf("\n\t Enter No. of Process:-\n");
printf("\t\t");
scanf("%d",&process); //Entering No. of Processes
printf("\n\tEnter No. of Resources:-\n");

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

printf("\n\tEnter No. of Available Instances\n");

for(i=0;i<resource;i++)
{
printf("\t\t");
scanf("%d",&instanc);
avail[i]=instanc; // Storing Available instances
}

printf("\n\tEnter Maximum No. of instances of resources that a Process need:\n");

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();
}

You might also like