Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct queue_data
  6. {
  7. unsigned short PID;
  8. unsigned short priority;
  9. unsigned long exec_time;
  10. unsigned int memory_start;
  11. unsigned int memory_finish;
  12. unsigned int quantum;
  13. struct generic_queue *stack_data;
  14. }Data;
  15.  
  16. typedef struct SandF
  17. {
  18. struct generic_queue *first;
  19. struct generic_queue *last;
  20. }Delimitation;
  21.  
  22. typedef struct generic_queue
  23. {
  24. void *info;
  25. struct generic_queue *next;
  26. }TCell, *TList, **aList;
  27.  
  28. int extrQueue(Delimitation *Q, aList aQ)
  29. {
  30. if (!(*Q).first)
  31. {
  32. return 0;
  33. }
  34. (*aQ) = (*Q).first;
  35. (*Q).first = (*Q).first->next;
  36. return 1;
  37. }
  38.  
  39. int insrtQueue(void* info, Delimitation *Q)
  40. {
  41. TList aux = malloc(sizeof(TCell));
  42. if (!aux)
  43. {
  44. printf("Eroare\n");
  45. return 0;
  46. }
  47. aux->info = info;
  48. aux->next = NULL;
  49. if (!(*Q).first)
  50. {
  51. (*Q).first = (*Q).last = aux;
  52. }
  53. else
  54. {
  55. (*Q).last->next = aux;
  56. (*Q).last = aux;
  57. }
  58. return 1;
  59. }
  60.  
  61. int Push(void* info, aList aS)
  62. {
  63. TList aux = malloc(sizeof(TCell));
  64. if (!aux)
  65. {
  66. printf("Eroare\n");
  67. return 0;
  68. }
  69. aux->info = info;
  70. aux->next = (*aS);
  71. (*aS) = aux;
  72. return 1;
  73. }
  74.  
  75. int Pop(aList aS, aList elem)
  76. {
  77. if (!aS)
  78. {
  79. printf("Eroare\n");
  80. return 0;
  81. }
  82. else
  83. {
  84. (*elem) = (*aS);
  85. (*aS) = (*aS)->next;
  86. (*elem)->next = NULL;
  87. }
  88. return 1;
  89. }
  90.  
  91. int sortPID(Delimitation *DelQ, Delimitation *Running, int *nr_processes)
  92. {
  93. Data *auxData = malloc(sizeof(Data));
  94. TList ListAux = malloc(sizeof(TCell));
  95. Delimitation *aux = malloc(sizeof(Delimitation));
  96. Delimitation *aux2 = malloc(sizeof(Delimitation));
  97. Delimitation *sortPID = malloc(sizeof(Delimitation));
  98. aux->first = NULL;
  99. aux2->first = NULL;
  100. sortPID->first = NULL;
  101. int auxiliar = *nr_processes;
  102. int i;
  103. int currentPID = 1;
  104. if (Running->first != NULL)
  105. {
  106. auxData = Running->first->info;
  107. insrtQueue(auxData, sortPID);
  108. }
  109. while (DelQ->first != NULL)
  110. {
  111. auxData = DelQ->first->info;
  112. extrQueue(DelQ, &ListAux);
  113. insrtQueue(auxData, sortPID);
  114. insrtQueue(auxData, aux);
  115. }
  116. while (aux->first != NULL)
  117. {
  118. auxData = aux->first->info;
  119. extrQueue(aux, &ListAux);
  120. insrtQueue(auxData, DelQ);
  121. }
  122.  
  123. while (*nr_processes != 0)
  124. {
  125. auxData = sortPID->first->info;
  126. unsigned int min_PID = auxData->PID;
  127. for (i = 0; i < *nr_processes; i++)
  128. {
  129. auxData = sortPID->first->info;
  130. if (auxData->PID < min_PID)
  131. {
  132. min_PID = auxData->PID;
  133. }
  134. extrQueue(sortPID, &ListAux);
  135. insrtQueue(auxData, aux);
  136. }
  137. (*nr_processes)--;
  138. while (aux->first != NULL)
  139. {
  140. auxData = aux->first->info;
  141. if (auxData->PID == min_PID)
  142. {
  143. insrtQueue(auxData, sortPID);
  144. extrQueue(aux, &ListAux);
  145. }
  146. else
  147. {
  148. insrtQueue(auxData, aux2);
  149. extrQueue(aux, &ListAux);
  150. }
  151. }
  152. while (sortPID->first != NULL)
  153. {
  154. auxData = sortPID->first->info;
  155. extrQueue(sortPID, &ListAux);
  156. insrtQueue(auxData, aux2);
  157. }
  158. while (aux2->first != NULL)
  159. {
  160. auxData = aux2->first->info;
  161. extrQueue(aux2, &ListAux);
  162. insrtQueue(auxData, sortPID);
  163. }
  164. }
  165. while (sortPID->first != NULL)
  166. {
  167. auxData = sortPID->first->info;
  168. if (currentPID != auxData->PID)
  169. {
  170. (*nr_processes) = auxiliar;
  171. return currentPID;
  172. }
  173. currentPID++;
  174. extrQueue(sortPID, &ListAux);
  175. }
  176. (*nr_processes) = auxiliar;
  177. return currentPID;
  178. }
  179.  
  180. int sortMemory(Delimitation *Mem, Delimitation* DelQ, Delimitation* Running, int *nr_processes)
  181. {
  182. Data *auxData = malloc(sizeof(Data));
  183. TList ListAux = malloc(sizeof(TCell));
  184. Delimitation *aux = malloc(sizeof(Delimitation));
  185. Delimitation *aux2 = malloc(sizeof(Delimitation));
  186. aux->first = NULL;
  187. aux2->first = NULL;
  188. Mem->first = NULL;
  189. int auxiliary = *nr_processes;
  190. int i;
  191. if (Running->first != NULL)
  192. {
  193. auxData = Running->first->info;
  194. insrtQueue(auxData, Mem);
  195. }
  196. while (DelQ->first != NULL)
  197. {
  198. auxData = DelQ->first->info;
  199. extrQueue(DelQ, &ListAux);
  200. insrtQueue(auxData, Mem);
  201. insrtQueue(auxData, aux);
  202. }
  203. while (aux->first != NULL)
  204. {
  205. auxData = aux->first->info;
  206. extrQueue(aux, &ListAux);
  207. insrtQueue(auxData, DelQ);
  208. }
  209.  
  210. while (*nr_processes > 1)
  211. {
  212. auxData = Mem->first->info;
  213. unsigned int min_memory = auxData->memory_start;
  214. int min_PID = auxData->PID;
  215. for (i = 0; i < *nr_processes; i++)
  216. {
  217. auxData = Mem->first->info;
  218. if (auxData->memory_start < min_memory)
  219. {
  220. min_memory = auxData->memory_start;
  221. min_PID = auxData->PID;
  222. }
  223. extrQueue(Mem, &ListAux);
  224. insrtQueue(auxData, aux);
  225. }
  226. (*nr_processes)--;
  227. while (aux->first != NULL)
  228. {
  229. auxData = aux->first->info;
  230. if (auxData->PID == min_PID)
  231. {
  232. insrtQueue(auxData, Mem);
  233. extrQueue(aux, &ListAux);
  234. }
  235. else
  236. {
  237. insrtQueue(auxData, aux2);
  238. extrQueue(aux, &ListAux);
  239. }
  240. }
  241. while (Mem->first != NULL)
  242. {
  243. auxData = Mem->first->info;
  244. extrQueue(Mem, &ListAux);
  245. insrtQueue(auxData, aux2);
  246. }
  247. while (aux2->first != NULL)
  248. {
  249. auxData = aux2->first->info;
  250. extrQueue(aux2, &ListAux);
  251. insrtQueue(auxData, Mem);
  252. }
  253. }
  254. *nr_processes = auxiliary;
  255. return 1;
  256. }
  257.  
  258. void freeMemory(Delimitation* DelMem, Delimitation* Mem, Delimitation *DelQ, Delimitation *Running, int *nr_processes)
  259. {
  260. int m_finish1 = 0;
  261. Delimitation *DelAux = malloc(sizeof(Delimitation));
  262. TList ListAux = malloc(sizeof(TCell));
  263. TList aux = DelAux->first;
  264. DelAux->first = NULL;
  265. DelMem->first = NULL;
  266. sortMemory(Mem, DelQ, Running, nr_processes);
  267. while (Mem->first != NULL)
  268. {
  269. Data *aux2;
  270. aux2 = Mem->first->info;
  271. int m_start2, m_finish2, s;
  272. m_start2 = aux2->memory_start;
  273. m_finish2 = aux2->memory_finish;
  274. s = m_start2 - m_finish1;
  275. insrtQueue(s, DelMem);
  276. extrQueue(Mem, &ListAux);
  277. insrtQueue(aux2, DelAux);
  278. m_finish1 = m_finish2;
  279. }
  280. while (DelAux->first != NULL)
  281. {
  282. aux = DelAux->first;
  283. Data *aux2 = aux->info;
  284. extrQueue(DelAux, &aux);
  285. insrtQueue(aux2, Mem);
  286. }
  287. insrtQueue((int*)3145728 - m_finish1, DelMem);
  288. }
  289.  
  290. int add(unsigned int memory, unsigned int time, unsigned short priority,
  291. Delimitation *DelQ, Delimitation *MemDelim,
  292. Delimitation *Running, int *nr_processes, Delimitation *Mem)
  293. {
  294. Data *queueData = malloc(sizeof(Data));
  295. queueData->exec_time = time;
  296. queueData->priority = priority;
  297. queueData->stack_data = NULL;
  298. TList ListAux = malloc(sizeof(TCell));
  299. int currentPID = sortPID(DelQ, Running, nr_processes);
  300. if (DelQ->first == NULL && Running->first == NULL)
  301. {
  302. TList aux = malloc(sizeof(TCell));
  303. if (!aux)
  304. {
  305. printf("Eroare\n");
  306. return 0;
  307. }
  308. queueData->PID = 1;
  309. queueData->memory_start = 0;
  310. queueData->memory_finish = memory - 1;
  311. insrtQueue(queueData, Running);
  312. (*nr_processes)++;
  313. printf("Process created successfully: PID: %d, Memory starts at %x.\n", queueData->PID, queueData->memory_start);
  314. }
  315. else
  316. {
  317. TList aux = malloc(sizeof(TCell));
  318. Delimitation *DelAux = malloc(sizeof(Delimitation));
  319. Data *auxData = malloc(sizeof(Data));
  320. Data *auxData2 = malloc(sizeof(Data));
  321. DelAux->first = NULL;
  322. if (!aux)
  323. {
  324. printf("Eroare\n");
  325. return 0;
  326. }
  327. int i = 0;
  328. while ((unsigned int)MemDelim->first->info < memory)
  329. {
  330. if (MemDelim->first == NULL)
  331. {
  332. printf("Cannot reserve memory for PID 0x%d.\n", currentPID);
  333. return 0;
  334. }
  335. extrQueue(MemDelim, &ListAux);
  336. if (i > 0)
  337. {
  338. auxData = Mem->first->info;
  339. extrQueue(Mem, &ListAux);
  340. insrtQueue(auxData, DelAux);
  341. }
  342. i++;
  343. }
  344. auxData = Mem->first->info;
  345. queueData->PID = currentPID;
  346. queueData->memory_start = auxData->memory_finish + 1;
  347. queueData->memory_finish = queueData->memory_start + memory - 1;
  348. insrtQueue(queueData, DelAux);
  349. printf("Process created successfully: PID: %d, Memory starts at 0x%x.\n", queueData->PID, queueData->memory_start);
  350. (*nr_processes)++;
  351. while (DelQ->first != NULL)
  352. {
  353. auxData = DelQ->first->info;
  354. extrQueue(DelQ, &ListAux);
  355. insrtQueue(auxData, DelAux);
  356. }
  357. while (DelAux->first != NULL)
  358. {
  359. aux = DelAux->first;
  360. auxData = aux->info;
  361. extrQueue(DelAux, &aux);
  362. insrtQueue(auxData, DelQ);
  363. }
  364. }
  365. freeMemory(MemDelim, Mem, DelQ, Running, nr_processes);
  366. return 1;
  367. }
  368.  
  369. int sortQueue(Delimitation* DelQ, int *nr_processes, Delimitation *Finished, Delimitation *Running)
  370. {
  371. Data *auxData = malloc(sizeof(Data));
  372. Delimitation *aux = malloc(sizeof(Delimitation));
  373. Delimitation *aux2 = malloc(sizeof(Delimitation));
  374. TList ListAux = malloc(sizeof(TCell));
  375. aux->first = NULL;
  376. aux2->first = NULL;
  377. int i;
  378. int auxiliary = *nr_processes;
  379. int prevPID = 0;
  380.  
  381. if (Running->first != NULL)
  382. {
  383. auxData = Running->first->info;
  384. extrQueue(Running, &ListAux);
  385. if (auxData->exec_time == 0)
  386. {
  387. insrtQueue(auxData, Finished);
  388. }
  389. else
  390. {
  391. insrtQueue(auxData, DelQ);
  392. prevPID = auxData->PID;
  393. }
  394. }
  395.  
  396. while (DelQ->first != NULL)
  397. {
  398. auxData = DelQ->first->info;
  399. extrQueue(DelQ, &ListAux);
  400. if (auxData->exec_time == 0)
  401. {
  402. insrtQueue(auxData, Finished);
  403. }
  404. else
  405. {
  406. insrtQueue(auxData, aux);
  407. }
  408. }
  409. while (aux->first != NULL)
  410. {
  411. auxData = aux->first->info;
  412. extrQueue(aux, &ListAux);
  413. insrtQueue(auxData, DelQ);
  414. }
  415.  
  416. while (*nr_processes > 0)
  417. {
  418. auxData = DelQ->first->info;
  419. int min_time = auxData->exec_time;
  420. int min_pirority = auxData->priority;
  421. int min_PID = auxData->PID;
  422. for (i = 0; i < *nr_processes; i++)
  423. {
  424. auxData = DelQ->first->info;
  425. if (auxData->priority > min_pirority)
  426. {
  427. min_pirority = auxData->priority;
  428. min_time = auxData->exec_time;
  429. min_PID = auxData->PID;
  430. }
  431. else if (auxData->priority == min_pirority)
  432. {
  433. if (auxData->exec_time < min_time)
  434. {
  435. min_pirority = auxData->priority;
  436. min_time = auxData->exec_time;
  437. min_PID = auxData->PID;
  438. }
  439. else if (auxData->exec_time == min_time)
  440. {
  441. if (auxData->PID < min_PID)
  442. {
  443. min_pirority = auxData->priority;
  444. min_time = auxData->exec_time;
  445. min_PID = auxData->PID;
  446. }
  447. }
  448. }
  449. extrQueue(DelQ, &ListAux);
  450. insrtQueue(auxData, aux);
  451. }
  452. (*nr_processes)--;
  453. while (aux->first != NULL)
  454. {
  455. auxData = aux->first->info;
  456. if (auxData->PID == min_PID)
  457. {
  458. insrtQueue(auxData, DelQ);
  459. extrQueue(aux, &ListAux);
  460. }
  461. else
  462. {
  463. insrtQueue(auxData, aux2);
  464. extrQueue(aux, &ListAux);
  465. }
  466. }
  467. //printf("\n\n");
  468. //print_waiting(DelQ);
  469. while (DelQ->first != NULL)
  470. {
  471. auxData = DelQ->first->info;
  472. extrQueue(DelQ, &ListAux);
  473. insrtQueue(auxData, aux2);
  474. }
  475. while (aux2->first != NULL)
  476. {
  477. auxData = aux2->first->info;
  478. extrQueue(aux2, &ListAux);
  479. insrtQueue(auxData, DelQ);
  480. }
  481. }
  482. if (DelQ->first != NULL)
  483. {
  484. auxData = DelQ->first->info;
  485. extrQueue(DelQ, &ListAux);
  486. insrtQueue(auxData, Running);
  487. }
  488. *nr_processes = auxiliary;
  489. return 1;
  490. }
  491.  
  492. int get(Delimitation* DelQ, Delimitation *Finished, int searchedPID, Delimitation *Running)
  493. {
  494. Data *auxData = malloc(sizeof(Data));
  495. TList ListAux = malloc(sizeof(TCell));
  496. Delimitation *aux = malloc(sizeof(Delimitation));
  497. aux->first = NULL;
  498. int found = 0;
  499.  
  500. auxData = Running->first->info;
  501. if (auxData->PID == searchedPID)
  502. {
  503.  
  504. printf("Process %d is running (remaining_time: %d)\n", searchedPID, auxData->exec_time);
  505. found = 1;
  506. }
  507.  
  508. if (found == 1)
  509. {
  510. return 1;
  511. }
  512.  
  513. while (DelQ->first != NULL)
  514. {
  515. auxData = DelQ->first->info;
  516. extrQueue(DelQ, &ListAux);
  517. insrtQueue(auxData, aux);
  518. if (auxData->PID == searchedPID)
  519. {
  520. printf("Process %d is waiting (remaining_time: %d)\n", searchedPID, auxData->exec_time);
  521. found = 1;
  522. }
  523. }
  524. while (aux->first != NULL)
  525. {
  526. auxData = aux->first->info;
  527. extrQueue(aux, &ListAux);
  528. insrtQueue(auxData, DelQ);
  529. }
  530. if (found == 0)
  531. {
  532. while (Finished->first != NULL)
  533. {
  534. auxData = Finished->first->info;
  535. extrQueue(Finished, &ListAux);
  536. insrtQueue(auxData, aux);
  537. if (auxData->PID == searchedPID)
  538. {
  539. printf("Process %d is finished.\n", searchedPID);
  540. found = 1;
  541. }
  542. }
  543. while (aux->first != NULL)
  544. {
  545.  
  546. auxData = aux->first->info;
  547. extrQueue(aux, &ListAux);
  548. insrtQueue(auxData, Finished);
  549. }
  550. }
  551. if (found == 0)
  552. {
  553. printf("Process %d is not found.\n", searchedPID);
  554. }
  555. return 1;
  556. }
  557.  
  558. int push_stack(Delimitation* DelQ, int searchedPID, int dataBytes, Delimitation *Running)
  559. {
  560. Data *auxData = malloc(sizeof(Data));
  561. TList ListAux = malloc(sizeof(TCell));
  562. Delimitation *aux = malloc(sizeof(Delimitation));
  563. aux->first = NULL;
  564. int pushed = 0;
  565.  
  566. auxData = Running->first->info;
  567. if (auxData->PID == searchedPID)
  568. {
  569. Push((int *)dataBytes, &(auxData->stack_data));
  570. pushed = 1;
  571. }
  572.  
  573. if (pushed == 1)
  574. {
  575. return 1;
  576. }
  577.  
  578. while (DelQ->first != NULL)
  579. {
  580. auxData = DelQ->first->info;
  581. extrQueue(DelQ, &ListAux);
  582. insrtQueue(auxData, aux);
  583. if (auxData->PID == searchedPID)
  584. {
  585. Push((int*)dataBytes, &auxData->stack_data);
  586. pushed = 1;
  587. }
  588. }
  589. while (aux->first != NULL)
  590. {
  591. auxData = aux->first->info;
  592. extrQueue(aux, &ListAux);
  593. insrtQueue(auxData, DelQ);
  594. }
  595. if (pushed == 0)
  596. {
  597. printf("PID %d not found.\n", searchedPID);
  598. }
  599. return 1;
  600. }
  601.  
  602. int pop_stack(Delimitation* DelQ, int searchedPID, Delimitation *Running)
  603. {
  604. Data *auxData = malloc(sizeof(Data));
  605. TList ListAux = malloc(sizeof(TCell));
  606. Delimitation *aux = malloc(sizeof(Delimitation));
  607. aux->first = NULL;
  608. int popped = 0;
  609.  
  610. auxData = Running->first->info;
  611. if (auxData->PID == searchedPID)
  612. {
  613. if (auxData->stack_data == NULL)
  614. {
  615. printf("Empty stack PID %d.\n", searchedPID);
  616. }
  617. else
  618. {
  619. Pop(&auxData->stack_data, &ListAux);
  620. popped = 1;
  621. }
  622. }
  623.  
  624. if (popped == 1)
  625. {
  626. return 1;
  627. }
  628.  
  629. while (DelQ->first != NULL)
  630. {
  631. auxData = DelQ->first->info;
  632. extrQueue(DelQ, &ListAux);
  633. insrtQueue(auxData, aux);
  634. if (auxData->PID == searchedPID)
  635. {
  636. if (auxData->stack_data == NULL)
  637. {
  638. popped = 1;
  639. printf("Empty stack PID %d.\n", searchedPID);
  640. }
  641. else
  642. {
  643. Pop(&auxData->stack_data, &ListAux);
  644. popped = 1;
  645. }
  646. }
  647. }
  648. while (aux->first != NULL)
  649. {
  650. auxData = aux->first->info;
  651. extrQueue(aux, &ListAux);
  652. insrtQueue(auxData, DelQ);
  653. }
  654. if (popped == 0)
  655. {
  656. printf("PID %d not found.\n", searchedPID);
  657. }
  658. return 1;
  659. }
  660.  
  661. int print_stack(Delimitation* DelQ, int searchedPID, Delimitation *Running)
  662. {
  663. Data *auxData = malloc(sizeof(Data));
  664. TList ListAux = NULL;
  665. TList ListAux2 = NULL;
  666. Delimitation *aux = malloc(sizeof(Delimitation));
  667. aux->first = NULL;
  668. int popped = 0;
  669.  
  670. auxData = Running->first->info;
  671. if (auxData->PID == searchedPID)
  672. {
  673. if (auxData->stack_data == NULL)
  674. {
  675. popped = 1;
  676. printf("Empty stack PID %d.\n", searchedPID);
  677. }
  678. else
  679. {
  680. popped = 1;
  681. printf("Stack of PID %d:", searchedPID);
  682. while (auxData->stack_data != NULL)
  683. {
  684. Pop(&auxData->stack_data, &ListAux);
  685. printf(" %d", (int)ListAux->info);
  686. Push((int*)ListAux->info, &ListAux2);
  687. }
  688. printf(".\n");
  689. while (ListAux2 != NULL)
  690. {
  691. Pop(&ListAux2, &ListAux);
  692. Push((int*)ListAux->info, &auxData->stack_data);
  693. }
  694. }
  695. }
  696.  
  697. while (DelQ->first != NULL)
  698. {
  699. auxData = DelQ->first->info;
  700. extrQueue(DelQ, &ListAux);
  701. insrtQueue(auxData, aux);
  702. if (auxData->PID == searchedPID)
  703. {
  704. if (auxData->stack_data == NULL)
  705. {
  706. popped = 1;
  707. printf("Empty stack PID %d.\n", searchedPID);
  708. }
  709. else
  710. {
  711. popped = 1;
  712. printf("Stack of PID %d:", searchedPID);
  713. while (auxData->stack_data != NULL)
  714. {
  715. Pop(&auxData->stack_data, &ListAux);
  716. printf(" %d", (int)ListAux->info);
  717. Push((int*)ListAux->info, &ListAux2);
  718. }
  719. printf(".\n");
  720. while (ListAux2 != NULL)
  721. {
  722. Pop(&ListAux2, &ListAux);
  723. Push((int*)ListAux->info, &auxData->stack_data);
  724. }
  725. }
  726. }
  727. }
  728.  
  729.  
  730. while (aux->first != NULL)
  731. {
  732. auxData = aux->first->info;
  733. extrQueue(aux, &ListAux);
  734. insrtQueue(auxData, DelQ);
  735. }
  736. if (popped == 0)
  737. {
  738. printf("PID %d not found.\n", searchedPID);
  739. }
  740. return 1;
  741. }
  742.  
  743. int print_waiting(Delimitation* DelQ)
  744. {
  745. Data *auxData = malloc(sizeof(Data));
  746. TList ListAux = malloc(sizeof(TCell));
  747. Delimitation *aux = malloc(sizeof(Delimitation));
  748. aux->first = NULL;
  749.  
  750. printf("Waiting queue:\n[");
  751. while (DelQ->first != NULL)
  752. {
  753. auxData = DelQ->first->info;
  754. extrQueue(DelQ, &ListAux);
  755. printf("(%d: priority = %d, remaining_time = %d)", auxData->PID, auxData->priority, auxData->exec_time);
  756. if (DelQ->first != NULL)
  757. {
  758. printf(",\n");
  759. }
  760. insrtQueue(auxData, aux);
  761. }
  762. printf("]\n");
  763.  
  764. while (aux->first != NULL)
  765. {
  766. auxData = aux->first->info;
  767. extrQueue(aux, &ListAux);
  768. insrtQueue(auxData, DelQ);
  769. }
  770.  
  771. return 1;
  772. }
  773.  
  774. int print_finished(Delimitation* Finished)
  775. {
  776. Data *auxData = malloc(sizeof(Data));
  777. TList ListAux = malloc(sizeof(TCell));
  778. Delimitation *aux = malloc(sizeof(Delimitation));
  779. aux->first = NULL;
  780.  
  781. printf("Finished queue:\n[");
  782. while (Finished->first != NULL)
  783. {
  784. auxData = Finished->first->info;
  785. extrQueue(Finished, &ListAux);
  786. printf("(%d: priority = %d, remaining_time = %d)", auxData->PID, auxData->priority, auxData->exec_time);
  787. if (Finished->first != NULL)
  788. {
  789. printf(",\n");
  790. }
  791. insrtQueue(auxData, aux);
  792. }
  793. printf("]\n");
  794.  
  795. while (aux->first != NULL)
  796. {
  797. auxData = aux->first->info;
  798. extrQueue(aux, &ListAux);
  799. insrtQueue(auxData, Finished);
  800. }
  801.  
  802. return 1;
  803. }
  804.  
  805. int finish(Delimitation *DelQ, Delimitation *Running)
  806. {
  807. Data *auxData = malloc(sizeof(Data));
  808. TList ListAux = malloc(sizeof(TCell));
  809. int s = 0;
  810.  
  811. while (DelQ->first != NULL)
  812. {
  813. auxData = DelQ->first->info;
  814. s = s + auxData->exec_time;
  815. extrQueue(DelQ, &ListAux);
  816. }
  817.  
  818. while (Running->first != NULL)
  819. {
  820. auxData = Running->first->info;
  821. s = s + auxData->exec_time;
  822. extrQueue(Running, &ListAux);
  823. }
  824. printf("Total time: %d\n", s);
  825. return s;
  826. }
  827.  
  828. int run(int time_to_run, int quantum, Delimitation *DelQ, Delimitation *Running, Delimitation *Finished, int *nr_processes)
  829. {
  830. Data *auxData = malloc(sizeof(Data));
  831. TList ListAux = malloc(sizeof(TCell));
  832. Delimitation *aux = malloc(sizeof(aux));
  833. aux->first = NULL;
  834. int i, dif;
  835. sortQueue(DelQ, nr_processes, Finished, Running);
  836. if (Running->first == NULL)
  837. {
  838. sortQueue(DelQ, nr_processes, Finished, Running);
  839. auxData = Running->first->info;
  840. auxData->quantum = quantum;
  841. }
  842. int j = 1;
  843. for (i = time_to_run; i > 0; i = i - quantum)
  844. {
  845. //printf("%d\n", j);
  846. //j++;
  847. if (i > quantum)
  848. {
  849. dif = quantum;
  850. }
  851. else
  852. {
  853. dif = i;
  854. }
  855. auxData = Running->first->info;
  856. if (auxData->quantum == 0)
  857. {
  858. auxData->quantum = quantum;
  859. if (DelQ->first != NULL)
  860. {
  861. extrQueue(Running, &ListAux);
  862. insrtQueue(auxData, aux);
  863. if (DelQ->first != NULL)
  864. {
  865. auxData = DelQ->first->info;
  866. extrQueue(DelQ, &ListAux);
  867. insrtQueue(auxData, Running);
  868. while (DelQ->first != NULL)
  869. {
  870. auxData = DelQ->first->info;
  871. extrQueue(DelQ, &ListAux);
  872. insrtQueue(auxData, aux);
  873. }
  874. while (aux->first != NULL)
  875. {
  876. auxData = aux->first->info;
  877. extrQueue(aux, &ListAux);
  878. insrtQueue(auxData, DelQ);
  879. }
  880. }
  881. }
  882. }
  883. if (auxData->quantum < dif)
  884. {
  885. if (auxData->exec_time <= auxData->quantum)
  886. {
  887. dif = dif - auxData->exec_time;
  888. extrQueue(Running, &ListAux);
  889. auxData->exec_time = 0;
  890. insrtQueue(auxData, Finished);
  891. (*nr_processes)--;
  892. sortQueue(DelQ, nr_processes, Finished, Running);
  893. auxData = Running->first->info;
  894. auxData->quantum = quantum;
  895. }
  896. else
  897. {
  898. dif = dif - auxData->exec_time;
  899. auxData->exec_time -= auxData->quantum;
  900. sortQueue(DelQ, nr_processes, Finished, Running);
  901. auxData = Running->first->info;
  902. auxData->quantum = quantum;
  903. }
  904. }
  905. while(dif != 0)
  906. {
  907. //sortQueue(DelQ, nr_processes, Finished, Running);
  908. auxData = Running->first->info;
  909. if (auxData->quantum == 0)
  910. {
  911. auxData->quantum = quantum;
  912. if (DelQ->first != NULL)
  913. {
  914. extrQueue(Running, &ListAux);
  915. insrtQueue(auxData, aux);
  916. if (DelQ->first != NULL)
  917. {
  918. auxData = DelQ->first->info;
  919. extrQueue(DelQ, &ListAux);
  920. insrtQueue(auxData, Running);
  921. while (DelQ->first != NULL)
  922. {
  923. auxData = DelQ->first->info;
  924. extrQueue(DelQ, &ListAux);
  925. insrtQueue(auxData, aux);
  926. }
  927. while (aux->first != NULL)
  928. {
  929. auxData = aux->first->info;
  930. extrQueue(aux, &ListAux);
  931. insrtQueue(auxData, DelQ);
  932. }
  933. }
  934. }
  935. }
  936. else
  937. {
  938. while (auxData->exec_time < dif)
  939. {
  940. dif = dif - auxData->exec_time;
  941. extrQueue(Running, &ListAux);
  942. auxData->exec_time = 0;
  943. insrtQueue(auxData, Finished);
  944. (*nr_processes)--;
  945. if (*nr_processes == 0)
  946. {
  947. break;
  948. }
  949. sortQueue(DelQ, nr_processes, Finished, Running);
  950. auxData = Running->first->info;
  951. auxData->quantum = quantum;
  952. }
  953. if (auxData->exec_time == dif)
  954. {
  955. dif = 0;
  956. extrQueue(Running, &ListAux);
  957. auxData->exec_time = 0;
  958. insrtQueue(auxData, Finished);
  959. (*nr_processes)--;
  960. sortQueue(DelQ, nr_processes, Finished, Running);
  961. auxData = Running->first->info;
  962. auxData->quantum = quantum;
  963. }
  964. if (auxData->exec_time > dif)
  965. {
  966. auxData = Running->first->info;
  967. auxData->exec_time -= dif;
  968. dif = 0;
  969. auxData->quantum = 0;
  970. }
  971. }
  972.  
  973. }
  974. }
  975. return 1;
  976. }
  977.  
  978. int execute(int argc, char *argv[])
  979. {
  980. char buffer[1000], *p, *comanda, arg_4[1000];
  981. int arg_1, arg_2, arg_3;
  982. FILE *in = fopen(argv[1], "r");
  983. Delimitation *DelQ = malloc(sizeof(Delimitation));
  984. Delimitation *MemDelim = malloc(sizeof(Delimitation));
  985. Delimitation *Running = malloc(sizeof(Delimitation));
  986. Delimitation *Mem = malloc(sizeof(Delimitation));
  987. Delimitation *Finished = malloc(sizeof(Delimitation));
  988. DelQ->first = NULL;
  989. MemDelim->first = NULL;
  990. Running->first = NULL;
  991. Mem->first = NULL;
  992. Finished->first = NULL;
  993. int nr_processes = 0, quantum;
  994. fscanf(in, "%d", &quantum);
  995. while (fscanf(in, "%s", buffer) == 1)
  996. {
  997. p = strtok(buffer, " \n");
  998. comanda = malloc(sizeof(p) + 1);
  999. strcpy(comanda, p);
  1000. if (strcmp(comanda, "add") == 0)
  1001. {
  1002. fscanf(in, "%d", &arg_1);
  1003. fscanf(in, "%d", &arg_2);
  1004. fscanf(in, "%d", &arg_3);
  1005. add(arg_1, arg_2, arg_3, DelQ, MemDelim, Running, &nr_processes, Mem);
  1006. }
  1007. else if (strcmp(comanda, "get") == 0)
  1008. {
  1009. fscanf(in, "%d", &arg_1);
  1010. get(DelQ, Finished, arg_1, Running);
  1011. }
  1012. else if (strcmp(comanda, "push") == 0)
  1013. {
  1014. fscanf(in, "%d", &arg_1);
  1015. fscanf(in, "%d", &arg_2);
  1016. push_stack(DelQ, arg_1, arg_2, Running);
  1017. }
  1018. else if (strcmp(comanda, "pop") == 0)
  1019. {
  1020. fscanf(in, "%d", &arg_1);
  1021. pop_stack(DelQ, arg_1, Running);
  1022. }
  1023. else if (strcmp(comanda, "print") == 0)
  1024. {
  1025. fscanf(in, "%s", arg_4);
  1026. if (strcmp(arg_4, "stack") == 0)
  1027. {
  1028. fscanf(in, "%d", &arg_1);
  1029. print_stack(DelQ, arg_1, Running);
  1030. }
  1031. else if (strcmp(arg_4, "waiting") == 0)
  1032. {
  1033. sortQueue(DelQ, &nr_processes, Finished, Running);
  1034. print_waiting(DelQ);
  1035. }
  1036. else if (strcmp(arg_4, "finished") == 0)
  1037. {
  1038. print_finished(Finished);
  1039. }
  1040. }
  1041. else if (strcmp(comanda, "run") == 0)
  1042. {
  1043. fscanf(in, "%d", &arg_1);
  1044. run(arg_1, quantum, DelQ, Running, Finished, &nr_processes);
  1045. }
  1046. else if (strcmp(comanda, "finish") == 0)
  1047. {
  1048. finish(DelQ, Running);
  1049. }
  1050. }
  1051. fclose(in);
  1052. }
  1053.  
  1054. int main(int argc, char *argv[])
  1055. {
  1056. TList Q = NULL;
  1057. TList mem = NULL;
  1058. Delimitation *DelQ = malloc(sizeof(Delimitation));
  1059. Delimitation *MemDelim = malloc(sizeof(Delimitation));
  1060. Delimitation *Finished = malloc(sizeof(Delimitation));
  1061. DelQ->first = NULL;
  1062. MemDelim->first = NULL;
  1063. Finished->first = NULL;
  1064. execute(argc, argv);
  1065. //add(100, 20, 2, Q, mem, DelQ, MemDelim, 1);
  1066. //add(200, 30, 3, Q, mem, DelQ, MemDelim, 2);
  1067. //add(100, 20, 2, Q, mem, DelQ, MemDelim, 1);
  1068. //sortQueue(DelQ, 2, Finished);
  1069. Data *x = malloc(sizeof(Data));
  1070. //x = DelQ->first->info;
  1071. //get(DelQ, Finished, 1);
  1072. return 0;
  1073. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement