Advertisement
Guest User

backupdone

a guest
Mar 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define n 65
  6. #define block 5
  7.  
  8. int indexToBlock(int cell);
  9. int blockToIndex(int blk);
  10. int powerExp(int exp);
  11. int power(int value, int powerValue);
  12. int findReqBlocksWithPtr(int rows);
  13. int *findFreeBlock(int reqBlocks, long decBitmap, char* method);
  14. long decimalToBinary(long dec);
  15. long binaryToDecimal(long bin);
  16. int *getDirectoryData(int data, char* method);
  17. int *newMemory();
  18. void printDiskMap(char* method, int *memArray);
  19. void readCommand(char* method, int datafile, int *memArray);
  20. int *readcsv();
  21. void AddFile(char* method, int *memory);
  22. int concat123(int xxx, int yyy);
  23.  
  24.  
  25.  
  26. /* main function to call above defined function */
  27. int main () {
  28.  
  29. int *memory;
  30. char * allocMethod;
  31.  
  32.  
  33. free(memory);
  34. allocMethod = "contiguous";
  35. memory = newMemory();
  36. memory[0]=1000114;
  37. memory[1]= 22001505;
  38.  
  39. AddFile(allocMethod,memory);
  40. printDiskMap(allocMethod,memory);
  41.  
  42.  
  43. /* allocMethod = "indexed";
  44. memory = newMemory();
  45. memory[0]=10001;
  46. memory[1]= 220015;
  47. //printDiskMap(allocMethod,memory);
  48.  
  49. *
  50. *
  51. *
  52. * free(memory);
  53. allocMethod = "linked";
  54. memory = newMemory();
  55.  
  56. memory[0]=1000104;
  57. memory[1]= 2000505;
  58.  
  59. memory[10]= 101;
  60. memory[11]= 102;
  61. memory[12] = 103;
  62. memory[13] = 104;
  63. memory[14] = 4;
  64. memory[20] = 105;
  65. memory[21]= 106;
  66. memory[25] = 201;
  67. memory[26] = 202;
  68. memory[27] = 203;*//*
  69. //printDiskMap(allocMethod,memory);*/
  70.  
  71. /* testing getDirectoryData function
  72. int *dataArray;
  73. dataArray = getDirectoryData(678911, "linked");
  74. printf("Filename: %d | start: %d | end: %d\n",dataArray[0],dataArray[1],dataArray[2]);
  75. */
  76.  
  77. /* testing findFreeBlock function
  78. long todec = 1111100000;
  79. printf("%ld\n",todec);
  80.  
  81. int *testArray;
  82. long bitmap = binaryToDecimal(todec);
  83. testArray = findFreeBlock(3, bitmap, "contiguous");
  84. //printf("Contiguous free block: %d\n\n",testArray[0]);
  85.  
  86. testArray = findFreeBlock(6, bitmap, "linked");
  87. printf("linked free block: ");
  88. for (int i = 0; i < 6; i++){
  89. //printf("%d ",testArray[i]);
  90. }
  91.  
  92. testArray = findFreeBlock(5, bitmap, "indexed");
  93. printf("\nindexed free block: ");
  94. for (int i = 0; i < 6; i++){
  95. printf("%d ",testArray[i]);
  96. }
  97. */
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. FILE *fp;
  106. char buffer[255];
  107. char * split;
  108. char * retrieveData;
  109. fp = fopen("../data.csv", "r");
  110. // if there is no such file available
  111. // display an error message, break from the method
  112. if (fp == NULL) {
  113. printf("Could not open.\n");
  114. return 0;
  115. }
  116. else {
  117. retrieveData = "";
  118. int count = 0;
  119. fgets(buffer, 255, (FILE*)fp);
  120. // removes \n at the end of the line
  121. buffer[strcspn(buffer, "\n")] = 0;
  122. // splits the line with space as the delimiter
  123. split = strtok(buffer, ", ");
  124.  
  125. while (split != NULL) {
  126. retrieveData = split;
  127. split = strtok(NULL, ", ");
  128.  
  129. if (count == 1){
  130. printf("First: %s\n",retrieveData);
  131. }
  132.  
  133. if (split == NULL){
  134. printf("Last: %s\n",retrieveData);
  135. count = 0;
  136. }
  137. else {
  138. count++;
  139. }
  140.  
  141.  
  142. }
  143. }
  144. fclose(fp);
  145. return 0;
  146. }
  147.  
  148.  
  149. /////////////////////////// MAIN END HERE /////////////////////////////////////
  150.  
  151.  
  152.  
  153.  
  154. void AddFile(char* method, int *memory) {
  155.  
  156. /////////////////////
  157. long todec = 11111000000; // total 11 bits
  158. //printf("%ld\n",todec);
  159.  
  160. long bitmap = binaryToDecimal(todec);
  161. //printf("%ld\n",bitmap);
  162. long bitmapconvertback = decimalToBinary(bitmap);
  163. //printf("%ld\n",bitmapconvertback);
  164. ////////////////////////////
  165. memory[5] = bitmap;
  166.  
  167. // void add file function name/ parameter is method / 'memory'
  168.  
  169. // use >>> create a dummy bitmap and use the below function to convert before placing it at cell 5
  170. //long decimalToBinary(long dec);
  171. //long binaryToDecimal(long bin);
  172.  
  173. //int lol = findFreeBlock(needhowmanyblock, memory[5],"linked");
  174.  
  175. // put stuff at directory
  176.  
  177. // create 3 if statement for various method >> each counter for each method (reset everytime counter = 0)
  178.  
  179.  
  180. if(strcmp(method, "contiguous") == 0){
  181.  
  182. int testing[3];
  183. testing[0] = 0;
  184. testing[1] = 100;
  185. testing[2] = 106;
  186.  
  187.  
  188. int result = testing[2] - testing[1];
  189. //printf("%d\n", result);
  190. int needhowmanyblock = findReqBlocksWithPtr(result);
  191. //printf("%d\n", needhowmanyblock);
  192.  
  193. int* testArray;
  194.  
  195. testArray = findFreeBlock(needhowmanyblock, memory[5], method);
  196. //printf("%d\n", *testArray);
  197.  
  198. int firstconcat = concat123(testing[1],*testArray);
  199. int secondconcat = concat123(firstconcat,needhowmanyblock);
  200. //printf("%d\n", firstconcat);
  201. //printf("%d\n", secondconcat);
  202.  
  203. for (int i = 0; i < block; i++) {
  204.  
  205. if (memory[i] == -1) {
  206. memory[i] = secondconcat;
  207. //printf("%d\n", memory[i]);
  208. break;
  209. }
  210. }
  211.  
  212. for (int i = *testArray*5; i <= *testArray*5+result; i++) {
  213. memory[i] = testing[1]++;
  214. }
  215. //put counter for time here for adding
  216.  
  217. }
  218.  
  219. if(strcmp(method, "linked") == 0){
  220. //put counter for time here
  221.  
  222.  
  223.  
  224. //put counter for time here for adding
  225.  
  226. }
  227.  
  228. if(strcmp(method, "index") == 0){
  229.  
  230. //put counter for time here for adding
  231. }
  232.  
  233.  
  234. }
  235.  
  236.  
  237. int concat123(int xxx, int yyy){
  238. int temp=0;
  239. int z=xxx;
  240. while(yyy>0)
  241. {
  242. // take reciprocal of y into temp
  243. temp=(temp*10)+(yyy%10);
  244. yyy=yyy/10;
  245. }
  246. while(temp>0)
  247. {
  248. // take each number from last of temp and add to last of z
  249. z=(z*100)+(temp%10);
  250. temp=temp/10;
  251.  
  252. }
  253. return z;
  254. //printf("%d\n",z);
  255.  
  256. }
  257.  
  258.  
  259. int indexToBlock(int cell){
  260. return cell/5;
  261. }
  262.  
  263. int blockToIndex(int blk){
  264. return blk*5;
  265. }
  266.  
  267. int powerExp(int exp){
  268. int result = 1;
  269. int base = 10;
  270. for (exp;exp>0;exp--){
  271. result *= base;
  272. }
  273. return result;
  274.  
  275. }
  276.  
  277. int power(int value, int powerValue){
  278. int result = 1;
  279. for (int i = 0; i < powerValue; i++){
  280. result *= value;
  281. }
  282. return result;
  283. }
  284.  
  285. int findReqBlocksWithPtr(int rows){
  286. int reqBlocks = 0;
  287. // if theres 5 or less rows, only 1 block is needed
  288. if (rows <= 5){
  289. reqBlocks = 1;
  290. }
  291.  
  292. // if theres more than 5 rows, each block can only store
  293. // the location of 4 blocks, and 1 pointer
  294. else {
  295. while (rows > 0) {
  296. // if 1 block is remaining, it does not need 1 more index block, it can fit in the last index block
  297. // there is only 4 blocks used in the last index block and we dont need a pointer for it
  298. if (rows != 1){
  299. rows -= 4;
  300. reqBlocks++;
  301. }
  302. else {
  303. break;
  304. }
  305. }
  306. }
  307. return reqBlocks;
  308. }
  309.  
  310. int *findFreeBlock(int reqBlocks, long decBitmap, char* method){
  311. // convert dec bitmap to bin bitmap
  312. long binBitmap = decimalToBinary(decBitmap);
  313. // how many bits the bitmap have
  314. int nDigits = 1;
  315. long countData = binBitmap;
  316. // divide by 10 until countData is left with a single digit
  317. while (countData > 9) {
  318. nDigits++;
  319. countData /= 10;
  320. }
  321. //printf("digits: %d\n", nDigits);
  322.  
  323. int freeBlocksCount = 0, counter = 0;
  324.  
  325. long remainder;
  326. // finding free blocks for contiguous
  327. if (strcmp(method, "contiguous") == 0){
  328. // create array of size 1 and set it to -1
  329. static int freeBlockFound[1] = {-1};
  330. while(binBitmap != 0) {
  331. remainder = binBitmap%10;
  332. if (remainder == 1){
  333. // free block found
  334. if (freeBlocksCount == 0){
  335. // new free block
  336. freeBlockFound[0] = counter+2;
  337. }
  338. freeBlocksCount +=1;
  339. if (freeBlocksCount == reqBlocks){
  340. return freeBlockFound;
  341. }
  342. }
  343. else {
  344. freeBlocksCount = 0;
  345. freeBlockFound[0] = -1;
  346. }
  347.  
  348. binBitmap /= 10;
  349. counter++;
  350. }
  351. // no space for conti allocation
  352. freeBlockFound[0] = -1;
  353. return freeBlockFound;
  354. }
  355.  
  356. // finding free blocks for linked
  357. else if (strcmp(method, "linked") == 0){
  358. // static int freeBlockFound[reqBlocks];
  359. // create array of size required blocks size and set it to -1
  360. int *freeBlockFound = malloc(reqBlocks * sizeof *freeBlockFound);
  361.  
  362. while(binBitmap != 0) {
  363. remainder = binBitmap%10;
  364. if (remainder == 1){
  365. // free block found
  366. freeBlocksCount++;
  367. freeBlockFound[freeBlocksCount-1] = counter + 2;
  368. if (freeBlocksCount == reqBlocks){
  369. return freeBlockFound;
  370. }
  371. }
  372.  
  373. binBitmap /= 10;
  374. counter++;
  375. }
  376. // no space for linked allocation
  377. freeBlockFound[0] = -1;
  378. return freeBlockFound;
  379. }
  380.  
  381. // finding free blocks for indexed
  382. else {
  383. // calculate number of index blocks required for index allocation
  384. int reqIndexBlocks = findReqBlocksWithPtr(reqBlocks);
  385.  
  386. // create array of and set it to -1. Every free data and index block found will be stored here
  387. int *freeBlockFound = malloc((reqBlocks + reqIndexBlocks) * sizeof *freeBlockFound);
  388. int enoughFreeIndex = 0;
  389. while(binBitmap != 0) {
  390. remainder = binBitmap%10;
  391. // 5 blocks for index, find free index blocks
  392. if (counter < 5){
  393. // dont need to check already once we know we have enough index blocks
  394. if (enoughFreeIndex == 0){
  395. printf("\nChecking indexed block");
  396. if (remainder == 1) {
  397. printf ("\nfree index block found at: %d",counter+2);
  398. // free index block found
  399. freeBlocksCount++;
  400. freeBlockFound[freeBlocksCount-1] = counter + 2;
  401. if (freeBlocksCount == reqIndexBlocks) {
  402. enoughFreeIndex = 1;
  403. printf("enough index");
  404. }
  405. }
  406. }
  407.  
  408. }
  409. // will run below code once done checking for free index blocks
  410. else if (enoughFreeIndex == 0) {
  411. // no space in index blocks
  412. freeBlockFound[0] = -1;
  413. printf("\nnot enough index");
  414. return freeBlockFound;
  415. }
  416. else {
  417. if (remainder == 1) {
  418. printf ("\nfree data block found at: %d",counter+2);
  419. freeBlocksCount++;
  420. freeBlockFound[freeBlocksCount-1] = counter + 2;
  421. if (freeBlocksCount == reqBlocks + reqIndexBlocks){
  422. return freeBlockFound;
  423. }
  424. }
  425. }
  426. binBitmap /= 10;
  427. counter++;
  428. }
  429. // no space for indexed allocation
  430. freeBlockFound[0] = -1;
  431. return freeBlockFound;
  432.  
  433.  
  434. }
  435. }
  436.  
  437. /* Function to convert a decinal number to binary number */
  438. long decimalToBinary(long dec) {
  439. long remainder;
  440. long binary = 0, i = 1;
  441.  
  442. while(dec != 0) {
  443. remainder = dec % 2;
  444. dec /= 2;
  445. binary += (remainder*i);
  446. i = i*10;
  447. }
  448. return binary;
  449. }
  450.  
  451. /* Function to convert a binary number to decimal number */
  452. long binaryToDecimal(long bin) {
  453. long remainder;
  454. long decimal = 0, i = 0;
  455. while(bin != 0) {
  456. remainder = bin % 10;
  457. bin /= 10;
  458. decimal += (remainder*power(2,i));
  459. ++i;
  460. }
  461. return decimal;
  462. }
  463.  
  464. int *getDirectoryData(int data, char* method){
  465.  
  466. // how many digits the data have
  467. int nDigits = 1, countData = data;
  468. while (countData > 9) {
  469. nDigits++;
  470. countData /= 10;
  471. }
  472. // printf("digits: %d\n", nDigits);
  473.  
  474. // extracting individual digits
  475. int digitsArray[nDigits];
  476. memset( digitsArray, 0,nDigits*sizeof(int) );
  477. int digitsArrayInd = nDigits - 1;
  478.  
  479. int remainder;
  480. while(data != 0)
  481. {
  482. remainder = data%10;
  483. digitsArray[digitsArrayInd--] = remainder;
  484. data /= 10;
  485. }
  486.  
  487. // converting individual digits into correct data
  488. static int dataArray[3] = {0};
  489. int dataArrayInd = nDigits -1;
  490.  
  491. if (strcmp(method, "indexed") == 0){
  492. // INDEXED
  493. // e.g. [1,2,3,4,5] = filename: 123, indexedLocation: 45
  494. dataArray[0] = 0;
  495. dataArray[1] = digitsArray[dataArrayInd--] + (digitsArray[dataArrayInd--]*10);
  496. for (int i = 0; i < nDigits-2; i++){
  497. if (i == 0){
  498. dataArray[0] += digitsArray[dataArrayInd--];
  499. }
  500. else {
  501. dataArray[0] += (digitsArray[dataArrayInd--] * powerExp(i));
  502. }
  503. }
  504. }
  505. else {
  506. // CONTIGUOUS/LINKED
  507. // e.g. [1,2,3,4,5,6,7] = filename: 123, start: 45, end: 67
  508. dataArray[0] = 0;
  509. dataArray[2] = digitsArray[dataArrayInd--] + (digitsArray[dataArrayInd--]*10);
  510. dataArray[1] = digitsArray[dataArrayInd--] + (digitsArray[dataArrayInd--]*10);
  511. for (int i = 0; i < nDigits-4; i++){
  512. if (i == 0){
  513. dataArray[0] += digitsArray[dataArrayInd--];
  514. }
  515. else {
  516. dataArray[0] += (digitsArray[dataArrayInd--] * powerExp(i));
  517. }
  518. }
  519. }
  520.  
  521.  
  522. return dataArray;
  523.  
  524. }
  525.  
  526. int *newMemory(){
  527.  
  528. int *mem = malloc(n * sizeof *mem);
  529.  
  530. // all allocation methods will have 1 block reserved for directory
  531. for (int i = 0; i < block; i++){
  532. // -1 meaning empty
  533. mem[i] = -1;
  534. }
  535.  
  536. // all allocation methods will have 1 block reserved for bitmap
  537. for (int i = block; i < 2*block; i++){
  538. // every entry is pointing to next entry (empty space)
  539. if (i == block){
  540. // only first cell of bitmap block will be used
  541. mem[i] = binaryToDecimal(11111111111);
  542. }
  543. else {
  544. // the rest set as -1
  545. mem[i] = -1;
  546. }
  547. }
  548.  
  549. // everything else will be empty data blocks
  550. for (int i = 2*block; i < n; i++){
  551. mem[i] = -1;
  552. }
  553.  
  554. return mem;
  555. }
  556.  
  557. void printDiskMap(char* method, int *memArray){
  558. printf("-Disk map for %s allocation method-\n", method);
  559.  
  560. int *dataArray;
  561.  
  562. //printing directory
  563. if (strcmp("indexed",method) == 0){
  564. for (int i = 0; i < block; i++){
  565. if (memArray[i] < 0){
  566. printf("%2d - B%-2d - %s \n",i,indexToBlock(i),"empty");
  567. }
  568. else {
  569. dataArray = getDirectoryData(memArray[i],method);
  570. printf("%2d - B%-2d - %d, %d \n",i,indexToBlock(i),dataArray[0],dataArray[1]);
  571. }
  572. }
  573. }
  574. else {
  575. for (int i = 0; i < block; i++){
  576. if (memArray[i] < 0){
  577. printf("%2d - B%-2d - %s \n",i,indexToBlock(i),"empty");
  578. }
  579. else {
  580. dataArray = getDirectoryData(memArray[i],method);
  581. printf("%2d - B%-2d - %d, %d, %d \n",i,indexToBlock(i),dataArray[0],dataArray[1],dataArray[2]);
  582. }
  583. }
  584. }
  585.  
  586.  
  587.  
  588. // printing bitmap block
  589. printf("%2d - B%-2d - %d\n",block,indexToBlock(block),memArray[block]);
  590. for (int i = block+1; i < 2*block; i++){
  591. printf("%2d - B%-2d - Unused\n",i,indexToBlock(i));
  592. }
  593.  
  594. // printing index and data
  595. for (int i = 2*block; i < n; i++){
  596. printf("%2d - B%-2d - %d\n",i,indexToBlock(i),memArray[i]);
  597. }
  598.  
  599. }
  600.  
  601. /*
  602. void readCommand(char* method, int datafile, int *memArray){
  603. int filename = (datafile*100)/100;
  604. if (strcmp("indexed",method) == 0){
  605. //something
  606. }
  607. else if (strcmp("indexed",method){
  608. //something
  609. }
  610. else {
  611. // something
  612. }
  613. }
  614. */
  615.  
  616. /*****************************************************************
  617. /* NOTES / TO-DO / QUESTIONS
  618. /*****************************************************************
  619.  
  620. [Directory data - Block 0]
  621. When empty: -1
  622. When not empty:
  623. Indexed: 543289
  624. (5432 - file name[1-4 digits], immediately followed by 89 - index location[MUST be 2 digits])
  625.  
  626. Linked & Contiguous: 54326789
  627. (5432 - file name[1-4 digits], immediately followed by 67 - start[Must be 2 digits], then 89 - end[Must be 2 digits])
  628.  
  629. [Bitmap - Block 1]
  630. 1 representing empty space, 0 representing used space
  631. Read backwards - LSB is block 2
  632.  
  633.  
  634. [Index]
  635. When empty: -1
  636. When not empty: pointer to data location
  637.  
  638. [File data]
  639. When empty: -1
  640. when not empty: a positive number
  641.  
  642.  
  643.  
  644.  
  645. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement