I have been trying to understand how the round robin concept and how the algorithm works. I have tried running this code in ubuntu, and i'm unable to get the answer that i wanted.
So based on the Round Robin Scheduling Algorithm; Let's say there are 3 processes. Where Processor 1 - Burst Time is 24, Processor 2 - Burst time is 3 and Processor 3 - Burst time is 3. and the Time Quantum is 3.
Based on this information, the waiting time for P1 is 6, P2 is 4 and P3 is 7. So the Turn Around Time is P1 is 30, P2 is 7 and P3 is 10.
Average Turn Around time is 15.6667 and The average waiting time is 5.667
Based on the code below, if i run it, it would return me; for waiting time - P1 is 6, P2 is 3 and P3 is 6, Turn around time P1 is 30, P2 is 6, P3 is 9.
And the Average Turn Around time is 15.0000 and The average waiting time is 5.000
I'm unable to figure out the error. Can any one help me with this problem and provide an explanation to error and solution?
#include<stdio.h>
#include<curses.h>
int main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
clear();
printf("Enter the no of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];
att+=tat[i];
awt+=wa[i];
}
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");
for(i=0;i<n;i++)
{
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
}
printf("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
getch();
}
The code is returning the right answer.
All the processes arrived at time 0.
P1 - 0-3 21 units remaining
P2 - 3-6 0 units remaining
P3 - 6-9 0 units remaining
P1 - 9-30 0 units remaining
P1 waited 6 units, P2 waited 3 units and P3 waited 6 units.
Note that Waiting time is the amount of time a process is waiting without being executed after given to the scheduler. Turnaround time is the total time a process took to complete its execution (Waiting time + Burst time).
Avg waiting time: (6+3+6) / 3 = 15
Average Turnaround time = (30+6+9) / 3 = 15
Related
I have implemented Sieve of Eratosthenes to find out the prime numbers using openMp method for various term values and thread.
Here is my code
// Type your code here, or load an example.
#include <stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<omp.h>
void SieveOfEratosthenes(int n)
{
bool prime[n + 1];
memset(prime, true, sizeof(prime));
const int end_p=sqrt(n);
#pragma omp parallel for
for (int p = 2; p <= end_p; p++)
{
bool prime_p;
#pragma omp atomic read
prime_p=prime[p];
if (prime_p == true)
{
for (int i = p * p; i <= n; i += p)
{
#pragma omp atomic write
prime[i] = false;
}
}
}
for (int p = 2; p <= n; p++)
if (prime[p])
printf("%d ", p);
}
int main(int argc, char* argv[]){
int procs;
int term =100;
double start,finish;
procs = omp_get_num_procs();
omp_set_num_threads(procs);
start = omp_get_wtime();
SieveOfEratosthenes(term);
finish = omp_get_wtime();
printf("Elapsed time = %e seconds\n", finish - start);
return 0;
}
I am going to show the output of elapsed time for various terms and threads
Here is the result:
1.term=100
a)Thread 1 , elapsed time= 3.879014e-04 seconds
b)Thread 2, elapsed time = 3.887471e-04 seconds
c)Thread 4, elapsed time = 3.742063e-04 seconds
d)Thread 8, elapsed time = 3.988100e-04 seconds
e)Thread 16, elapsed time = 5.262811e-04 seconds
2.term = 100000
a)Thread 1, elapsed time = 6.131708e-03
b)Thread 2, elapsed time = 4.231855e-03
c)Thread 4, elapsed time = 4.193152e-03
d)Thread 8, elapsed time = 6.109135e-03
e)Thread 16, elapsed time = 4.225969e-03
3.term = 100000000
a)Thread 1, elapsed time = 1.237387e+01
b)Thread 2, elapsed time = 1.184531e+01
c)Thread 4, elapsed time = 1.160130e+01
d)Thread 8, elapsed time = 1.128761e+01
e)Thread 16, elapsed time = 1.18116e+01seconds
Now I can see from the statistics when term is 100 elapsed time increased for thread 8,16(1-d),e))
When term = 100000, elapsed time is increased for thread 8(2 d)), then again decrease.
When term = 100000000, elapsed time is increased in for number of threads 16 than number of threads 8.
My confusion is when the task divided on number of threads elapsed time should decrease. I mean if the number of threads increased elapsed time decreased. However, I saw a variation in my result.
It would be great if someone help me to find out what I missed in my code.
Thank you.
Use omp_set_num_threads(thread_count); in your main to set the number of threads.
Exclude the printout from speed measurement, as it has nothing to do with the calculation and it is the slowest part of your code.
Use dynamic schedule #pragma omp parallel for schedule(dynamic). The load balance is not optimal using the default schedule, because the bigger the p the lower the workload.
Your code has a race condition, which is undefined behavior in C language.
I saw that atomic operation did not have any effect on the code. I got
same result with them without them.
Please understand that even if you happen to get correct results on a given compiler/architecture (e.g. x86/64), it does not necessarily mean that your code is correct. Your code should work properly (and as designed) using any compilers/computer architectures.
After these changes, I see speedup:
term=100000000;
Thread 1, Elapsed time = 6.903768e-01 seconds
Thread 8, Elapsed time = 2.813646e-01 seconds
I am tasked with creating a simple program for a train schedule. I need to have a user input how many trains will be running and how many times the trains will run for the day. For each run I need to tell 1 train at a time to go and the others to standby until they have all traveled. I'm trying to use a nested do while loop to achieve this but not having much luck. Is this possible or am I going about it wrong from the start?
If the user inputs 2 trains will run 2 times my output will read:
Train 1 go. All other trains standby.
Train 2 go. All other trains standby.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int numTrains = 0;
int timeBlocks = 0;
int numRuns = 1;
int timeBlocksran = 0;
printf("How many trains will be running today?\n");
scanf("%d", &numTrains);
printf("How many times will the trains run today?\n");
scanf("%d", &timeBlocks);
printf("OK. There will be %d trains running %d times today. Let's get them started. All Aboard.\n", numTrains, timeBlocks);
do
{
timeBlocksran++;
printf("Time Block %d\n", timeBlocksran);
do
{
printf("Train %d go. All other trains standby.\n", numRuns);
numRuns++;
} while (numRuns <= numTrains);
} while (timeBlocksran <= timeBlocks);
return 0;
}
If the user inputs 2 trains will run 2 times I want my output to read:
TIME BLOCK 1
Train 1 go. All other trains standby.
Train 2 go. All other trains standby.
TIME BLOCK 2
Train 1 go. All other trains standby.
Train 2 go. All other trains standby.
#include<stdio.h>
typedef struct nonpresjf
{
int at,bt,ft,tat,wt,id;
}nonpresjf;
nonpresjf p[20],p1[20],aux;
int main()
{
int i,limit,nextval,m,min,j;
float mediaperm=0,mediaespera=0;
p[0].wt=p[0].tat=0;
int n=3;
p[0].at=3;
p[1].at=5;
p[2].at=7;
p[0].bt=9;
p[1].bt=3;
p[2].bt=2;
limit=p[0].at;
for (i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(p[i].at<p[j].at){
aux=p[j];
p[j]=p[i];
p[i]=aux;
}
}
}
for(i=0;i<n;i++)
limit+=p[i].bt;
for(i=0;i<n;i++)
p1[i]=p[i];
printf("\n\n Diagram: ");
printf("%d",p[0].at);
nextval=p[0].at;
m=0;
do
{
min = 9999;
for(i=0;p[i].at<=nextval && i<n ;i++)
if(p[i].bt<min && p[i].bt>0)
{
m=i;
min=p[i].bt;
}
nextval+=p[m].bt;
p[m].bt=0;
printf("->P%d->%d",m,nextval);
if(p[m].bt==0)
{
p[m].ft=nextval;
p[m].tat=p[m].ft-p[m].at;
p[m].wt=p[m].tat-p1[m].bt;
p[0].tat+=p[m].tat;
p[0].wt+=p[m].wt;
}
}while(nextval<limit);
p[0].tat=p[0].tat/n;
p[0].wt=p[0].wt/n;
printf("\n\n-----------TABLE-------------------\n");
printf("\nProcess\tArrival\tBurst\tTurnAround\tWaitTime\n");
for(i=0;i<n;i++)
printf("\nP%d\t%d\t%d\t\t%d\t\t%d\n",p[i].id,p[i].at,p1[i].bt,p[i].tat,p[i].wt);
printf("\n\n--------------------------------\n");
}
return 0;
}
Due to the fact that this is an important assignment and that my other post wasn't answered this is another attemp at programing this.
It seems to be working if arrival times are in order but it goes on an infinite loop randomly with burst times. I debugged it an has something to do with nextval<limit nextval never hits limit this happens sometimes.
I think the sort by aT doesn't work properly too, I was suggested in my other post to sort it starting in j=i+1 but that way the theoretically correct output is wrong.
Input:
number of process: 3
Arrival Time0: 3
Arrival Time1: 5
Arrival Time2: 7
Burst time 0: 9
Burst time 1: 3
Burst time 2: 2
Expected output.
-----------TABLE-------------------
Process Arrival Burst TurnAround WaitTime
P0 3 9 9 0
P1 5 3 12 9
P2 7 2 7 5
Actual output.
-----------TABLE-------------------
Process Arrival Burst TurnAround WaitTime
P0 3 9 9 4
P1 5 3 7 9
P2 7 2 12 5
If input was:
number of process: 3
Arrival Time0: 5
Arrival Time1: 2
Arrival Time2: 7
Burst time 0: 3
Burst time 1: 9
Burst time 2: 2
I'd get an infinite loop
i have this in the main :
#define N 23
start-time=clock();
readData(c); // just read a matrix of integer size N (in this case matrix 23*23)
lp (c,d); // resolve it by cplex with a time limit cplex command 1h 30
final-time=clock();
time = (final_time -start-time) *0.001;
printf("\n CPU = %f sec\n\n", time);
the problem shows:
Default row names c1, c2 ... being created.
solution status is Feasible
obj. value: 5557
gap : 1.1697
CPU = 10800.494141 sec
why the time is so large? did the main() spend another 1 h just to read a matrix size 23*23 !!!!!!
clock
The value returned is the CPU time used so far as a clock_t; to get
the number of seconds used, divide by CLOCKS_PER_SEC
I am new in stackoverflow and i am sorry if i make mistakes.
I am starter in C language and i have one project what it needs to subtact a weight per second.For example
Weight: 50kg
Subtract per second 4%
i found this code
while(waitFor(1))
{
*weight=(*weight)-(*weight)*0.4;
}
void waitFor(int secs)
{
int retTime;
retTime = time(0) + secs; // Get finishing time.
while (time(0) < retTime); // Loop until it arrives.
}
But i dont want to wait x seconds to finish. I want faster solution. Any ideas
**Note: I want to know how much seconds need to make weight 0
Sleep command it is not working in my computer.
**
For cleaning and disinfecting of the pool dropped into the water of a chemical
a solid body. This solid body upon contact with water
immediately begins to dissolve, losing weight equal to 4% by mass per second.
If the dissolution rate of the chemical remains constant, to implement program
will accept the weight of the solid body in grams and displays after how
time will completely dissolve. The time is displayed in a "hours: minutes: seconds".
For example, if the dissolution time is 3,740 seconds, displaying 01: 02: 20.
To calculate the time you need to implement a function which accepts
gram and return the three time parameters ie
hours, minutes and seconds. Note that the time printed on the main
function.
you can use sleep(int) function in loop it will wait suspend the process up to the integer value.
while((1) && weight > 0)
{
sleep(1);
*weight=(*weight)-(*weight)*0.4;
}
it will wait for 1 seconds and subtraction will made, it will run continuously
Edit:
To find out the number of seconds required for the weight to reach 0:
unsigned seconds = 0;
while(*weight != 0){
*weight -= *weight * 0.04
seconds++;
//in case you have the patience to attend:
sleep(1000); //in milliseconds => 1 second
}
Please note that weight is considered to be a pointer to an integer type.