Académique Documents
Professionnel Documents
Culture Documents
#include<stdio.h>
typedef struct process
{int id;
int wait;
int burst;
};
struct process pro[10];
struct process temp[20];
table(struct process pro[],int nop);
gantt(struct process pro[],int nop);
int getdata();
incwait(int tempburst,int id);
init();
int nop;
main()
{
struct process tmppro;
int choice,quantum;
int i,j,x,z,ret;
int ans,rrwt;
float rrawt,rratrt,fcfsawt,fcfsatrt,sjfatrt,sjfawt;
int procount,t,tburst[10];
do{printf("\t\t* * * * * * MENU * * * * * * \n");
printf("\t\t1.FCFS\n\t\t2.SJF\n\t\t3.Round Robin");
printf("\n\nPlease enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
init();
nop=getdata();
table(pro,nop);
gantt(pro,nop);
fcfsawt=0;
fcfsatrt=0;
for(i=nop-1;i>=0;i--)
{
pro[i].wait=0;
for(j=0;j<i;j++)
{
pro[i].wait=pro[i].wait+pro[j].burst;
}
}
for(i=0;i<nop;i++)
{
case 3:
{
init();
j=0;
ret=0;
nop=getdata();
for(i=0;i<nop;i++)
tburst[i]=pro[i].burst;
printf("\nPlease enter the time quantum : ");
scanf("%d",&quantum);
for(i=0;i<nop;i++)
{
if(pro[i].burst>0)
{
if(pro[i].burst>quantum)
{
temp[j].id=pro[i].id;
temp[j].burst=quantum;
pro[i].burst=pro[i].burst-quantum;
j++;
incwait(quantum,pro[i].id);
}
else
{
temp[j]=pro[i];
j++;
incwait(pro[i].burst,pro[i].id);
pro[i].burst=0;
}
}
if(i==nop-1)
{
for(z=0;z<nop;z++)
{
if(pro[z].burst!=0)
{
i=-1;break;
}
elseif(z==(nop-1)) ret=1;
}
}
if(ret==1) break;
}
table(temp,40);
gantt(temp,40);
printf("\n\nRound Robin awt and atrt : ");
rrawt=0;
rratrt=0;
for(i=0;i<nop;i++)
{
printf("\nP%d Wait Time : %d",pro[i].id,pro[i].wait);
printf("\nP%d Turn Around Time : %d",pro[i].id,(pro[i].wait+tburst[i]));
rrawt=rrawt+pro[i].wait;rratrt=rratrt+(pro[i].wait+tburst[i]);
}
printf("\n\nAWT of RR : %f",rrawt/nop);
printf("\n\nATRT of RR : %f",rratrt/nop);break;
}
}
printf("\n\nPress 1 to cont ...");
scanf("%d",&ans);
}
while(ans==1);
}
incwait(int tempburst,int id)
{
int i=0;
for(i=0;i<nop;i++)
{
if(pro[i].id!=id && pro[i].burst>0)
pro[i].wait=pro[i].wait+tempburst;
}
}
init()
{
int i;
nop=0;
for(i=0;i<10;i++)
{
pro[i].burst=0;
pro[i].id=0;
pro[i].wait=0;
}
for(i=0;i<20;i++)
{
temp[i].burst=0;
temp[i].id=0;
temp[i].wait=0;
}
}
table(struct process pro[],int nop)
{
int i;
printf("\n\n\n* * * * * * * PROCESS TABLE * * * * * * *\n\n");
printf("\nProcess Name Burst Time");
for(i=0;i<nop;i++)
{
if(pro[i].id==0)break;
printf("\nP%d\t\t%d\t\t%d",pro[i].id,pro[i].burst);
}
}
12.
#include<stdio.h>
struct process
{
int all[6],max[6],need[6],finished,request[6];
}p[10];
int avail[6],sseq[10],ss=0
int ch,i=0,j=0,k,ch1;
int violationcheck=0,waitcheck=0;
do
{
printf("\n\n\t 1. Input: ");
printf("\n\n\t 2. New Request: ");
printf("\n\n\t 3. Safe State or Not: ");
printf("\n\n\t 4. Print");
printf("\n\n\t 5. Exit");
printf("\n\n\t Enter an option : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
//Input
printf("\n\n\t Enter number of processes : ");
scanf("%d",&n);
printf("\n\n\t Enter the Number of Resources : ");
scanf("%d",&nor);
printf("\n\n\t Enter the Available Resources : ");
of available resources in the system
//no.
for(j=0;j<n;j++)
{
for(k=0;k<nor;k++)
{
if(j==0)
{
printf("\n\n\t For Resource type %d : ",k);
scanf("%d",&avail[k]);
//for each resource type
}
p[j].max[k]=0;
p[j].all[k]=0;
p[j].need[k]=0;
p[j].finished=0;
p[j].request[k]=0;
}
}
for(i=0;i<n;i++)
{
printf("\n\n\t Enter Max and Allocated resources for P%d : ",i);
for(j=0;j<nor;j++)
{
printf("\n\n\t Enter the Max of resource %d : ",j);
scanf("%d",&p[i].max[j]);
//maximum resources the process will need
printf("\n\n\t Allocation of resource %d
: ",j);
scanf("%d",&p[i].all[j]);
//resources allocated to the process
if(p[i].all[j]>p[i].max[j])
//obviously cannot be allocated more than a process needs
{
printf("\n\n\t Allocation should be less < or == max
resources");
}
else
p[i].need[j]=p[i].max[j]-p[i].all[j];
//how much more is needed
avail[j]=avail[j]-p[i].all[j];
//now how much is available in the system
}
}
break;
case 2:
//New request
violationcheck=0;
waitcheck=0;
printf("\n\n\t Requesting process id : ");
scanf("%d",&pid);
for(j=0;j<nor;j++)
{
printf("\n\n\t Number of Request for resource %d : ",j);
scanf("%d",&p[pid].request[j]);
if(p[pid].request[j]>p[pid].need[j])
//if a process requests more than it needs
violationcheck=1;
if(p[pid].request[j]>avail[j])
//if requests more than is available
waitcheck=1;
}
if (violationcheck==1)
printf("\n\n\t The Process Exceeds limited resources
Max Need: Terminated");
else if(waitcheck==1)
printf("\n\n\t Lack of resources : Process State Circular
Wait");
else
{
for(j=0;j<nor;j++)
{
avail[j]=avail[j]-p[pid].request[j];
//no. of available resources in the system get reduced
p[pid].all[j]=p[pid].all[j]+p[pid].request[j];
//resources allocated to the requesting process
p[pid].need[j]=p[pid].need[j]-p[pid].request[j]; //now
the no. of processes needed
}
ch1=safeseq();
if(ch1==0)
{
printf("\n\n\t Request Denied ");
for(j=0;j<nor;j++)
{
avail[j]=avail[j]+p[pid].request[j];
//resources again available because seq not safe
p[pid].all[j]=p[pid].all[j]-p[pid].request[j];
//resources unallocated
p[pid].need[j]=p[pid].need[j]+p[pid].request[j];
//now the process again needs the resources
}
}
else if(ch1==1)
printf("\n\n\t Request Committed ");
}
break;
case 3:
//Safe Seq or not
if(safeseq()==1)
printf("\n\n\t The System is in safe state ");
else
printf("\n\n\t The System is Not in safe state ");
break;
case 4:
//Print
printf("\n\n\t Number of processes : %d",n);
printf("\n\n\t Number of Resources : %d",nor);
printf("\n\n\t Pid \t Max \t Allocated \t Need ");
for(i=0;i<n;i++)
{
printf("\n\n\t P%d : ",i);
for(j=0;j<nor;j++)
printf(" %d ",p[i].max[j]);
printf("\t");
for(j=0;j<nor;j++)
printf(" %d ",p[i].all[j]);
printf("\t");
for(j=0;j<nor;j++)
printf(" %d ",p[i].need[j]);
}
printf("\n\n\t Available : ");
for(i=0;i<nor;i++)
printf("%d ",avail[i]);
break;
case 5:
break;
}
getch();
}while(ch!=5);
}
int safeseq()
{
int i,j,k,tk,tj;
ss=0;
for(j=0;j<nor;j++)
work[j]=avail[j];
for(j=0;j<n;j++)
p[j].finished=0;
for(tk=0;tk<nor;tk++)
{
for(j=0;j<n;j++)
{
if(p[j].finished==0)
{
check1=0;
for(k=0;k<nor;k++)
if(p[j].need[k]<=work[k])
by process is available
check1++;
if(check1==nor)
//freeing resources...
//process finished
}
sseq[ss]=j;
ss++;
}
}
}
}
check2=0;
for(i=0;i<n;i++)
if(p[i].finished==1)
check2++;
printf("\n\n\t");
if(check2>=n)
{
printf("\n\n\t Safe Sequence : ");
for(tj=0;tj<n;tj++)
printf(" P%d ",sseq[tj]);
return 1;
}
Else
Printf(\n\nt the system is not in safestate);
Return 0;
}
//sequence stored
- 1);
- 2);
- 1);
- 2);
}
/* random number generation upto 24 */
int random_num()
{
int temp;
temp = rand();
temp = temp%24;
MAX = temp;
return(MAX);
}
int main(int argc, char * argv[])
{
int FibNumber[25] = {0};
int j, temp,tmp,id,i = 0;
int n, tid, nthreads;
printf("Please Enter the number Range :");
scanf("%d",&n);
printf("\n");
omp_set_num_threads(2);
//Parallel region
# pragma omp parallel
{
printf("The number of threads are
%d\n",omp_get_num_threads());
# pragma omp for private (tid, tmp, FibNumber)
for(j = 1; j<=n; j++)
{
tmp = random_num();
/* Get thread number /
/ tid = omp_get_thread_num();