-
Notifications
You must be signed in to change notification settings - Fork 7
/
lottery.c
141 lines (121 loc) · 3.2 KB
/
lottery.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void main()
{
int n,i,j,k,temp=65,flag=0;
char process[20];
int brust[20],priority[20],pos;
int time=0,quantom=1,tbt=0;
int z=0,lottery[20],ticket[20][20],q=0;
printf("Enter Number Of Process: ");
scanf("%d",&n);
if(n<=0)
{
printf("\n\n:::: Invalid Value Of Number Of Process ::::");
exit(0);
}
for(i=0;i<n;i++)
{
process[i] = temp;
temp+=1;
}
for(i=0;i<n;i++)
{
printf("\nEnter The Brust Time For Process %c: ",process[i]);
scanf("%d",&brust[i]);
printf("Enter The Priority For Process %c(b/w 1 to %d): ",process[i],n);
scanf("%d",&priority[i]);
}
//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(priority[j]<priority[pos])
pos=j;
}
temp=process[i];
process[i]=process[pos];
process[pos]=temp;
temp=brust[i];
brust[i]=brust[pos];
brust[pos]=temp;
temp=priority[i];
priority[i]=priority[pos];
priority[pos]=temp;
if(brust[i]<0)
{
flag = 1;
}
}
if(flag==1)
{
printf("\n\n::: Invalid Time Entered ::: \n");
exit(0);
}
printf("\n |Priority | Process | Brust |");
for(i=0;i<n;i++)
{
printf("\n | %d \t | %c \t| %d |",priority[i],process[i],brust[i]);
tbt = tbt + brust[i];
}
printf("\n\n::::::::: Quantom time is 1 Sec ::::::::: \n\n");
//assign one or more lottery numbers to each process
int p=1,m_ticket=0;
printf("\n\n\nPriority process Brust Lottery Tickets");
for(i=0;i<n;i++)
{
lottery[i] = (brust[i]/quantom) + (n-priority[i]);
for (z=0;z<lottery[i];z++)
{
ticket[i][z] = p++;
m_ticket = p;
}
printf("\n %d\t %c\t %d \t %d\t",priority[i],process[i],brust[i],lottery[i]);
for(z=0;z<lottery[i];z++)
{
if(ticket[i][z]<10)
printf(" ::%d:: ",ticket[i][z]);
else
printf(" ::%d::",ticket[i][z]);
}
}
while(time!=tbt)
{
int winner = (rand()%m_ticket-1)+ 1;
for(i =0;i<n;i++)
for(z=0;z<lottery[i];z++)
if(ticket[i][z]==winner)
q=i;
printf("\n\n-------------------------------------");
printf("<<<< Winner: %d >>>>",winner);
printf("-------------------------------------\n");
if ((brust[q]>0))
{
brust[q]-=quantom;
if (brust[q]>0)
{
time+=quantom;
}
else
{
time+=(brust[q]+quantom);
}
if(brust[q]<0)
{
brust[q]=0;
}
printf("\n\t\t\t\t Process That Are Running Is: %c",process[q]);
printf("\n\t (Total Time << Remaining Brust Time Of This Process << process ): ( %d << %d << %c )\n",time,brust[q],process[q]);
}
else
{
printf("\n\t\t >>>>>>Related Process With This Ticket Has Been Completed<<<<<<\n");
}
}
}