FINAL.txt 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  1. int main()
  2. {
  3. ;
  4. }
  5. /*
  6. ....................................................... TCP C SERVER ....................................................................................
  7. #include<netinet/in.h>
  8. #include<stdio.h>
  9. #include<stdlib.h>
  10. #include<string.h>
  11. int main()
  12. {
  13. int i, receiver ,listensock , size;
  14. char recvBuffer[100] , sendbuffer[100];
  15. struct sockaddr_in thisSocket , clientSock ;
  16. listensock = socket(PF_INET , SOCK_STREAM , 0);
  17. thisSocket.sin_family = AF_INET;
  18. thisSocket.sin_addr.s_addr = inet_addr("127.0.0.1");
  19. thisSocket.sin_port = htons(7778);
  20. bind(listensock , (struct sockaddr *)&thisSocket , sizeof(thisSocket));
  21. listen(listensock , 1);
  22. size = sizeof(struct sockaddr_in);
  23. receiver = accept(listensock , (struct sockaddr *)&clientSock , &size);
  24. while(1)
  25. {
  26. //////////// RECV PART ////////////
  27. i = recv(receiver,recvBuffer,100,0);
  28. if(i > 0)
  29. {
  30. recvBuffer[i] = '\0';
  31. printf("%s",recvBuffer);
  32. }
  33. //////////// SEND PART ////////////
  34. fgets(sendbuffer,100,stdin);
  35. send(receiver , sendbuffer , strlen(sendbuffer),0);
  36. }
  37. }
  38. ....................................................... TCP C CLIENT ....................................................................................
  39. #include<netinet/in.h>
  40. #include<stdio.h>
  41. #include<stdlib.h>
  42. #include<string.h>
  43. int main()
  44. {
  45. int i, receiver;
  46. char recvBuffer[100] , sendbuffer[100];
  47. struct sockaddr_in thisSocket;
  48. receiver = socket(PF_INET , SOCK_STREAM , 0);
  49. thisSocket.sin_family = AF_INET;
  50. thisSocket.sin_addr.s_addr = inet_addr("127.0.0.1");
  51. thisSocket.sin_port = htons(7778);
  52. connect(receiver,(struct sockaddr *)&thisSocket,sizeof(thisSocket));
  53. while(1)
  54. {
  55. //////////// SEND PART ////////////
  56. fgets(sendbuffer,100,stdin);
  57. send(receiver,sendbuffer,strlen(sendbuffer),0);
  58. //////////// RECV PART ////////////
  59. i =recv(receiver,recvBuffer,100,0);
  60. if(i>0)
  61. {
  62. recvBuffer[i]='\0';
  63. printf("%s",recvBuffer);
  64. }
  65. }
  66. }
  67. ........................................................ TCP JAVA SERVER ...................................................................................
  68. import java.io.*;
  69. import java.net.*;
  70. import java.util.*;
  71. public class Main {
  72. public static void main(String[] args) throws IOException {
  73. ServerSocket ss = null;
  74. Socket s = null;
  75. ss = new ServerSocket(7777);
  76. s = ss.accept();
  77. DataOutputStream dos = new DataOutputStream(s.getOutputStream());
  78. DataInputStream dis = new DataInputStream(s.getInputStream());
  79. while(true)
  80. {
  81. String from_Client = dis.readUTF();
  82. System.out.println(from_Client);
  83. Scanner sc = new Scanner(System.in);
  84. String to_client = sc.nextLine();
  85. dos.writeUTF(to_client);
  86. }
  87. }
  88. }
  89. ........................................................ TCP JAVA CLIENT ...................................................................................
  90. import java.io.*;
  91. import java.net.*;
  92. import java.util.*;
  93. public class Main {
  94. public static void main(String[] args) throws IOException {
  95. Socket s = null;
  96. s = new Socket("127.0.0.1",7777);
  97. DataOutputStream dos = new DataOutputStream(s.getOutputStream());
  98. DataInputStream dis = new DataInputStream(s.getInputStream());
  99. while (true)
  100. {
  101. Scanner sc = new Scanner(System.in);
  102. String to_server = sc.nextLine();
  103. dos.writeUTF(to_server);
  104. String from_server = dis.readUTF();
  105. System.out.println(from_server);
  106. }
  107. }
  108. }
  109. ........................................................ UDP C CLIENT ...................................................................................
  110. #include<stdio.h>
  111. #include<netinet/in.h>
  112. #include<sys/socket.h>
  113. #include<sys/types.h>
  114. #include<string.h>
  115. #include<unistd.h>
  116. int main()
  117. {
  118. int sd,portno,i;
  119. struct sockaddr_in cli;
  120. char msg[80],ip[80];
  121. sd=socket(AF_INET,SOCK_DGRAM,0);
  122. bzero(&cli,sizeof(cli));
  123. printf("enter the port no");
  124. scanf("%d",&portno);
  125. cli.sin_port=htons(portno);
  126. cli.sin_family=AF_INET;
  127. printf("enter the ip address");
  128. scanf("%s",ip);
  129. cli.sin_addr.s_addr=inet_addr(ip);
  130. printf("\nenter the message to send\n");
  131. scanf("%s",msg);
  132. sendto(sd,msg,80,0,(struct sockaddr*)&cli,sizeof(cli));
  133. printf("\nreply from server\n");
  134. i=sizeof(cli);
  135. recvfrom(sd,msg,80,0,(struct sockaddr*)&cli,&i);
  136. printf("%s",msg);
  137. return 0;
  138. //scanf("%s",msg);
  139. }
  140. ........................................................ UDP C SERVER ...................................................................................
  141. #include<stdio.h>
  142. #include<netinet/in.h>
  143. #include<sys/socket.h>
  144. #include<sys/types.h>
  145. #include<string.h>
  146. #include<unistd.h>
  147. int main()
  148. {
  149. int sd,portno,i;
  150. struct sockaddr_in cli;
  151. char msg[80],ip[80];
  152. sd=socket(AF_INET,SOCK_DGRAM,0);
  153. bzero(&cli,sizeof(cli));
  154. printf("enter the port no");
  155. scanf("%d",&portno);
  156. cli.sin_port=htons(portno);
  157. cli.sin_family=AF_INET;
  158. cli.sin_addr.s_addr=INADDR_ANY;
  159. bind(sd,(struct sockaddr*)&cli,sizeof(cli));
  160. printf("\nreply from client\n");
  161. i=sizeof(cli);
  162. recvfrom(sd,msg,80,0,(struct sockaddr*)&cli,&i);
  163. printf("%s",msg);
  164. printf("\nenter the message to send\n");
  165. scanf("%s",msg);
  166. sendto(sd,msg,80,0,(struct sockaddr*)&cli,sizeof(cli));
  167. return 0;
  168. }
  169. ........................................................ UDP JAVA SERVER ...................................................................................
  170. import java.io.*;
  171. import java.net.*;
  172. import java.util.Scanner;
  173. public class Main
  174. {
  175. public static void main(String args[])throws IOException {
  176. DatagramSocket ds = new DatagramSocket(6058);
  177. byte receiver[] = new byte[1024];
  178. byte sender[] = new byte[1024];
  179. DatagramPacket RECVdp = new DatagramPacket(receiver, receiver.length);
  180. ds.receive(RECVdp);
  181. String sentence = new String(RECVdp.getData());
  182. System.out.println(sentence);
  183. InetAddress ip = RECVdp.getAddress();
  184. int port = RECVdp.getPort();
  185. Scanner sc = new Scanner(System.in);
  186. String reply = sc.nextLine();
  187. sender = reply.getBytes();
  188. DatagramPacket SENDdp = new DatagramPacket(sender, sender.length, ip, port);
  189. ds.send(SENDdp);
  190. ds.close();
  191. }
  192. }
  193. ........................................................ UDP JAVA CLIENT ...................................................................................
  194. import java.io.*;
  195. import java.net.*;
  196. import java.util.*;
  197. public class Main {
  198. public static void main(String[] args)throws IOException {
  199. byte receiver[] = new byte[1024];
  200. byte sender[] = new byte[1024];
  201. DatagramSocket ds = new DatagramSocket();
  202. InetAddress ip = InetAddress.getByName("localhost");
  203. Scanner sc = new Scanner(System.in);
  204. String reply = sc.nextLine();
  205. sender = reply.getBytes();
  206. DatagramPacket SENDdp = new DatagramPacket(sender,sender.length,ip,6058);
  207. ds.send(SENDdp);
  208. DatagramPacket RECVdp = new DatagramPacket(receiver,receiver.length);
  209. ds.receive(RECVdp);
  210. String from_server = new String(RECVdp.getData());
  211. System.out.println(from_server);
  212. ds.close();
  213. }
  214. }
  215. ........................................................ FILE IN C ...................................................................................
  216. #include<stdio.h>
  217. #include<stdlib.h>
  218. #include<string.h>
  219. int main()
  220. {
  221. FILE *FP;
  222. int i=0;
  223. FP = fopen("test.txt","r");
  224. char str[1024];
  225. char c;
  226. while(!feof(FP))
  227. {
  228. str[i]=fgetc(FP);;
  229. i++;
  230. }
  231. str[i-1]='\0';
  232. puts(str);
  233. }
  234. ........................................................ FILE IN JAVA ...................................................................................
  235. import java.io.*;
  236. public class Main {
  237. public static void main(String args[]) throws IOException
  238. {
  239. int i=0;
  240. FileInputStream in = null;
  241. char str[] = new char[1000];
  242. in = new FileInputStream("test.txt");
  243. int c;
  244. while ((c = in.read()) != -1) {
  245. str[i++]=(char)c;
  246. }
  247. System.out.println(str);
  248. }
  249. }
  250. */
  251. .
  252. .
  253. .
  254. .
  255. .
  256. .
  257. .
  258. .
  259. .
  260. .
  261. .
  262. .
  263. .
  264. .
  265. .
  266. .
  267. .
  268. .
  269. .
  270. .
  271. .
  272. .
  273. .
  274. .
  275. .
  276. .
  277. .
  278. #include<stdio.h>
  279. int main()
  280. {
  281. printf("OSSSSSS");
  282. }
  283. /* --------------------------------- reader writer--------------------------------
  284. #include<stdio.h>
  285. #include<pthread.h>
  286. #include<string.h>
  287. #include<semaphore.h>
  288. void *read(), *write();
  289. int a;
  290. int hr=0, min=0, sec=0;
  291. void *ret;
  292. pthread_t rth, wth;
  293. pthread_mutex_t m;
  294. void main()
  295. {
  296. int r1=1, r2=1;
  297. a=10;
  298. //create thread
  299. r1=pthread_create(&rth,NULL,&read,NULL);
  300. r2=pthread_create(&wth,NULL,&write,NULL);
  301. //check for success/failure
  302. if(r1==0)
  303. printf("rth created successfully.\n");
  304. else
  305. printf("rth thread creation failed.\n");
  306. if(r2==0)
  307. printf("wth created successfully.\n");
  308. else
  309. printf("wth thread creation failed.\n");
  310. //initializing mutex
  311. pthread_mutex_init(&m,NULL);
  312. //Join threads
  313. pthread_join(rth,ret);
  314. pthread_join(wth,ret);
  315. }
  316. void *read()
  317. {
  318. while(1)
  319. {
  320. pthread_mutex_lock(&m);
  321. //printf("%d\n", a);
  322. printf("%d:%d:%d\n", hr, min, sec);
  323. pthread_mutex_unlock(&m);
  324. sleep(1);
  325. }
  326. pthread_exit(&ret);
  327. }
  328. void *write()
  329. {
  330. while(1)
  331. {
  332. pthread_mutex_lock(&m);
  333. sec++;
  334. if(sec==60)
  335. {
  336. sec=0;
  337. min++;
  338. }
  339. if(min==60)
  340. {
  341. min=0;
  342. hr++;
  343. }
  344. if(hr==24)
  345. {
  346. hr=0;
  347. }
  348. pthread_mutex_unlock(&m);
  349. sleep(1);
  350. }
  351. pthread_exit(&ret);
  352. }
  353. //-----------------------------------------------------------------------------------------------------------------
  354. */
  355. /*
  356. ....................................................... producer-consumer-semaphore ....................................................................................
  357. #include<stdio.h>
  358. #include<semaphore.h>
  359. #include<pthread.h>
  360. int buffer[10],tid1,tid2,cnt = 0;
  361. void *producer();
  362. void *consumer();
  363. void *ret;
  364. sem_t s1,s2;
  365. pthread_t pro,con;
  366. void main()
  367. {
  368. sem_init(&s1,0,0);
  369. sem_init(&s2,0,1);
  370. tid1 = pthread_create(&pro,NULL,&producer,NULL);
  371. if(tid1 == 0)
  372. printf("\nproducer created successfully");
  373. tid2 = pthread_create(&con,NULL,&consumer,NULL);
  374. if(tid2 == 0)
  375. printf("\nconsumer created successfully");
  376. pthread_join(pro,NULL);
  377. pthread_join(con,NULL);
  378. sem_destroy(&s1);
  379. sem_destroy(&s2);
  380. }
  381. void *producer()
  382. {
  383. int item_produced = 0;
  384. printf("\nproducing..");
  385. while(cnt < 10)
  386. {
  387. sem_wait(&s2);
  388. item_produced++;
  389. buffer[cnt] = item_produced;
  390. printf("\nitem produced : %d",item_produced);
  391. cnt++;
  392. sem_post(&s1);
  393. sleep(1);
  394. }
  395. return(&ret);
  396. }
  397. void *consumer()
  398. {
  399. int item_consumed;
  400. printf("\nconsuming..");
  401. while(cnt >= 0)
  402. {
  403. sem_wait(&s1);
  404. cnt--;
  405. item_consumed = buffer[cnt];
  406. printf("\nitem consumed : %d",item_consumed);
  407. sem_post(&s2);
  408. sleep(1);
  409. }
  410. return(&ret);
  411. }
  412. ....................................................... job scheduling ....................................................................................
  413. #include<cstdio>
  414. #include<iostream>
  415. #include<algorithm>
  416. #include<queue>
  417. using namespace std;
  418. void FCFS(int num_proc, int arrival_time[], int burst_time[])
  419. {
  420. int time_cntr = 0,next = 0,j,i,finish_time[num_proc],turnaround_time[num_proc],waiting_time[num_proc],done[num_proc+1];
  421. for(i=0; i<=num_proc; ++i)
  422. {
  423. done[i] = 0;
  424. }
  425. for(i=0; i<num_proc; ++i)
  426. {
  427. next = num_proc;
  428. for(j=0; j<=num_proc; ++j)
  429. {
  430. if(arrival_time[j] < arrival_time[next] && done[j] == 0)
  431. {
  432. //cout<<"next changed"<<endl<<endl;
  433. next = j;
  434. }
  435. }
  436. cout<<"next : "<<next<<endl;
  437. done[next] = 1;
  438. time_cntr = time_cntr + burst_time[next];
  439. finish_time[next] = time_cntr;
  440. cout<<"FT : "<<finish_time[next]<<endl;
  441. turnaround_time[next] = finish_time[next] - arrival_time[next];
  442. cout<<"TT : "<<turnaround_time[next]<<endl;
  443. waiting_time[next] = turnaround_time[next] - burst_time[next];
  444. cout<<"WT : "<<waiting_time[next]<<endl<<endl;
  445. }
  446. return;
  447. }
  448. void FCFS_new(int num_proc, int arrival_time[], int burst_time[])
  449. {
  450. int time_cntr=0,i,j,k,min_proc = 100,next,ft,wt,tt,done[num_proc];
  451. queue< int > ready_q;
  452. for(i=0; i<=num_proc; ++i)
  453. {
  454. done[i] = 0;
  455. }
  456. for(i=0; i<num_proc; ++i)
  457. {
  458. min_proc = 100;
  459. for(j=0; j<num_proc; ++j)
  460. {
  461. if(arrival_time[j] < min_proc && done[j] == 0)
  462. {
  463. min_proc = arrival_time[j];
  464. next = j;
  465. }
  466. }
  467. ready_q.push(next);
  468. done[next] = 1;
  469. }
  470. for(k=0; k<num_proc; ++k)
  471. {
  472. cout<<endl<<endl;
  473. next = ready_q.front();
  474. ready_q.pop();
  475. cout<<"next proc : "<<next;
  476. time_cntr = time_cntr + burst_time[next];
  477. ft = time_cntr;
  478. cout<<"finish time : "<<ft<<endl;
  479. tt = ft - arrival_time[next];
  480. cout<<"turnaround time : "<<tt<<endl;
  481. wt = tt - burst_time[next];
  482. cout<<"waiting time : "<<wt<<endl;
  483. }
  484. return;
  485. }
  486. void SJF_nonP(int num_proc, int arrival_time[], int burst_time[])
  487. {
  488. int i,j,k,done[num_proc],next,min_proc = 100,time_cntr = 0,ft,wt,tt;
  489. for(i=0; i<num_proc; ++i)
  490. {
  491. done[i] = 0;
  492. if(arrival_time[i] == 0)
  493. {
  494. done[i] = 1;
  495. next = i;
  496. }
  497. }
  498. time_cntr = burst_time[next];
  499. ft = time_cntr;
  500. cout<<"Finish time : "<<ft<<endl;
  501. tt = ft - arrival_time[next];
  502. cout<<"Turnaround time : "<<tt<<endl;
  503. wt = tt - burst_time[next];
  504. cout<<"Waiting time : "<<wt<<endl;
  505. for(i=1; i<num_proc; ++i)
  506. {
  507. min_proc = 100;
  508. for(j=0; j<num_proc; ++j)
  509. {
  510. if(arrival_time[j] < time_cntr)
  511. {
  512. if(burst_time[j] < min_proc && done[j] == 0)
  513. {
  514. min_proc = burst_time[j];
  515. next = j;
  516. }
  517. }
  518. }
  519. time_cntr = time_cntr + burst_time[next];
  520. ft = time_cntr;
  521. cout<<endl<<"Finish time : "<<ft<<endl;
  522. cout<<"Turnaround time : "<<tt<<endl;
  523. cout<<"Waiting time : "<<wt<<endl;
  524. }
  525. return;
  526. }
  527. void print_job(int ft,int tt,int wt)
  528. {
  529. cout<<endl<<"Finish time : "<<ft<<endl;
  530. cout<<"Turnaround time : "<<tt<<endl;
  531. cout<<"Waiting time : "<<wt<<endl;
  532. }
  533. void SJF_P(int num_proc, int arrival_time[], int burst_time[])
  534. {
  535. int i,j,k,done[num_proc],next,min_proc = 100,time_cntr = 0,ft,wt,tt;
  536. int bt1[100];
  537. for(i=0; i<num_proc; ++i)
  538. {
  539. done[i] = 0;
  540. bt1[i] = burst_time[i];
  541. // if(arrival_time[i] == 0)
  542. // {
  543. // if(burst_time[i] == 1)
  544. // done[i] = 1;
  545. // next = i;
  546. // }
  547. }
  548. int counter = 0;
  549. while(1)
  550. {
  551. min_proc = 100;
  552. for(i = 0;i < num_proc;i++)
  553. {
  554. if(burst_time[i] < min_proc && done[i] == 0 && arrival_time[i] <= counter)
  555. {
  556. min_proc = burst_time[i];
  557. next = i;
  558. }
  559. }
  560. counter++;
  561. time_cntr++;
  562. cout<<"selected "<<min_proc<<endl;
  563. if(min_proc == 100)
  564. break;
  565. if(burst_time[next] == 1)
  566. {
  567. done[next] = 1;
  568. //next = i;
  569. //time_cntr = time_cntr + bt1[next];
  570. ft = time_cntr;
  571. tt = ft - arrival_time[next];
  572. wt = tt - bt1[next];
  573. print_job(ft,tt,wt);
  574. burst_time[next]--;
  575. }
  576. else
  577. burst_time[next]--;
  578. // for(int i = 0;i < num_proc;i++)
  579. // cout<<burst_time[i]<<"\t"<<done[i]<<endl;
  580. }
  581. return;
  582. }
  583. void Priority_nonP(int num_proc, int arrival_time[], int burst_time[], int priority[])
  584. {
  585. int i,j,k,done[num_proc],next,min_proc = 100,time_cntr = 0,ft,wt,tt;
  586. for(i=0; i<num_proc; ++i)
  587. {
  588. done[i] = 0;
  589. if(arrival_time[i] == 0)
  590. {
  591. done[i] = 1;
  592. next = i;
  593. }
  594. }
  595. time_cntr = time_cntr + burst_time[next];
  596. ft = time_cntr;
  597. cout<<"Finish time : "<<ft<<endl;
  598. tt = ft - arrival_time[next];
  599. cout<<"Turnaround time : "<<tt<<endl;
  600. wt = tt - burst_time[next];
  601. cout<<"Waiting time : "<<wt<<endl;
  602. for(i=1; i<num_proc; ++i)
  603. {
  604. int max_priority = 100;
  605. for(j=0; j<num_proc; ++j)
  606. {
  607. if(arrival_time[j] < time_cntr)
  608. {
  609. if(priority[j] < max_priority && done[j] == 0)
  610. {
  611. max_priority = priority[j];
  612. next = j;
  613. }
  614. }
  615. }
  616. time_cntr = time_cntr + burst_time[next];
  617. ft = time_cntr;
  618. cout<<"Finish time : "<<ft<<endl;
  619. tt = ft - arrival_time[next];
  620. cout<<"Turnaround time : "<<tt<<endl;
  621. wt = tt - burst_time[next];
  622. cout<<"Waiting time : "<<wt<<endl;
  623. done[next] = 1;
  624. }
  625. return;
  626. }
  627. void Priority_P(int num_proc, int arrival_time[], int burst_time[], int priority[])
  628. {
  629. int i,j,k,done[num_proc],next,min_proc = 100,time_cntr = 0,ft,wt,tt;
  630. int bt1[num_proc];
  631. for(i=0; i<num_proc; ++i)
  632. {
  633. done[i] = 0;
  634. bt1[i] = burst_time[i];
  635. // if(arrival_time[i] == 0)
  636. // {
  637. // done[i] = 1;
  638. // next = i;
  639. // }
  640. }
  641. // time_cntr = time_cntr + burst_time[next];
  642. // ft = time_cntr;
  643. // cout<<"Finish time : "<<ft<<endl;
  644. // tt = ft - arrival_time[next];
  645. // cout<<"Turnaround time : "<<tt<<endl;
  646. // wt = tt - burst_time[next];
  647. // cout<<"Waiting time : "<<wt<<endl;
  648. while(1)
  649. {
  650. int max_priority = 100;
  651. for(j=0; j<num_proc; ++j)
  652. {
  653. if(arrival_time[j] <= time_cntr)
  654. {
  655. if(priority[j] < max_priority && done[j] == 0)
  656. {
  657. max_priority = priority[j];
  658. next = j;
  659. }
  660. }
  661. }
  662. time_cntr++;
  663. //cout<<"selected "<<max_priority<<endl;
  664. if(max_priority == 100)
  665. break;
  666. if(burst_time[next] == 1)
  667. {
  668. done[next] = 1;
  669. //next = i;
  670. //time_cntr = time_cntr + bt1[next];
  671. ft = time_cntr;
  672. tt = ft - arrival_time[next];
  673. wt = tt - bt1[next];
  674. print_job(ft,tt,wt);
  675. burst_time[next]--;
  676. }
  677. else
  678. burst_time[next]--;
  679. // ft = time_cntr;
  680. // tt = ft - arrival_time[next];
  681. // wt = tt - burst_time[next];
  682. //done[next] = 1;
  683. }
  684. return;
  685. }
  686. void RR(int num_proc, int arrival_time[], int burst_time[], int priority[])
  687. {
  688. return;
  689. }
  690. int main()
  691. {
  692. int i,j,k,num_proc,choice;
  693. while(1)
  694. {
  695. cout<<"Enter your choice :"<<endl;
  696. cout<<"1] FCFS"<<endl<<"2] SJF(Non-Preemptive)"<<endl<<"3] SJF(Preemptive)"<<endl<<"4] Priority(Non-Preemptive)"<<endl<<"5] Priority(Preemptive)"<<endl<<"6] Round-Robin"<<endl<<"7] EXIT"<<endl<<endl;
  697. cin>>choice;
  698. if(choice == 7)
  699. break;
  700. cout<<"Enter number of processes :"<<endl<<endl;
  701. cin>>num_proc;
  702. int arrival_time[num_proc+1],burst_time[num_proc],priority[num_proc];
  703. cout<<"Enter Arrival Times, Burst Times and Priority of processes :"<<endl<<endl;
  704. for(i=0; i<num_proc; i++)
  705. {
  706. cin>>arrival_time[i];
  707. cin>>burst_time[i];
  708. cin>>priority[i];
  709. }
  710. arrival_time[num_proc] = 10000;
  711. switch(choice)
  712. {
  713. case 1:
  714. FCFS_new(num_proc, arrival_time, burst_time);
  715. break;
  716. case 2:
  717. SJF_nonP(num_proc, arrival_time, burst_time);
  718. break;
  719. case 3:
  720. SJF_P(num_proc, arrival_time, burst_time);
  721. break;
  722. case 4:
  723. Priority_nonP(num_proc, arrival_time, burst_time, priority);
  724. break;
  725. case 5:
  726. Priority_P(num_proc, arrival_time, burst_time, priority);
  727. break;
  728. case 6:
  729. RR(num_proc, arrival_time, burst_time, priority);
  730. break;
  731. }
  732. }
  733. return 0;
  734. }
  735. ........................................................ page replacement FIFO ONLY ...................................................................................
  736. #include<stdio.h>
  737. #include<stdlib.h>
  738. #include<iostream>
  739. #include<queue>
  740. using namespace std;
  741. void FIFO(int num_of_frames,int num_of_pages,int pages[],int time[],int cnt)
  742. {
  743. int i,j,k,m,n,memory[num_of_frames],num_of_pf = 0,no_pf = 0, valid_pf = 0;
  744. queue< int > mem;
  745. for(j=0; j<num_of_frames; ++j)
  746. memory[j] = 0;
  747. for(j=0; j<cnt; ++j)
  748. {
  749. for(k=0; k<num_of_frames; ++k)
  750. {
  751. if(memory[k] == pages[j])
  752. {
  753. no_pf = 1;
  754. break;
  755. }
  756. else
  757. continue;
  758. }
  759. if(no_pf == 1)
  760. {
  761. cout<<"j : "<<j<<" no pf"<<endl;
  762. no_pf = 0;
  763. continue;
  764. }
  765. else
  766. {
  767. num_of_pf++;
  768. cout<<"j : "<<j<<" pf : "<<num_of_pf<<endl;
  769. for(m=0; m<num_of_frames; ++m)
  770. {
  771. if(memory[m] == 0)
  772. {
  773. valid_pf = 1;
  774. break;
  775. }
  776. }
  777. if(valid_pf == 1)
  778. {
  779. memory[m] = pages[j];
  780. mem.push(pages[j]);
  781. valid_pf = 0;
  782. continue;
  783. }
  784. else
  785. {
  786. int replace_page = mem.front();
  787. mem.pop();
  788. mem.push(pages[j]);
  789. for(n=0; n<num_of_frames; ++n)
  790. {
  791. if(memory[n] == replace_page)
  792. {
  793. memory[n] = pages[j];
  794. break;
  795. }
  796. }
  797. }
  798. }
  799. }
  800. cout<<"number of page faults : "<<num_of_pf<<endl;
  801. }
  802. void Optimal(int pages[],int time[],int num_of_frames,int num_of_pages)
  803. {
  804. }
  805. int main()
  806. {
  807. int num_of_frames,num_of_pages,i=0,j,k,cnt=0;
  808. cout<<"Enter no. of frames : ";
  809. cin>>num_of_frames;
  810. cout<<"Enter no. of pages : ";
  811. cin>>num_of_pages;
  812. int pages[num_of_pages],time[num_of_pages],choice;
  813. cout<<"Enter the Reference String : ";
  814. while(1)
  815. {
  816. cin>>pages[cnt];
  817. if(pages[cnt] == 0)
  818. break;
  819. cnt++;
  820. }
  821. cout<<"Enter your choice : "<<endl;
  822. cout<<"1] FIFO"<<endl<<"2] Optimal"<<endl<<"3] LRU"<<endl;
  823. cin>>choice;
  824. switch(choice)
  825. {
  826. case 1:
  827. FIFO(num_of_frames,num_of_pages,pages,time,cnt);
  828. break;
  829. case 2:
  830. Optimal(pages,time,num_of_frames,num_of_pages);
  831. break;
  832. case 3:
  833. //LRU(pages,time,num_of_frames,num_of_pages);
  834. break;
  835. case 4:
  836. exit(0);
  837. }
  838. return 0;
  839. }
  840. ........................................................ DISC SCHEDULING ...................................................................................
  841. #include<stdio.h>
  842. #include<queue>
  843. #include<iostream>
  844. #include<algorithm>
  845. using namespace std;
  846. void fcfs(int num, int req[], int current)
  847. {
  848. int i,seek=0;
  849. for(i=0; i<num; ++i)
  850. {
  851. if(current <= req[i])
  852. seek = seek + (req[i] - current);
  853. if(current > req[i])
  854. seek = seek + (current - req[i]);
  855. current = req[i];
  856. }
  857. cout<<"total seek time : "<<seek<<endl;
  858. }
  859. int find_min(int num, int current, int req[], int done[])
  860. {
  861. int temp,i,j,arr[num];
  862. for(i=0; i<num; ++i)
  863. {
  864. temp = current - req[i];
  865. if(temp < 0)
  866. temp = temp - (2 * temp);
  867. arr[i] = temp;
  868. }
  869. int min_req = 10000,min_index;
  870. for(i=0; i<num; ++i)
  871. {
  872. if(min_req > arr[i] && done[i] == 0)
  873. {
  874. min_req = arr[i];
  875. min_index = i;
  876. }
  877. }
  878. return min_index;
  879. }
  880. void sstf(int num, int req[], int current)
  881. {
  882. int i,j,done[num],next,seek = 0;
  883. for(i=0; i<num; ++i)
  884. done[i] = 0;
  885. for(i=0; i<num; ++i)
  886. {
  887. next = find_min(num,current,req,done);
  888. if(current > req[next])
  889. seek = seek + (current - req[next]);
  890. else if(current <= req[next])
  891. seek = seek + (req[next] - current);
  892. current = req[next];
  893. done[next] = 1;
  894. }
  895. cout<<seek;
  896. }
  897. void scan(int num, int req[], int current)
  898. {
  899. int i,j,newreq[num+1],cur_index;
  900. for(i=0; i<num; ++i)
  901. {
  902. newreq[i] = req[i];
  903. }
  904. newreq[num] = current;
  905. sort(newreq,(newreq+num+1));
  906. for(i=0; i<(num+1); ++i)
  907. {
  908. if(newreq[i] == current)
  909. {
  910. cur_index = i;
  911. break;
  912. }
  913. }
  914. cout<<"current at : "<<cur_index<<endl;
  915. int seek = 0,next = cur_index;
  916. for(i=0; i<(num-cur_index); ++i)
  917. {
  918. seek = seek + (newreq[next+1] - newreq[next]);
  919. next++;
  920. }
  921. next = cur_index-1;
  922. seek = seek + (newreq[num] - newreq[next]);
  923. for(i=1; i<cur_index; ++i)
  924. {
  925. seek = seek + (newreq[next] - newreq[next-1]);
  926. next--;
  927. }
  928. cout<<"final : "<<seek<<endl;
  929. }
  930. int main()
  931. {
  932. int i,num,cur;
  933. cout<<"enter current head position : ";
  934. cin>>cur;
  935. cout<<"enter number of requests : ";
  936. cin>>num;
  937. int req[num];
  938. cout<<"enter requests : ";
  939. for(i=0; i<num; ++i)
  940. cin>>req[i];
  941. //fcfs(num,req,cur);
  942. //sstf(num,req,cur);
  943. scan(num,req,cur);
  944. return 0;
  945. }
  946. ........................................................ BUBBLE SORT ...................................................................................
  947. #/bin/sh
  948. echo "Number of elements : "
  949. read num
  950. echo "Enter the elements : "
  951. for((i=0; i<num; ++i))
  952. do
  953. read arr[$i]
  954. done
  955. for((i=0; i<num; ++i))
  956. do
  957. for((j=0; j<num-1; ++j))
  958. do
  959. if [ ${arr[$j]} -gt ${arr[$j+1]} ]
  960. then
  961. temp=${arr[$j]}
  962. arr[$j]=${arr[$j+1]}
  963. arr[$j+1]=$temp
  964. fi
  965. done
  966. done
  967. echo "Sorted elements are : "
  968. for((i=0; i<num; ++i))
  969. do
  970. echo ${arr[$i]}
  971. done
  972. ........................................................ LRU ...................................................................................
  973. #include <stdio.h>
  974. #define MAX 20
  975. typedef struct page
  976. {
  977. int data;
  978. int hit;
  979. } page;
  980. void main()
  981. {
  982. int i,j,k,flag,cnt=0,q=0,flag2=0,temp,max,mix=0,f3=0;
  983. page p[MAX];
  984. int frame[MAX];
  985. int fs,nr;
  986. printf("\nEnter number of references: ");
  987. scanf("%d",&nr);
  988. printf("\nEnter %d references: ",nr);
  989. for(i=0; i<nr; i++)
  990. {
  991. scanf("%d",&frame[i]);
  992. }
  993. printf("\nEnter frame size: ");
  994. scanf("%d",&fs);
  995. for(i=0; i<fs; i++)
  996. {
  997. p[i].data=-1;
  998. p[i].hit=0;
  999. }
  1000. j=0;
  1001. for(i=0; i<nr; i++)
  1002. {
  1003. for(k=0; k<fs; k++)
  1004. {
  1005. if(p[k].data!=-1)
  1006. p[k].hit++;
  1007. }
  1008. flag=0;
  1009. printf("\nREF: %d",frame[i]);
  1010. for(k=0; k<fs; k++)
  1011. {
  1012. if(p[k].data==frame[i])
  1013. {
  1014. p[k].hit=1;
  1015. flag=1;
  1016. temp=k;
  1017. break;
  1018. }
  1019. }
  1020. if(flag!=1 && f3!=1)
  1021. {
  1022. p[j].data=frame[i];
  1023. p[j].hit=1;
  1024. temp=j;
  1025. cnt++;
  1026. j++;
  1027. }
  1028. if(f3==1 && flag!=1)
  1029. {
  1030. max=0;
  1031. mix=0;
  1032. for(k=0; k<fs; k++)
  1033. {
  1034. if(max < p[k].hit)
  1035. {
  1036. max=p[k].hit;
  1037. mix=k;
  1038. }
  1039. }
  1040. flag2=0;
  1041. for(k=0; k<fs; k++)
  1042. {
  1043. if(flag2!=1)
  1044. {
  1045. j=mix;
  1046. p[j].data=frame[i];
  1047. p[j].hit=1;
  1048. temp=j;
  1049. cnt++;
  1050. flag2=1;
  1051. }
  1052. }
  1053. }
  1054. if(j==fs)
  1055. {
  1056. j=0;
  1057. f3=1;
  1058. }
  1059. for(k=0; k<fs; k++)
  1060. {
  1061. if(temp==k)
  1062. {
  1063. printf("\n->%d-%d",p[k].data,p[k].hit);
  1064. }
  1065. else
  1066. {
  1067. printf("\n%d-%d",p[k].data,p[k].hit);
  1068. }
  1069. }
  1070. printf("\n-------\n");
  1071. }
  1072. printf("\nPage Fault: %d\n",cnt);
  1073. }
  1074. ........................................................ IDEAL process SCHEDULING PROGRAM ...................................................................................
  1075. #include<stdio.h>
  1076. //Definition of structure for process
  1077. struct process
  1078. {
  1079. char name[20];
  1080. int flag,arrival,burst,initburst,prio;
  1081. };
  1082. //Queue Definition along with queue functions
  1083. struct queue
  1084. {
  1085. struct process data[20];
  1086. int r,f;
  1087. };
  1088. void init(struct queue *q)
  1089. {
  1090. q->r=q->f=-1;
  1091. }
  1092. int empty(struct queue q)
  1093. {
  1094. if(q.r==-1)
  1095. return(1);
  1096. return(0);
  1097. }
  1098. //Normal enqueue(used in FCFS and Round Robin)
  1099. void enqueue(struct queue *q,struct process d)
  1100. {
  1101. if(empty(*q))
  1102. q->r=q->f=0;
  1103. else q->r=q->r+1;
  1104. q->data[q->r]=d;
  1105. }
  1106. /*Enqueue for sjf scheduling. In this, we maintain a priority based queue
  1107. where priority is given to the shortest job, i.e, the job with min. burst time is
  1108. always at the front of the queue*/
  1109. void sjfenqueue(struct queue *q, struct process d)
  1110. {
  1111. int x,y;
  1112. if(empty(*q))
  1113. {
  1114. q->r=q->f=0;
  1115. q->data[q->r]=d;
  1116. }
  1117. else
  1118. {
  1119. x=q->f;
  1120. y=q->r;
  1121. //Go through the queue to find position of new process d
  1122. while(q->data[x].burst<=d.burst&&x<=y)
  1123. x++;
  1124. //Shift elements one place to the right to make space for d
  1125. while(y>=x)
  1126. {
  1127. q->data[y+1]=q->data[y];
  1128. y--;
  1129. }
  1130. q->data[x]=d;
  1131. q->r=q->r+1;
  1132. }
  1133. }
  1134. /*Enqueue for priority scheduling. Same as above except the priority is now
  1135. using process priority instead of burst time*/
  1136. void prioenqueue(struct queue *q, struct process d)
  1137. {
  1138. int x,y;
  1139. if(empty(*q))
  1140. {
  1141. q->r=q->f=0;
  1142. q->data[q->r]=d;
  1143. }
  1144. else
  1145. {
  1146. x=q->f;
  1147. y=q->r;
  1148. //Go through the queue to find position of new process d
  1149. while(q->data[x].prio<=d.prio&&x<=y)
  1150. x++;
  1151. //Shift elements one place to the right to make space for d
  1152. while(y>=x)
  1153. {
  1154. q->data[y+1]=q->data[y];
  1155. y--;
  1156. }
  1157. q->data[x]=d;
  1158. q->r=q->r+1;
  1159. }
  1160. }
  1161. struct process dequeue(struct queue *q)
  1162. {
  1163. struct process d;
  1164. d=q->data[q->f];
  1165. if(q->f==q->r)
  1166. q->f=q->r=-1;
  1167. else q->f=q->f+1;
  1168. return(d);
  1169. }
  1170. void accept(struct process p[20],int n);
  1171. void fcfs(struct process p[20],int n);
  1172. void sjf(struct process p[20],int n);
  1173. void prio(struct process p[20],int n);
  1174. void sjfpre(struct process p[20],int n);
  1175. void priopre(struct process p[20],int n);
  1176. void rr(struct process p[20],int n);
  1177. void main()
  1178. {
  1179. int ch,n,i;
  1180. struct process p[20];
  1181. printf("\nEnter number of processes: ");
  1182. scanf("%d",&n);
  1183. accept(p,n);
  1184. do
  1185. {
  1186. for(i=0;i<n;i++)
  1187. p[i].flag=0;
  1188. printf("\nMENU:\n1.FCFS\n2.SJF\n3.SJF-preemptive\n4.Priority\n5.Priority-preemptive\n6.Round Robin\n7.Re-enter processes\n8.Exit\n\n\tEnter your choice: ");
  1189. scanf("%d",&ch);
  1190. switch(ch)
  1191. {
  1192. case 1: fcfs(p,n);
  1193. break;
  1194. case 2: sjf(p,n);
  1195. break;
  1196. case 3: sjfpre(p,n);
  1197. break;
  1198. case 4: prio(p,n);
  1199. break;
  1200. case 5: priopre(p,n);
  1201. break;
  1202. case 6: rr(p,n);
  1203. break;
  1204. case 7: printf("\nEnter number of processes: ");
  1205. scanf("%d",&n);
  1206. accept(p,n);
  1207. break;
  1208. case 8: break;
  1209. default:printf("\nWrong choice entered\n");
  1210. break;
  1211. }
  1212. }while(ch!=8);
  1213. }
  1214. void accept(struct process p[20],int n)
  1215. {
  1216. int i;
  1217. char s[2];
  1218. for(i=0;i<n;i++)
  1219. {
  1220. printf("\nProcess %d: ",i+1);
  1221. //We use sprintf to give thee processes names like 'P1','P2',etc.
  1222. sprintf(p[i].name,"P%d",i+1);
  1223. printf("\nEnter arrival time: ");
  1224. scanf("%d",&p[i].arrival);
  1225. printf("\nEnter burst time: ");
  1226. scanf("%d",&p[i].initburst);
  1227. //initburst is the initial burst time, and 'burst' is current burst time
  1228. p[i].burst=p[i].initburst;
  1229. }
  1230. }
  1231. void fcfs(struct process p[20],int n)
  1232. {
  1233. int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
  1234. float avgta=0,avgwt=0;
  1235. struct queue q;
  1236. init(&q);
  1237. struct process pc;
  1238. /* pc is the current process. Since initially, there is no process in
  1239. pc, we set its flag to -1*/
  1240. pc.flag=-1;
  1241. printf("\nGANTT CHART\n\n\t|");
  1242. /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses*/
  1243. for(i=0;comp<n;i++)
  1244. {
  1245. /*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
  1246. flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
  1247. */
  1248. for(j=0;j<n;j++)
  1249. {
  1250. if(p[j].arrival<=i && p[j].flag==0)
  1251. {
  1252. p[j].flag=1;
  1253. enqueue(&q,p[j]);
  1254. }
  1255. }
  1256. /*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
  1257. if(pc.flag==-1)
  1258. {
  1259. /*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
  1260. if(empty(q) && flag1==0)
  1261. flag1=1;
  1262. else if(empty(q) && flag1==1)
  1263. continue;
  1264. else if(!empty(q) && flag1==1)
  1265. {
  1266. printf(" |");
  1267. /*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */
  1268. pretime[cnt++]=i;
  1269. flag1=0;
  1270. pc=dequeue(&q);
  1271. }
  1272. else pc=dequeue(&q);
  1273. }
  1274. /*Decrement burst time to indicate completion of one burst */
  1275. pc.burst--;
  1276. /* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
  1277. if(pc.burst==0)
  1278. {
  1279. turnaround=(i+1)-pc.arrival;
  1280. waiting=turnaround-pc.initburst;
  1281. avgta+=turnaround;
  1282. avgwt+=waiting;
  1283. pretime[cnt++]=i+1;
  1284. comp++;
  1285. printf("%s|",pc.name);
  1286. pc.flag=-1;
  1287. }
  1288. }
  1289. //Display the times below the Gantt Chart
  1290. printf("\n\t0 ");
  1291. for(i=0;i<cnt;i++)
  1292. {
  1293. if(pretime[i]>9)
  1294. printf("%d ",pretime[i]);
  1295. else printf (" %d ",pretime[i]);
  1296. }
  1297. //Calculate avg. TA and WT
  1298. avgta=avgta/n;
  1299. avgwt=avgwt/n;
  1300. printf("\n\nAvg. TA time: %.2f",avgta);
  1301. printf("\n\nAvg. WT time: %.2f",avgwt);
  1302. }
  1303. //Exactly the same as FCFS, except we use sjfenqueue function instead of enqueue function
  1304. void sjf(struct process p[20],int n)
  1305. {
  1306. int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
  1307. float avgta=0,avgwt=0;
  1308. struct queue q;
  1309. init(&q);
  1310. struct process pc;
  1311. /* pc is the current process. Since initially, there is no process in
  1312. pc, we set its flag to -1*/
  1313. pc.flag=-1;
  1314. printf("\nGANTT CHART\n\n\t|");
  1315. /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses
  1316. */
  1317. for(i=0;comp<n;i++)
  1318. {
  1319. /*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
  1320. flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
  1321. */
  1322. for(j=0;j<n;j++)
  1323. {
  1324. if(p[j].arrival<=i && p[j].flag==0)
  1325. {
  1326. p[j].flag=1;
  1327. sjfenqueue(&q,p[j]);
  1328. }
  1329. }
  1330. /*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
  1331. if(pc.flag==-1)
  1332. {
  1333. /*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
  1334. if(empty(q) && flag1==0)
  1335. flag1=1;
  1336. else if(empty(q) && flag1==1)
  1337. continue;
  1338. else if(!empty(q) && flag1==1)
  1339. {
  1340. printf(" |");
  1341. /*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */
  1342. pretime[cnt++]=i;
  1343. flag1=0;
  1344. pc=dequeue(&q);
  1345. }
  1346. else pc=dequeue(&q);
  1347. }
  1348. /*Decrement burst time to indicate completion of one burst */
  1349. pc.burst--;
  1350. /* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
  1351. if(pc.burst==0)
  1352. {
  1353. turnaround=(i+1)-pc.arrival;
  1354. waiting=turnaround-pc.initburst;
  1355. avgta+=turnaround;
  1356. avgwt+=waiting;
  1357. pretime[cnt++]=i+1;
  1358. comp++;
  1359. printf("%s|",pc.name);
  1360. pc.flag=-1;
  1361. }
  1362. }
  1363. //Display the times below the Gantt Chart
  1364. printf("\n\t0 ");
  1365. for(i=0;i<cnt;i++)
  1366. {
  1367. if(pretime[i]>9)
  1368. printf("%d ",pretime[i]);
  1369. else printf (" %d ",pretime[i]);
  1370. }
  1371. //Calculate avg. TA and WT
  1372. avgta=avgta/n;
  1373. avgwt=avgwt/n;
  1374. printf("\n\nAvg. TA time: %.2f",avgta);
  1375. printf("\n\nAvg. WT time: %.2f",avgwt);
  1376. }
  1377. //Exactly the same as sjf,but with preemption
  1378. void sjfpre(struct process p[20],int n)
  1379. {
  1380. int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
  1381. float avgta=0,avgwt=0;
  1382. struct queue q;
  1383. init(&q);
  1384. struct process pc;
  1385. /* pc is the current process. Since initially, there is no process in
  1386. pc, we set its flag to -1*/
  1387. pc.flag=-1;
  1388. printf("\nGANTT CHART\n\n\t|");
  1389. /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses
  1390. */
  1391. for(i=0;comp<n;i++)
  1392. {
  1393. /*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
  1394. flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
  1395. */
  1396. for(j=0;j<n;j++)
  1397. {
  1398. if(p[j].arrival<=i && p[j].flag==0)
  1399. {
  1400. p[j].flag=1;
  1401. sjfenqueue(&q,p[j]);
  1402. }
  1403. }
  1404. /*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
  1405. if(pc.flag==-1)
  1406. {
  1407. /*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
  1408. if(empty(q) && flag1==0)
  1409. flag1=1;
  1410. else if(empty(q) && flag1==1)
  1411. continue;
  1412. else if(!empty(q) && flag1==1)
  1413. {
  1414. printf(" |");
  1415. /*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */
  1416. pretime[cnt++]=i;
  1417. flag1=0;
  1418. pc=dequeue(&q);
  1419. }
  1420. else pc=dequeue(&q);
  1421. }
  1422. //The following 'else if' gives the condition for pre-emption. If the process at the front of the queue has smaller burst time than the current process, then current process is enqueued and the new process is dequeued into pc
  1423. else if(q.f!=-1 && pc.burst > q.data[q.f].burst)
  1424. {
  1425. printf("%s|",pc.name);
  1426. pretime[cnt++]=i;
  1427. sjfenqueue(&q,pc);
  1428. pc=dequeue(&q);
  1429. }
  1430. /*Decrement burst time to indicate completion of one burst */
  1431. pc.burst--;
  1432. /* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
  1433. if(pc.burst==0)
  1434. {
  1435. turnaround=(i+1)-pc.arrival;
  1436. waiting=turnaround-pc.initburst;
  1437. avgta+=turnaround;
  1438. avgwt+=waiting;
  1439. pretime[cnt++]=i+1;
  1440. comp++;
  1441. printf("%s|",pc.name);
  1442. pc.flag=-1;
  1443. }
  1444. }
  1445. //Display the times below the Gantt Chart
  1446. printf("\n\t0 ");
  1447. for(i=0;i<cnt;i++)
  1448. {
  1449. if(pretime[i]>9)
  1450. printf("%d ",pretime[i]);
  1451. else printf (" %d ",pretime[i]);
  1452. }
  1453. //Calculate avg. TA and WT
  1454. avgta=avgta/n;
  1455. avgwt=avgwt/n;
  1456. printf("\n\nAvg. TA time: %.2f",avgta);
  1457. printf("\n\nAvg. WT time: %.2f",avgwt);
  1458. }
  1459. //Exactly the same as FCFS, except we use prioenqueue function instead of enqueue function
  1460. void prio(struct process p[20],int n)
  1461. {
  1462. int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
  1463. float avgta=0,avgwt=0;
  1464. struct queue q;
  1465. init(&q);
  1466. struct process pc;
  1467. /* pc is the current process. Since initially, there is no process in
  1468. pc, we set its flag to -1*/
  1469. pc.flag=-1;
  1470. //Accept the priority for the processes
  1471. for(i=0;i<n;i++)
  1472. {
  1473. printf("\nEnter priority for process %d: ",i+1);
  1474. scanf("%d",&p[i].prio);
  1475. }
  1476. printf("\nGANTT CHART\n\n\t|");
  1477. /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses
  1478. */
  1479. for(i=0;comp<n;i++)
  1480. {
  1481. /*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
  1482. flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
  1483. */
  1484. for(j=0;j<n;j++)
  1485. {
  1486. if(p[j].arrival<=i && p[j].flag==0)
  1487. {
  1488. p[j].flag=1;
  1489. prioenqueue(&q,p[j]);
  1490. }
  1491. }
  1492. /*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
  1493. if(pc.flag==-1)
  1494. {
  1495. /*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
  1496. if(empty(q) && flag1==0)
  1497. flag1=1;
  1498. else if(empty(q) && flag1==1)
  1499. continue;
  1500. else if(!empty(q) && flag1==1)
  1501. {
  1502. printf(" |");
  1503. /*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */
  1504. pretime[cnt++]=i;
  1505. flag1=0;
  1506. pc=dequeue(&q);
  1507. }
  1508. else pc=dequeue(&q);
  1509. }
  1510. /*Decrement burst time to indicate completion of one burst */
  1511. pc.burst--;
  1512. /* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
  1513. if(pc.burst==0)
  1514. {
  1515. turnaround=(i+1)-pc.arrival;
  1516. waiting=turnaround-pc.initburst;
  1517. avgta+=turnaround;
  1518. avgwt+=waiting;
  1519. pretime[cnt++]=i+1;
  1520. comp++;
  1521. printf("%s|",pc.name);
  1522. pc.flag=-1;
  1523. }
  1524. }
  1525. //Display the times below the Gantt Chart
  1526. printf("\n\t0 ");
  1527. for(i=0;i<cnt;i++)
  1528. {
  1529. if(pretime[i]>9)
  1530. printf("%d ",pretime[i]);
  1531. else printf (" %d ",pretime[i]);
  1532. }
  1533. //Calculate avg. TA and WT
  1534. avgta=avgta/n;
  1535. avgwt=avgwt/n;
  1536. printf("\n\nAvg. TA time: %.2f",avgta);
  1537. printf("\n\nAvg. WT time: %.2f",avgwt);
  1538. }
  1539. //Exactly the same as prio,but with preemption
  1540. void priopre(struct process p[20],int n)
  1541. {
  1542. int i,j,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
  1543. float avgta=0,avgwt=0;
  1544. struct queue q;
  1545. init(&q);
  1546. struct process pc;
  1547. /* pc is the current process. Since initially, there is no process in
  1548. pc, we set its flag to -1*/
  1549. pc.flag=-1;
  1550. //Accept the priority for the processes
  1551. for(i=0;i<n;i++)
  1552. {
  1553. printf("\nEnter priority for process %d: ",i+1);
  1554. scanf("%d",&p[i].prio);
  1555. }
  1556. printf("\nGANTT CHART\n\n\t|");
  1557. /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses
  1558. */
  1559. for(i=0;comp<n;i++)
  1560. {
  1561. /*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
  1562. flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
  1563. */
  1564. for(j=0;j<n;j++)
  1565. {
  1566. if(p[j].arrival<=i && p[j].flag==0)
  1567. {
  1568. p[j].flag=1;
  1569. prioenqueue(&q,p[j]);
  1570. }
  1571. }
  1572. /*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
  1573. if(pc.flag==-1)
  1574. {
  1575. /*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
  1576. if(empty(q) && flag1==0)
  1577. flag1=1;
  1578. else if(empty(q) && flag1==1)
  1579. continue;
  1580. else if(!empty(q) && flag1==1)
  1581. {
  1582. printf(" |");
  1583. /*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */
  1584. pretime[cnt++]=i;
  1585. flag1=0;
  1586. pc=dequeue(&q);
  1587. }
  1588. else pc=dequeue(&q);
  1589. }
  1590. //The following 'else if' gives the condition for pre-emption. If the process at the front of the queue has higher priority than the current process, then current process is enqueued and the new process is dequeued into pc
  1591. else if(q.f!=-1 && pc.prio > q.data[q.f].prio)
  1592. {
  1593. printf("%s|",pc.name);
  1594. pretime[cnt++]=i;
  1595. prioenqueue(&q,pc);
  1596. pc=dequeue(&q);
  1597. }
  1598. /*Decrement burst time to indicate completion of one burst */
  1599. pc.burst--;
  1600. /* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
  1601. if(pc.burst==0)
  1602. {
  1603. turnaround=(i+1)-pc.arrival;
  1604. waiting=turnaround-pc.initburst;
  1605. avgta+=turnaround;
  1606. avgwt+=waiting;
  1607. pretime[cnt++]=i+1;
  1608. comp++;
  1609. printf("%s|",pc.name);
  1610. pc.flag=-1;
  1611. }
  1612. }
  1613. //Display the times below the Gantt Chart
  1614. printf("\n\t0 ");
  1615. for(i=0;i<cnt;i++)
  1616. {
  1617. if(pretime[i]>9)
  1618. printf("%d ",pretime[i]);
  1619. else printf (" %d ",pretime[i]);
  1620. }
  1621. //Calculate avg. TA and WT
  1622. avgta=avgta/n;
  1623. avgwt=avgwt/n;
  1624. printf("\n\nAvg. TA time: %.2f",avgta);
  1625. printf("\n\nAvg. WT time: %.2f",avgwt);
  1626. }
  1627. //Procedure for Round Robin
  1628. void rr(struct process p[20],int n)
  1629. {
  1630. int i,j,qt,tr=0,turnaround,waiting,pretime[30],cnt=0,flag1=0,comp=0;
  1631. float avgta=0,avgwt=0;
  1632. struct queue q;
  1633. init(&q);
  1634. struct process pc;
  1635. /* pc is the current process. Since initially, there is no process in
  1636. pc, we set its flag to -1*/
  1637. pc.flag=-1;
  1638. //Obtain quantum time for Round Robin
  1639. printf("\nEnter quantum time: ");
  1640. scanf("%d",&qt);
  1641. printf("\nGANTT CHART\n\n\t|");
  1642. /*In this loop, i signifies the moment in time. A process with arrival time 1 will be said to have arrived when i=1. The loop will run until completed processes(comp) equals no. of proceses*/
  1643. //Slight modification in the loop for RR. Since the same process will run for qt moments, we increment i by qt instead of by 1
  1644. for(i=0;comp<n;i++)
  1645. {
  1646. /*In this loop, we find the processes that have arrived and place them in the queue. A process has arrived if its arrival time is less than the current moment (i)
  1647. flag is used to signify whether the process has been enqueued or not. If it has already been enqueued, it does not need to be enqueued again.
  1648. */
  1649. for(j=0;j<n;j++)
  1650. {
  1651. if(p[j].arrival<=i && p[j].flag==0)
  1652. {
  1653. p[j].flag=1;
  1654. enqueue(&q,p[j]);
  1655. }
  1656. }
  1657. /*If pc.flag=-1, it means no process has arrived upto the current moment or all arrived processes have completed execution*/
  1658. if(pc.flag==-1)
  1659. {
  1660. /*flag1 is used to indicate a gap between process execution. Eg: if p1 finishes execution at 2 and p2 begins execution at 4, we will need to show a gap in the gantt chart*/
  1661. if(empty(q) && flag1==0)
  1662. {
  1663. flag1=1;
  1664. continue;
  1665. }
  1666. else if(empty(q) && flag1==1)
  1667. continue;
  1668. else if(!empty(q) && flag1==1)
  1669. {
  1670. printf(" |");
  1671. /*Variable pretime is used to keep track of the times that will be required when displaying the Gantt chart,such as process preemption or completion. Variable cnt is the count of these times */
  1672. pretime[cnt++]=i;
  1673. flag1=0;
  1674. pc=dequeue(&q);
  1675. }
  1676. else pc=dequeue(&q);
  1677. }
  1678. /*If queue is not empty and the current process has already run for qt moments, then we replace the current process by the new process*/
  1679. else if(!empty(q) && tr==qt)
  1680. {
  1681. pretime[cnt++]=i;
  1682. printf("%s|",pc.name);
  1683. enqueue(&q,pc);
  1684. pc=dequeue(&q);
  1685. tr=0; //Reset time-run.
  1686. }
  1687. //Decrement burst time
  1688. pc.burst--;
  1689. //tr is time-run. It is required to keep track of whether a process has run for the given quantum time.
  1690. tr++;
  1691. /* If burst time is zero, process is completed. So we calculate its turnaround time and waiting time, which will later be used to calculate the average TA and WT. We print the process name in Gantt Chart and put pc.flag=-1 to indicate that there is no current process */
  1692. if(pc.burst==0)
  1693. {
  1694. turnaround=(i+1)-pc.arrival;
  1695. waiting=turnaround-pc.initburst;
  1696. avgta+=turnaround;
  1697. avgwt+=waiting;
  1698. pretime[cnt++]=i+1;
  1699. comp++;
  1700. printf("%s|",pc.name);
  1701. pc.flag=-1;
  1702. tr=0; //Reset time-run
  1703. }
  1704. }
  1705. //Display the times below the Gantt Chart
  1706. printf("\n\t0 ");
  1707. for(i=0;i<cnt;i++)
  1708. {
  1709. if(pretime[i]>9)
  1710. printf("%d ",pretime[i]);
  1711. else printf (" %d ",pretime[i]);
  1712. }
  1713. //Calculate avg. TA and WT
  1714. avgta=avgta/n;
  1715. avgwt=avgwt/n;
  1716. printf("\n\nAvg. TA time: %.2f",avgta);
  1717. printf("\n\nAvg. WT time: %.2f",avgwt);
  1718. }
  1719. -----------------------------------------------------------------------------------
  1720. */