SJF Algorithm in C not working properly - c

#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

Related

Round-Robin Scheduling Algorithm in OS

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

Standard C function is slower at the first call, how to solve this properly?

I want to make timing tests for learning how to benchmark using "time.h". But I noticed the first test is always longer.
0 1 2 3 4 5 6 7 8 9
time 0.000138
0 1 2 3 4 5 6 7 8 9
time 0.000008
0 1 2 3 4 5 6 7 8 9
time 0.000007
If I want to do several tests in the same main() function the results will be unreliable.
Here is the stupid code who prints the output above.
#include <stdio.h>
#include <time.h>
const int COUNT = 10;
void test() {
clock_t start = clock();
for(int i = 0; i < COUNT; i++) {
printf("%d ", i);
}
printf("\ntime %lf\n", (double)(clock() - start) / (double)CLOCKS_PER_SEC );
}
int main() {
test();
test();
test();
return 0;
}
I solved this by ignoring the first "test" function. Also, writing a first "printf" who prints some integer before the tests works too. But I guess it's not a proper solution.
CPU has cache. When code and data are not in cache, the code takes longer to run.
It's standard practice to discard the result of first run (or first few runs) when measuring performance. It's sometimes called "cache warmup".

Why is my programme showing Segmentation Fault on Ubuntu but working fine on Geeks IDE?

Experts, this is the program I made for First Come First Serve Scheduling in C language.
For the inputs like -
4
0 0 0 0
1 2 3 4
and inputs like -
5
0 1 2 3 4
4 3 1 2 5
My program is giving Segmentation fault (Core dumped) when I tried running it on my Ubuntu but it worked fine on GeeksForGeeks IDE.
And for the inputs like -
6
4 3 2 1 2 3
4 3 2 3 4 5
My program is working fine on my Ubuntu as well as on GeeksForGeeks IDE.
#include<stdio.h>
void main(){
int n,i;
printf("\nEnter the number of jobs:\n");
scanf("%d",&n);
int at[n],bt[n],at_copy[n];
printf("\nEnter the arrival time:\n");
for(i=0;i<n;i++){
scanf("%d",&at[i]);
at_copy[i]=at[i];
}
printf("\nEnter the burst time:\n");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
int priority[n],min=at_copy[0],k,j;
for(j=0;j<n;j++){
min=at_copy[0];
for(i=0;i<n;i++){
if(at_copy[i]<min){
min=at_copy[i];
k=i;
}
}
at_copy[k]=999;
priority[j]=k;
}
int ct[n],wt[n],tat[n],t=0;
for(i=0;i<n;i++){
if(at[i]<t)
k=0;
else
k=at[i];
t+=bt[i]+k;
ct[i]=t;
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[i];
}
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for(i=0;i<n;i++){
printf("P%d\t%d\t%d\t%d\t%d\t%d\n",i+1,at[i],bt[i],ct[i],tat[i],wt[i]);
}
}
In the output, I am expecting a table like structure displaying the arrival time, burst time, completion time, burst time, turnaround time and waiting time of all the processes.
Here:
int priority[n],min=at_copy[0],k,j;
for(j=0;j<n;j++){
min=at_copy[0];
for(i=0;i<n;i++){
if(at_copy[i]<min){
min=at_copy[i];
k=i;
}
}
at_copy[k]=999;
...
Imagine what happens, when at_copy[0] is the minimal value. Then, the condition atcopy[i]<min is never true and k remains uninitialized resulting in an out-of-bounds access here:
at_copy[k]=999;
You have to initialize k with 0, since you assume in the first iteration, that at_copy[0] is the minimum.
int priority[n],min=at_copy[0],k=0,j;
With Geeks-IDE, you might have been "lucky" and k had the value 0 without being initialized.

Trouble with extra zero in bubble sort in C

I am trying to sort, say, 10 integers in ascending order using bubble sort in C.
This is the code I am using:
#include<stdio.h>
void main()
{
int x[20],i,j;
float temp;
printf("Enter 10 values: \n");
for(i=0;i<10;i++)
{
printf("x[%d]: ",i+1);
scanf("%d",&x[i]);
}
for(i=0;i<10-1;i++)
{
for(j=0;j<=10-i-1;j++)
{
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
printf("The sorted list in ascending order is \n");
for(i=0;i<10;i++)
{
printf("%5d",x[i]);
}
}
The problem is that I am getting an extra zero as output despite giving only non-zero entries as my 10 integers.
This is the input and the corresponding output I am getting. Note that the second output gives a zero and the value 19 has disappeared from the sorted list:
Enter 10 values:
x[1]: 4
x[2]: 2
x[3]: 7
x[4]: 4
x[5]: 8
x[6]: 2
x[7]: 3
x[8]: 9
x[9]: 13
x[10]: 19
The sorted list in ascending order is
2 0 2 3 4 4 7 8 9 13
--------------------------------
Process exited after 44.89 seconds with return value 5
Press any key to continue . . .
I am unable to locate my precise mistake.
for(i=0;i<10-1;i++)
{
for(j=0;j<10-i-1;j++)
{
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
the bug is when i = 0 then inner loop condition is j<=10-0-1=9, then you compare a[j] and a[j+1], but a[j+1] could be a[10], the array starts from 0 to 19, and you only initialized the first 10 integers(0-9), the left 10 integers(10-19) is 0, so there will be an extra 0 in your result.
change j<=10-i-1 to j<10-i-1, and the code will run as you expect.

Null distribution of Rank tests

I need to compute the distribution of a test statistic in C. The test statistic is based on ranks. So rather than generating observations and rank them I think I can use natural numbers and there all possible distinct combinations for my computation. So i have written a code in C. But it is not displaying the expected output, just some symbols I suppose. When I ran it in an online compiler it displayed a Segmentation fault. Please help me rectify the errors. Any suggestions will be appreciated. The following is the code that I have written.
#include<stdio.h>
#include<conio.h>
void main()
{
int i1,i2,i3,j1,j2,j3,i,k,p,m1,n1,combo1,combo2,combo3;
int fcombo1=0, fcombo2=0, fcombo3=0;
m1=3;
n1=3;
p=1;
int factorial(int n,int r);
for(i1=1;i1<=6;i1++)
for(i2=i1+1;i2<=6;i2++)
for(i3=i2+1;i3<=6;i3++)
for(j1=1;j1<=6;j1++)
if(j1!=i1&&j1!=i2&&j1!=i3)
for(j2=1;j2<=6;j2++)
if(j2!=i1&&j2!=i2&&j2!=i3&&j2>j1)
for(j3=1;j3<=6;j3++)
if(j3!=i1&&j3!=i2&&j3!=i3&&j3>j2)
{
for(i=1;i<=3;i++)
for(k=0;k<=1;k++)
{
if(i==1)
{
combo1=factorial(i-1,1)*factorial(m1-1,p)*factorial(i1-i,p-k)*factorial(n1-i1+i,p+k+1);
fcombo1=fcombo1+combo1;
}
else if(i==2)
{
combo2=factorial(i-1,p)*factorial(m1-1,p)*factorial(i2-i,p-
k)*factorial(n1-i2+i,p+k+1);
fcombo2=fcombo2+combo2;
}
else if(i==3)
{
combo3=factorial(i-1,p)*factorial(m1-1,p)*factorial(i3-i,p-
k)*factorial(n1-i3+i,p+k+1);
fcombo3=fcombo3+combo3;
}
printf("%3d%3d%3d%3d%3d%3d%3d%3d%3d\n",i1,i2,i3,j1,j2,j3,
fcombo1,fcombo2,fcombo3);
}
}
getch();
}
int factorial(int n,int r)
{
if(n<r)
return(0);
else if(n==r)
return(1);
else if(r==1)
return(n);
else
return(factorial(n-1,r)+factorial(n-1,r-1));
}
Output:
1 2 3 4 5 6 0 0 0
1 2 3 4 5 6 0 0 0
1 2 3 4 5 6 0 0 0
1 2 3 4 5 6 0 2 0
1 2 3 4 5 6 0 2 0
1 2 3 4 5 6 0 2 4
1 2 4 3 5 6 0 2 4
1 2 4 3 5 6 0 2 4
1 2 4 3 5 6 0 2 4
1 2 4 3 5 6 0 4 4
1 2 4 3 5 6 0 4 8
Segmentation fault
First of all, the way you design the program uses 10 levels of for loops, it is usually a terrible idea to do that, think about how you can refactor it.
Second, for the specific problem, it lies in the factorial function, if r goes to less than 0, e.g. for the case of factorial(1, 0), the function will recurse infinitely, because none of the termination condition matches. As a result, the program aborted by segfault because of stack overflow in the function. So depending on how you need to calculate, add a terminate condition in the function, for example:
int factorial(int n,int r)
{
if(n<r)
return(0);
else if(n==r)
return(1);
else if(r<=1)
return(n);
else
return(factorial(n-1,r)+factorial(n-1,r-1));
}
or
if (r <= 0) abort(); // this should never happen
I just wanted to know why i am getting a segmentation fault....
I guess the reason is because of Stack Overflow. You are calling a recursive function factorial within a 8 levels of nesting. so this will be in the millions of calls.
Each time you call a function, some memory is allocated in the stack. Over millions of nested calls, the stack will overflow and cause a segmentation fault

Resources