Dear Readers/Members
The site name imraan-prrec.blogspot.com will be changed to gnitcsehyd.blogspot.com effectively from 30-4-2016.
The site name imraan-prrec.blogspot.com will be changed to gnitcsehyd.blogspot.com effectively from 30-4-2016.
//RSA ALGORITHM
#include <stdio.h>
int modulo(int e, int n, int pt)
{
int h;
if(e==0)
return 1;
else if(e==1)
return pt%n;
else
{
h = modulo(e/2, n, pt);
if(e%2==0)
return ((h*h)%n)%n;
else
return ((h*h)*(pt%n))%n;
}
}
int main()
{
int p, q, n, z, e, d, pt,ct;
printf("Enter p & q values <prime numbers>: ");
scanf("%d%d", &p, &q);
n = p*q;
z = (p-1)*(q-1);
printf("\nEnter a relative prime number to %d i.e., e value: ",z);
scanf("%d", &e);
d = 1;
while((d*e)%z!=1)
d++;
printf("d value is: %d\n", d);
printf("Keys are: <e, n>:(%d, %d)\t<d, n>:(%d, %d)",e,n,d,n);
printf("\n\nEnter message<pt> such that pt < %d : ",n);
scanf("%d", &pt);
ct = modulo(e,n,pt);
printf("\nCipher Text: %d\n",ct);
pt = modulo(d,n,ct);
printf("Decrypted Text: %d",pt);
return 0;
}
/* SAMPLE OUTPUT
Enter p & q values <prime numbers>: 11 7
Enter a relative prime number to 60 i.e., e value: 13
d value is: 37
Keys are: <e, n>:(13, 77) <d, n>:(37, 77)
Enter message<pt> such that pt < 77 : 5
Cipher Text: 26
Decrypted Text: 5
*/
/PROGRAM FOR DIJKSTRA'S ALGORITHM #include <stdio.h> #include <conio.h> #define GRAPHSIZE 2048 #define INFINITY GRAPHSIZE*GRAPHSIZE #define MAX(a, b) ((a > b) ? (a) : (b)) int e; /* The number of nonzero edges in the graph */ int n; /* The number of nodes in the graph */ long dist[GRAPHSIZE][GRAPHSIZE]; /* dist[i][j] is the distance between node i and j; or 0 if there is no direct connection */ long d[GRAPHSIZE]; /* d[i] is the length of the shortest path between the source (s) and node i */ int prev[GRAPHSIZE]; /* prev[i] is the node that comes right before i in the shortest path from the source to i*/ void printD() { int i; printf("Distances:\n"); for (i = 1; i <= n; ++i) printf("%d\t", i); printf("\n"); for (i = 1; i <= n; ++i) { printf("%ld\t", d[i]); } printf("\n"); } /* Prints the shortest path from the source to dest. * dijkstra(int) MUST be run at least once BEFORE this is called */ void printPath(int dest) { if (prev[dest] != -1) printPath(prev[dest]); printf("%d ", dest); } void dijkstra(int s) { int i, k, mini; int visited[GRAPHSIZE]; for (i = 1; i <= n; ++i) { d[i] = INFINITY; prev[i] = -1; /* no path has yet been found to i */ visited[i] = 0; /* the i-th element has not yet been visited */ } d[s] = 0; for (k = 1; k <= n; ++k) { mini = -1; for (i = 1; i <= n; ++i) if (!visited[i] && ((mini == -1) || (d[i] < d[mini]))) mini = i; visited[mini] = 1; for (i = 1; i <= n; ++i) if (dist[mini][i]) if (d[mini] + dist[mini][i] < d[i]) { d[i] = d[mini] + dist[mini][i]; prev[i] = mini; } } } void main() { int i, j; int u, v, w; //clrscr(); FILE *fin = fopen("dist.txt", "r"); fscanf(fin, "%d", &e); for (i = 0; i < e; ++i) for (j = 0; j < e; ++j) dist[i][j] = 0; n = -1; for (i = 0; i < e; ++i) { fscanf(fin, "%d%d%d", &u, &v, &w); dist[u][v] = w; n = MAX(u, MAX(v, n)); } fclose(fin); dijkstra(1); printD(); printf("\n"); for (i = 1; i <= n; ++i) { printf("Path to %d: ", i); printPath(i); printf("\n"); } getch(); } /*SAMPLE OUTPUT ____________________ inputfile: dist.txt 10 1 2 10 1 4 5 2 3 1 2 4 3 3 5 6 4 2 2 4 3 9 4 5 2 5 1 7 5 3 4 _____________________
OUTPUT
Distances:
1 2 3 4 5
0 7 8 5 7
Path to 1: 1
Path to 2: 1 4 2
Path to 3: 1 4 2 3
Path to 4: 1 4
Path to 5: 1 4 5
*/
//PROGRAM FOR DISTANCE VECTOR ROUTING ALGORITHM
#include<stdio.h>
#include<conio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
printf("\nEnter number of nodes : ");
scanf("%d",&n);
printf("\nEnter the cost matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
rt[i].dist[j]=dmat[i][j];
rt[i].from[j]=j;
}
do
{
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<n;i++)
{
printf("\n\nNODE %d ROUTING TABLE",i+1);
printf("\nNode\tDistance\tViaNode\n");
for(j=0;j<n;j++)
{
printf("\t\n %d\t %d\t\t %d",j+1,rt[i].dist[j],rt[i].from[j]+1);
}
}
printf("\n\n");
return 0;
getch();
}
/*SAMPLE OUTPUT
**********INPUT*********
Enter number of nodes : 5
Enter the cost matrix :
0 4 2 6 99
4 0 99 99 99
2 99 0 3 99
6 99 3 0 2
99 99 99 2 0
::::::OUTPUT :::::
NODE 1 ROUTING TABLE
Node Distance ViaNode
1 0 1
2 4 2
3 2 3
4 5 3
5 7 4
NODE 2 ROUTING TABLE
Node Distance ViaNode
1 4 1
2 0 2
3 6 1
4 9 1
5 11 1
NODE 3 ROUTING TABLE
Node Distance ViaNode
1 2 1
2 6 1
3 0 3
4 3 4
5 5 4
NODE 4 ROUTING TABLE
Node Distance ViaNode
1 5 3
2 9 1
3 3 3
4 0 4
5 2 5
NODE 5 ROUTING TABLE
Node Distance ViaNode
1 7 4
2 11 4
3 5 4
4 2 4
5 0 5
=> A Generator is shared between the Sender and the Receiver.
=> Sender will convert Dataword into Codeword using the Generator and send it
to the Receiver.
=> Receiver upon receiving the Codeword, will check for errors in the received
Codeword with the help of the Generator.
=> If no error is found, receiver can extract Dataword from the Codeword.
Sender Side:
Generator/Divisor: 1100000001011 (Total 13-bits)
Dataword: 10101 (5-bits)
Extended Dataword: 10101000000000000 (17-bits: Dataword + 12 0's)
1100000001011 ) 10101000000000000 ( 11001
1100000001011
_____________
x1101000010110
1100000001011
_____________
x0010000111010
0000000000000
_____________
x0100001110100
0000000000000
_____________
x1000011101000
1100000001011
_____________
x100011100011
=> So, CRC Bits are: 100011100011
=> Now Codeword is 10101100001110100 (17-bits: Dataword + CRC bits)
=> Sender will send this Codeword to the Receiver.
Receiver Side:
Codeword: 10101100011100011
Generator/Divisor: 1100000001011
1100000001011) 10101100011100011 ( 11001
1100000001011
_____________
x1101100001010
1100000001011
_____________
x0011000000010
0000000000000
_____________
x0110000000101
0000000000000
_____________
x1100000001011
1100000001011
_____________
0000000000000
=> Reached Codeword contain no errors and now extract Dataword from Codeword
=> Dataword is 10101 [[by removing (Generator_Size - 1) bits from Codeword from Right]]
// PROGRAM FOR CYCLIC REDUNDANCY CHECK
#include<stdio.h>
int main()
{
int ds, dws, i, j, k, c=0; // ds-DivisorSize: dws-DataWordSize:
int div[100], dw[100], ta[100]; // div-Divisor: dw-DataWord: ta-TemporaryArray
//TAKING INPUT - DIVISOR
printf("Enter the size of divisor: ");
scanf("%d", &ds);
printf("Enter the divisor in bit pattern: ");
for(i=0; i<ds ; i++)
scanf("%d", &div[i]);
//TAKING INPUT - DATAWORD
printf("Enter the size of the DataWord: ");
scanf("%d",&dws);
printf("Enter the DATAWORD in bit pattern: ");
for(i=0; i<dws; i++)
scanf("%d",&dw[i]);
//ADDING ZEROS TO THE DATAWORD i.e., WE GOT EXTENDED DATAWORD HERE
for(i=dws; i<(dws+ds-1); i++)
dw[i] = 0;
//PRINTING THE EXTENDED DATAWORD
printf("Data after inserting zeros or <EXTENDED DATAWORD> : ");
for(i=0; i<(dws+ds-1); i++)
printf("%d", dw[i]);
//COPYING DATAWORD INTO TEMPORARY ARRAY
for(i=0; i<(dws+ds-1); i++)
ta[i] = dw[i];
//GENERATING THE REDUNDANT BITS
for(i=0; i!= dws; i++)
{
if(ta[i]==1)
for(j=0, k=i; j<ds; j++, k++)
ta[k] = ta[k]^div[j];
}
//ADDING REDUNDANT BITS TO THE DATAWORD
for(i=dws; i<(dws+ds-1); i++)
dw[i] = ta[i];
//PRINTING THE CODEWORD
printf("\n\nThe Generated CODEWORD is: ");
for(i=0; i<(dws+ds-1); i++)
printf("%d", dw[i]);
//COPYING CODEWORD INTO TEMPORARY ARRAY
for(i=0; i<(dws+ds-1); i++)
ta[i] = dw[i];
//DIVIDING CODEWORD WITH DIVISOR AT RECEIVERS END
for(i=0; i!= dws; i++)
{
if(ta[i]==1)
for(j=0, k=i; j<ds; j++, k++)
ta[k] = ta[k]^div[j];
}
printf("\nThe Remainder @ receiver's End is: ");
for(i=dws; i<(dws+ds-1); i++)
printf("%d",ta[i]);
//CHECKING IF THERE ARE ANY 1s IN THE REMAINDER
for(i=dws; i<(dws+ds-1); i++)
{
if(ta[i]!=0)
c++;
}
if(c==0)
printf("\nThe Codeword hasn't been altered::Message sent SUCCESSFULLY :)");
else
printf("\nThe Codeword has been altered :(");
return 0;
}
/* EXAMPLE OUTPUT
Enter the size of divisor: 4
Enter the divisor in bit pattern: 1 0 1 1
Enter the size of the DataWord: 5
Enter the DataWord in bit pattern: 1 0 1 1 1
Data after inserting zeros or <EXTENDED DATAWORD> : 10111000
The Generated CODEWORD is: 10111011
The Remainder @ receiver's End is: 000
The Codeword hasn't been altered::Message sent SUCCESSFULLY :)
*/
// PROGRAM FOR CHARACTER STUFFING
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j=1;
char str1[100], str2[100], str3[100];
//clrscr();
printf("Enter a string: ");
scanf("%s",&str1);
//GENERATING ENCODED MESSAGE
str2[0] = '@';
for(i=0; i< strlen(str1); i++)
{
if( str1[i]=='@' || str1[i] == '#')
{
str2[j] = '#'; j++;
}
str2[j] = str1[i]; j++;
}
str2[j] = '@'; j++;
str2[j] = '\0';
printf("Encoded message is: %s", str2);
// DECODED MESSAGE or ACTUAL MESSAGE
j=0;
for(i=1; i<(strlen(str2)-1);i++)
{
if(str2[i]=='#')
i++;
str3[j]=str2[i];
j++;
}
str3[j] ='\0';
printf("\nDecoded or actual message is: %s", str3);
getch();
}
//PROGRAM FOR BIT-STUFFING #include<stdio.h> #include <string.h> int main() { char ed[100]; //ed - Entered Data char ta[100]; //ta - Temporary Array char dd[100]; //dd - Decoded Data char ecd[100]={'0','1','1','1','1','1','1','0'}; //ecd - EnCoded Data int n, i, j=0, count=0; printf("Enter the string:"); scanf("%s",&ed); printf("\nEntered bit pattern: %s",ed); //ENCODING for(i=0; i<strlen(ed); i++) { if(ed[i]=='1') count++; else count = 0; ta[j]=ed[i]; j++; if(count==5) { ta[j] = '0'; j++; count = 0; } } ta[j] = '\0'; strcat(ta,ecd); strcat(ecd,ta); printf("\nEncoded Data: %s",ecd); //DECODING j=0; count =0; for(i=8; i<strlen(ecd)-8; i++) { if(ecd[i]=='1') count ++; else count = 0; dd[j] = ecd[i]; j++; if(count==5) { i++; count=0; } } dd[j] ='\0'; printf("\nDecoded Data: %s", dd); return 0; }
//OPTIMAL PAGE REPLACEMENT ALGORITHM
#include <stdio.h>
#include <conio.h>
int main()
{
int n, rss, fa[20], rsa[50], ta[20]; //n-No_of_Frames
//rss->Reference_String_Size::fa->Frame_Array
//rsa->Reference_String_Array::ta->Temporary_Array
int i,j,k, d,pfc=0,npf, cp,ff=0;
//d-distance[How soon a page will be used again?]
//cp->Current_Position :: ff->Frames_Filled ::pfc->Page_Fault_Count
//npf:NO_Page_Faults [0-False, 1-True]
printf("Enter number of frames: ");
scanf("%d", &n);
printf("Enter number of pages in reference string: ");
scanf("%d", &rss);
printf("Enter Reference string:\n");
for(i=0; i<rss; i++)
scanf("%d",&rsa[i]);
for(i=0;i<n;i++)
{
fa[i]=-1;
ta[i]=999;
}
printf("\nCURRENT_PAGE\t\tPAGES_IN_FRAME\t\tPAGE_FAULT_OCCURED?\n");
for(i=0; i<rss; i++)
{
printf("\n\t%d\t\t",rsa[i]);
npf=0;
for(j=0;j<n;j++) //Checking for the page in FRAME ARRAY
{
if(fa[j]==rsa[i])
{
npf = 1;
printf("\t\t\t\tNO");
break;
}
}
if(npf==0) // if page fault occurs
{
pfc++;
if(ff<n)
{
fa[ff]=rsa[i];
ff++;
}
else
{
for(k=0;k<n;k++)
ta[k]=999;
for(k=0; k<n; k++) //finding how near a page is
{
d = 0; // d-> distance
for(j=i+1;j<rss;j++)
{
if(fa[k]==rsa[j])
{
ta[k]=d;
break;
}
else
d++;
}
}
cp=0;
for(j=1;j<n;j++)
{
if(ta[cp]<ta[j])
cp=j; //cp->current position
}
fa[cp] = rsa[i];
}
for(j=0;j<n;j++)
printf("%d\t",fa[j]);
printf("\tYES");
}
}
printf("\nTotal no of pagefaults: %d",pfc);
return 0;
}
//LRU PAGE REPLACEMENT ALGORITHM
#include <stdio.h>
#include <conio.h>
int main()
{
int n, rss, fa[20], rsa[50], ta[20]; //n-No_of_Frames
//rss->Reference_String_Size::fa->Frame_Array
//rsa->Reference_String_Array::ta->Temporary_Array
int i,j,k,pfc=0,npf, cp;
//cp->Current_Position ::pfc->Page_Fault_Count
//npf:NO_Page_Faults [0-False, 1-True]
printf("Enter number of frames: ");
scanf("%d", &n);
printf("Enter number of pages in reference string: ");
scanf("%d", &rss);
printf("Enter Reference string:\n");
for(i=0; i<rss; i++)
scanf("%d",&rsa[i]);
for(i=0;i<n;i++)
fa[i]=-1;
for(i=0;i<n;i++)
ta[i]=n-(i+1);
printf("\nCURRENT_PAGE\t\tPAGES_IN_FRAME\t\tPAGE_FAULT_OCCURED?\n");
for(i=0; i<rss; i++)
{
printf("\n\t%d\t\t",rsa[i]);
npf=0;
for(j=0;j<n;j++) //Checking for the page in FRAME ARRAY
{
if(fa[j]==rsa[i])
{
npf = 1;
for(k=0;k<n;k++)
ta[k]++;
ta[j]=0;
printf("\t\t\t\tNO");
break;
}
}
if(npf==0) // if page fault occurs
{
pfc++;
cp=0;
for(j=1;j<n;j++)
if(ta[cp]<ta[j])
cp=j; //cp->current position
fa[cp] = rsa[i];
for(k=0;k<n;k++)
ta[k]++;
ta[cp]=0;
for(j=0;j<n;j++)
printf("%d\t",fa[j]);
printf("\tYES");
}
}
printf("\nTotal no of pagefaults: %d",pfc);
return 0;
}
//FIFO PAGE REPLACEMENT ALGORITHM
#include <stdio.h>
#include <conio.h>
int main()
{
int n, rss, fa[20], rsa[50]; //n-No_of_Frames
//rss->Reference_String_Size::fa->Frame_Array
//rsa->Reference_String_Array::ta->Temporary_Array
int i,j,k,pfc=0,npf, cp=0;
//cp->Current_Position :: ff->Frames_Filled ::pfc->Page_Fault_Count
//npf:NO_Page_Faults [0-False, 1-True]
printf("Enter number of frames: ");
scanf("%d", &n);
printf("Enter number of pages in reference string: ");
scanf("%d", &rss);
printf("Enter Reference string:\n");
for(i=0; i<rss; i++)
scanf("%d",&rsa[i]);
for(i=0;i<n;i++)
fa[i]=-1;
printf("\nCURRENT_PAGE\t\tPAGES_IN_FRAME\t\tPAGE_FAULT_OCCURED?\n");
for(i=0; i<rss; i++)
{
printf("\n\t%d\t\t",rsa[i]);
npf=0;
for(j=0;j<n;j++) //Checking for the page in FRAME ARRAY
{
if(fa[j]==rsa[i])
{
npf = 1;
printf("\t\t\t\tNO");
break;
}
}
if(npf==0) // if page fault occurs
{
pfc++;
fa[cp] = rsa[i];
cp=(cp+1)%n;
for(j=0;j<n;j++)
printf("%d\t",fa[j]);
printf("\tYES");
}
}
printf("\nTotal no of pagefaults: %d",pfc);
return 0;
}
//PROGRAM FOR PRIORITY CPU SCHEDULING ALGORITHM" WITH NO-PRE_EMPTION & ARRIVAL TIMES
#include<stdio.h>
#include<string.h>
int main(void)
{
//VARIABLE DECLARATION
char pn[20][20], c[20][20]; //PN-PROGRAM NAMES C-A TEMPORARY ARRAY
int n,i,j,at[20], bt[20], pt[20], wt[20],ct[20],tat[20];
//bt-BURST TIME ;pt-PRIORITY;wt-WAITING TIME; tat-TURN AROUND TIME
int temp1, temp2, temp3, count=0,twt=0;//,tat=0;
printf("Enter number of processes:");
scanf("%d", &n);
printf("Enter <ProcessName> <ArrivalTime> <BurstTime> <Priority> :\n");
//TAKING INPUT VALUES i.e., PROCESS-NAMES, ARRIVAL-TIMES AND BURST-TIMES
for(i=0; i<n; i++)
scanf("%s%d%d%d",&pn[i],&at[i],&bt[i],&pt[i]);
//SCHEDULING THE PROCESSES ACCORDING TO SJF
for(i=0 ; i<n; i++)
{
for(j=i+1; j<n; j++)
if (
//IF THERE IS NO PROCESS IN MAIN MEMORY,
//SORT PROCESSES ACCORDING TO ARRIVAL TIMES &
//IF WE GOT PROCESSES WITH SAME ARRIVAL TIME SORT THEM BY PRIORITY
( (i==0||count<1)&&(at[i]>at[j]||(at[i]==at[j]&&pt[i]>pt[j])) )
//IF WE GOT ONLY ONE PROCESS IN MAIN MEMORY AFTER COMPLETION OF THE CURRENT PROCESS
|| (count == 1 && ct[i-1]>=at[j])
//IF WE GOT MORE THAN ONE PROCESS IN MAIN MEMORY, SORT THEM BY PRIORITY
|| ((ct[i-1]>=at[j]&&pt[i]>pt[j]))// || (ct[i-1]<at[i]&&ct[i-1]>=at[j]))
)
//SWAPPING PROCESSES
{
temp1 = bt[i];
bt[i] = bt[j];
bt[j] = temp1;
temp2 = at[i];
at[i] = at[j];
at[j] = temp2;
temp3 = pt[i];
pt[i] = pt[j];
pt[j] = temp3;
strcpy(c[i],pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],c[i]);
}
if(i==0 || count<1)
ct[i] = at[i] + bt[i];
else
ct[i] = ct[i-1] + bt[i];
wt[i] = ct[i] - (at[i] + bt[i]);
tat[i] = ct[i] - at[i];
count = 0 ;
for(j=i+1; j<n; j++)
if(ct[i]>=at[j])
count++;
}
//PRINTING THE VALUES AFTER ALL PREOCESSES COMPLETED
printf("S.N.\tPN\tAT\tBT\tPri\tCT\tWT\tTAT\n");
for(i=0; i<n; i++)
printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n",(i+1),pn[i],at[i],bt[i],pt[i],ct[i],wt[i],tat[i]);
} //END OF MAIN
/* EXAMPLE OUTPUT
Enter number of processes:4
Enter <ProcessName> <ArrivalTime> <BurstTime> <Priority> :
p1 3 2 4
p2 2 1 6
p3 10 2 3
p4 2 3 2
S.N. PN AT BT Pri CT WT TAT
1 p4 2 3 2 5 0 3
2 p1 3 2 4 7 2 4
3 p2 2 1 6 8 5 6
4 p3 10 2 3 12 0 2
*/
//PROGRAM FOR ROUND ROBIN "CPU SCHEDULING ALGORITHM" WITH ARRIVAL TIMES
#include<stdio.h>
#include<string.h>
int main(void)
{
//VARIABLE DECLARATION
char pn[20][20], c[20][20]; //PN-PROGRAM NAMES
int n,i,j,k,l, tq, at[20], bt[20], rbt[20], wt[20],tt[20],ct[20]; //bt-BURST TIME ; wt-WAITING TIME; tat-TURN AROUND TIME
int temp1, temp2, temp3, count=0,twt=0, tn;//,tat=0;
printf("Enter <Number_of_Processes & Time_Quantum:\n");
scanf("%d%d", &n, &tq);
printf("Enter PN, AT, BT:\n");
//TAKING INPUT VALUES i.e., PROCESS-NAMES, ARRIVAL-TIMES, BURST-TIMES
for(i=0; i<n; i++)
scanf("%s%d%d",&pn[i],&at[i],&bt[i]);
for(i=0; i<n; i++)
rbt[i]=bt[i];
//SCHEDULING THE PROCESSES ACCORDING TO SJF
for(i=0;i<n;i++)
{
for(j=i+1; j<n;j++)
{
//SORTING BASED ON ARRIVAL TIMES
if(at[i]>at[j])
{
temp1 = bt[i];
bt[i] = bt[j];
bt[j] = temp1;
temp2 = at[i];
at[i] = at[j];
at[j] = temp2;
temp3 = rbt[i];
rbt[i] = rbt[j];
rbt[j] = temp3;
strcpy(c[i],pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],c[i]);
}
} //END OF J FOR-LOOP
}//END OF I FOR-LOOP
tn = at[0];
label:
for(i=0; i<n; i++)
{
if(at[i]>tn) i--;
if(rbt[i]>0)
{
if(rbt[i]>tq)
{
tn += tq;
rbt[i] -= tq;
}
else
{
tn += rbt[i];
rbt[i] = 0;
ct[i] = tn;
count++;
}
}
}
if(count<n) goto label;
//CALCULATING WAITING TIME & TAT
for(i=0;i<n;i++)
{
wt[i] = ct[i]-at[i]-bt[i];
twt += wt[i];
}
//PRINTING THE VALUES AFTER ALL PREOCESSES COMPLETED
printf("S.N.\tPN\tAT\tBT\tCT\tWT\n");
for(i=0; i<n; i++)
printf("%d\t%s\t%d\t%d\t%d\t%d\n",(i+1),pn[i],at[i],bt[i],ct[i],wt[i]);
printf("Total waiting time:%d", twt);
} //END OF MAIN
//PROGRAM FOR SHORTEST-JOB-FIRST(SJF) "CPU SCHEDULING ALGORITHM" WITH PRE_EMPTION
#include<stdio.h>
#include<conio.h>
int main()
{
int n,at[10], bt[10], ct[10], wt[10], tn =0, c, ta[10],tat[10];
//at-ArritvalTime::br-BurstTime::ct-CompletionTime::ta-TemporaryArray
//wt-WaitingTime::tat-TurnAroundTime::tn-CurrentTime(TimeNow)
// int i, j, k, tot, pc=0, pointer = 0, lp=-1;//lp-Last-executedProcess
int i, k, tot, pc=0, pointer = 0, lp=-1;//lp-Last-executedProcess
char pn[10][10];
printf("Enter the number of processes: ");
scanf("%d",&n);
printf("Enter <ProcessName> <ArrivalTime> <BurstTime>\n");
for(i=0;i<n;i++)
scanf("%s%d%d",&pn[i],&at[i],&bt[i]);
for(i=0; i<n; i++)
{
ct[i] = -1;
ta[i] = bt[i];
}
while(pc!=n)
{
k=0;
c = 0;
for(i=0; i<n; i++)
{
if(ct[i]<0 && at[i]<=tn)
c++;
}
if(c==0)
tn++;
else
{
pointer = 0;
while(at[pointer]>tn || ct[pointer]>0)
pointer++;
for(k=pointer+1; k<n; k++)
if( (at[k]<=tn && ct[k]<0) &&
( (bt[pointer]==bt[k] && k==lp) || bt[pointer]>bt[k] ) )
pointer = k;
if(ct[pointer]<0)
{
bt[pointer]--;
tn++;
if(bt[pointer]==0)
{
ct[pointer] = tn;
wt[pointer] = ct[pointer] - ( at[pointer]+ ta[pointer] );
tat[pointer] = ct[pointer] - at[pointer];
pc++;
}
}
lp = pointer;
}
}
printf("\nPN\tAT\tBT\tCT\tWT\tTAT\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\t%d\t%d\t%d\n",pn[i],at[i],ta[i],ct[i],wt[i],tat[i]);
return 0;
}
/* SAMPLE OUTPUT
Enter the number of processes: 6
Enter <ProcessName> <ArrivalTime> <BurstTime>
p1 3 1
p2 1 4
p3 10 6
p4 25 10
p5 1 3
p6 12 3
PN AT BT CT WT TAT
p1 3 1 5 1 2
p2 1 4 9 4 8
p3 10 6 19 3 9
p4 25 10 35 0 10
p5 1 3 4 0 3
p6 12 3 15 0 3
*/
//PROGRAM FOR SHORTEST-JOB-FIRST(SJF) "CPU SCHEDULING ALGORITHM" WITHOUT PRE_EMPTION
#include<stdio.h>
#include<conio.h>
int main()
{
int at[10], bt[10], ct[10], wt[10], ta[10], tat[10];
//at-ArritvalTime::br-BurstTime::ct-CompletionTime::ta-TemporaryArray
//wt-WaitingTime::tat-TurnAroundTime::tn-CurrentTime(TimeNow)
int n, i, k, pc=0, pointer = 0, tn =0, c;//pc-ProcessesCompleted
char pn[10][10]; //pn-ProcessName
printf("Enter the number of processes: ");
scanf("%d",&n);
printf("Enter <ProcessName> <ArrivalTime> <BurstTime>\n");
for(i=0;i<n;i++)
scanf("%s%d%d",&pn[i],&at[i],&bt[i]);
for(i=0; i<n; i++)
{
ct[i] = -1;
ta[i] = bt[i];
}
while(pc!=n)
{
c = 0;
for(i=0; i<n; i++)
if(ct[i]<0 && at[i]<=tn)
c++;
if(c==0)
tn++;
else
{
pointer = 0;
while(at[pointer]>tn || ct[pointer]>0)
pointer++;
for(k=pointer+1; k<n; k++)
if(at[k]<=tn && ct[k]<0 && bt[pointer]>bt[k])
pointer = k;
if(ct[pointer]<0)
{
tn=tn+bt[pointer];
bt[pointer] = 0;
ct[pointer] = tn;
wt[pointer] = ct[pointer] - ( at[pointer]+ ta[pointer] );
tat[pointer] = ct[pointer] - at[pointer];
pc++;
}
}
}
printf("\nPN\tAT\tBT\tCT\tWT\tTAT\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\t%d\t%d\t%d\n",pn[i],at[i],ta[i],ct[i],wt[i],tat[i]);
return 0;
}
/* EXAMPLE OUTPUT
Enter the number of processes: 6
Enter <ProcessName> <ArrivalTime> <BurstTime>
p1 3 2
p2 6 5
p3 1 1
p4 6 2
p5 15 3
p6 20 10
PN AT BT CT WT TAT
p1 3 2 5 0 2
p2 6 5 13 2 7
p3 1 1 2 0 1
p4 6 2 8 0 2
p5 15 3 18 0 3
p6 20 10 30 0 10
*/
//PROGRAM FOR SHORTEST-JOB-FIRST(SJF) "CPU SCHEDULING ALGORITHM" WITH NO-PRE_EMPTION & ARRIVAL TIMES
#include<stdio.h>
#include<string.h>
int main(void)
{
//VARIABLE DECLARATION
char pn[20][20], c[20][20]; //PN-PROGRAM NAMES C-A TEMPORARY ARRAY
int n,i,j,at[20], bt[20], wt[20],ct[20],tat[20]; //bt-BURST TIME ; wt-WAITING TIME; tat-TURN AROUND TIME
int temp1, temp2, count=0,twt=0;//,tat=0;
printf("Enter number of processes:");
scanf("%d", &n);
printf("Enter PN, AT, BT:\n");
//TAKING INPUT VALUES i.e., PROCESS-NAMES, ARRIVAL-TIMES AND BURST-TIMES
for(i=0; i<n; i++)
scanf("%s%d%d",&pn[i],&at[i],&bt[i]);
//SCHEDULING THE PROCESSES ACCORDING TO SJF
for(i=0 ; i<n; i++)
{
for(j=i+1; j<n; j++)
if (
//IF THERE IS NO PROCESS IN MAIN MEMORY,
//SORT PROCESSES ACCORDING TO ARRIVAL TIMES &
//IF WE GOT PROCESSES WITH SAME ARRIVAL TIME SORT THEM BY BURST TIMES
( (i==0||count<1)&&(at[i]>at[j]||(at[i]==at[j]&&bt[i]>bt[j])) )
//IF WE GOT ONLY ONE PROCESS IN MAIN MEMORY AFTER COMPLETION OF THE CURRENT PROCESS
|| (count == 1 && ct[i-1]>=at[j])
//IF WE GOT MORE THAN ONE PROCESS IN MAIN MEMORY, SORT THEM BY BURST TIMES
|| ((ct[i-1]>=at[j]&&bt[i]>bt[j]))// || (ct[i-1]<at[i]&&ct[i-1]>=at[j]))
)
//SWAPPING PROCESSES
{
temp1 = bt[i];
temp2 = at[i];
bt[i] = bt[j];
at[i] = at[j];
bt[j] = temp1;
at[j] = temp2;
strcpy(c[i],pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],c[i]);
}
if(i==0 || count<1)
ct[i] = at[i] + bt[i];
else
ct[i] = ct[i-1] + bt[i];
wt[i] = ct[i] - (at[i] + bt[i]);
tat[i] = ct[i] - at[i];
count = 0 ;
for(j=i+1; j<n; j++)
if(ct[i]>=at[j])
count++;
}
//PRINTING THE VALUES AFTER ALL PREOCESSES COMPLETED
printf("S.N.\tProcess-Name\tArrival-Time\tBurst-Time\tCT\tWT\tTAT\n");
for(i=0; i<n; i++)
printf("%d\t\t%s\t\t%d\t\t%d\t%d\t%d\t%d\n",(i+1),pn[i],at[i],bt[i],ct[i],wt[i],tat[i]);
} //END OF MAIN
/* EXAMPLE OUTPUT
Enter number of processes:6
Enter PN, AT, BT:
p1 3 2
p2 6 5
p3 1 1
p4 6 2
p5 15 3
p6 20 10
S.N. Process-Name Arrival-Time Burst-Time CT WT TAT
1 p3 1 1 2 0 1
2 p1 3 2 5 0 2
3 p4 6 2 8 0 2
4 p2 6 5 13 2 7
5 p5 15 3 18 0 3
6 p6 20 10 30 0 10
*/
//PROGRAM FOR FIRST-COME-FIRST-SERVE "CPU SCHEDULING ALGORITHM"
#include<stdio.h>
#include<string.h>
int main(void)
{
//VARIABLE DECLARATION
char pn[20][20], c[20][20]; //PN-PROGRAM NAMES
int n,i,j,at[20], bt[20], wt[20],tat[20], ct[20]; //bt-BURST TIME ; wt-WAITING TIME; tat-TURN AROUND TIME
int twt=0, ttat=0, temp1, temp2;
printf("Enter number of processes:");
scanf("%d", &n);
//TAKING INPUT VALUES i.e., PROCESS-NAMES, ARRIVAL-TIMES AND BURST-TIMES
printf("Enter <Process-name> <Arrival-time> <Burst-time> for processes:\n", (i+1));
for(i=0; i<n; i++)
scanf("%s%d%d",&pn[i],&at[i],&bt[i]);
//SORT THE PROCESSES ACCORDING TO ARRIVAL TIMES
for(i=0;i<n;i++)
{
for(j=i+1; j<n;j++)
{
if(at[i]>at[j])
{
temp1 = bt[i];
temp2 = at[i];
bt[i] = bt[j];
at[i] = at[j];
bt[j] = temp1;
at[j] = temp2;
strcpy(c[i],pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],c[i]);
}
}
if(i==0) ct[0]=at[0]+bt[0];
if(i>0)
{
if(at[i]>ct[i-1])
ct[i]=at[i]+bt[i];
else
ct[i]=ct[i-1]+bt[i];
}
}
//CALCULATING WAITING TIME & TAT
wt[0]=0;
tat[0]=ct[0]-at[0];
for(i=1;i<n;i++)
{
tat[i] = ct[i]-at[i];
wt[i] = tat[i]-bt[i];
twt += wt[i];
ttat += tat[i];
}
//PRINTING THE VALUES AFTER ALL PREOCESSES COMPLETED
printf("S.N.\tPN\tAT\tBT\tCT\tWT\tTAT\n");
for(i=0; i<n; i++)
printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\n",(i+1),pn[i],at[i],bt[i],ct[i],wt[i],tat[i]);
printf("Total waiting time:%d\n", twt);
printf("Total Turn Around Time:%d", ttat);
}