blog/assets/js/f60c832f.186a1d8e.js
github-actions[bot] 70bdf5ed11 deploy: d91860e0f7
2023-09-07 18:30:53 +00:00

1 line
No EOL
16 KiB
JavaScript

"use strict";(self.webpackChunkfi=self.webpackChunkfi||[]).push([[9611],{3905:(e,t,a)=>{a.d(t,{Zo:()=>p,kt:()=>h});var n=a(7294);function r(e,t,a){return t in e?Object.defineProperty(e,t,{value:a,enumerable:!0,configurable:!0,writable:!0}):e[t]=a,e}function i(e,t){var a=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),a.push.apply(a,n)}return a}function o(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?i(Object(a),!0).forEach((function(t){r(e,t,a[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):i(Object(a)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))}))}return e}function m(e,t){if(null==e)return{};var a,n,r=function(e,t){if(null==e)return{};var a,n,r={},i=Object.keys(e);for(n=0;n<i.length;n++)a=i[n],t.indexOf(a)>=0||(r[a]=e[a]);return r}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(n=0;n<i.length;n++)a=i[n],t.indexOf(a)>=0||Object.prototype.propertyIsEnumerable.call(e,a)&&(r[a]=e[a])}return r}var s=n.createContext({}),l=function(e){var t=n.useContext(s),a=t;return e&&(a="function"==typeof e?e(t):o(o({},t),e)),a},p=function(e){var t=l(e.components);return n.createElement(s.Provider,{value:t},e.children)},u="mdxType",c={inlineCode:"code",wrapper:function(e){var t=e.children;return n.createElement(n.Fragment,{},t)}},d=n.forwardRef((function(e,t){var a=e.components,r=e.mdxType,i=e.originalType,s=e.parentName,p=m(e,["components","mdxType","originalType","parentName"]),u=l(a),d=r,h=u["".concat(s,".").concat(d)]||u[d]||c[d]||i;return a?n.createElement(h,o(o({ref:t},p),{},{components:a})):n.createElement(h,o({ref:t},p))}));function h(e,t){var a=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var i=a.length,o=new Array(i);o[0]=d;var m={};for(var s in t)hasOwnProperty.call(t,s)&&(m[s]=t[s]);m.originalType=e,m[u]="string"==typeof e?e:r,o[1]=m;for(var l=2;l<i;l++)o[l]=a[l];return n.createElement.apply(null,o)}return n.createElement.apply(null,a)}d.displayName="MDXCreateElement"},2490:(e,t,a)=>{a.r(t),a.d(t,{assets:()=>s,contentTitle:()=>o,default:()=>c,frontMatter:()=>i,metadata:()=>m,toc:()=>l});var n=a(7462),r=(a(7294),a(3905));const i={slug:"garbage_collect",title:"Practice exam B",description:"Garbage everywhere\u2026\n",last_update:{date:new Date("2023-05-08T00:00:00.000Z")}},o="Garbage Collection",m={unversionedId:"pexam/b-garbage_collect",id:"pexam/b-garbage_collect",title:"Practice exam B",description:"Garbage everywhere\u2026\n",source:"@site/pb071/pexam/b-garbage_collect.md",sourceDirName:"pexam",slug:"/pexam/garbage_collect",permalink:"/pb071/pexam/garbage_collect",draft:!1,editUrl:"https://github.com/mfocko/blog/tree/main/pb071/pexam/b-garbage_collect.md",tags:[],version:"current",lastUpdatedAt:1683504e3,formattedLastUpdatedAt:"May 8, 2023",frontMatter:{slug:"garbage_collect",title:"Practice exam B",description:"Garbage everywhere\u2026\n",last_update:{date:"2023-05-08T00:00:00.000Z"}},sidebar:"autogeneratedBar",previous:{title:"Practice Exams",permalink:"/pb071/category/practice-exams"},next:{title:"Practice exam C",permalink:"/pb071/pexam/cams"}},s={},l=[{value:"Format of the shell history",id:"format-of-the-shell-history",level:2},{value:"Format of the output",id:"format-of-the-output",level:2},{value:"<code>-gt \u2039min_size\u203a</code>",id:"-gt-min_size",level:3},{value:"<code>-f \u2039total_size\u203a \u2039min_unused\u203a</code>",id:"-f-total_size-min_unused",level:3},{value:"Example usage",id:"example-usage",level:2},{value:"Requirements and notes",id:"requirements-and-notes",level:2}],p={toc:l},u="wrapper";function c(e){let{components:t,...a}=e;return(0,r.kt)(u,(0,n.Z)({},p,a,{components:t,mdxType:"MDXLayout"}),(0,r.kt)("h1",{id:"garbage-collection"},"Garbage Collection"),(0,r.kt)("admonition",{title:"Exam environment",type:"caution"},(0,r.kt)("ul",{parentName:"admonition"},(0,r.kt)("li",{parentName:"ul"},"During the exam you will be provided with a barebone ",(0,r.kt)("em",{parentName:"li"},"exam session")," on the\n",(0,r.kt)("em",{parentName:"li"},"faculty computers"),"."),(0,r.kt)("li",{parentName:"ul"},"In browser you are only allowed to have the following tabs open:",(0,r.kt)("ul",{parentName:"li"},(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("a",{parentName:"li",href:"https://en.cppreference.com"},"C documentation")),(0,r.kt)("li",{parentName:"ul"},"page containing the assignment"))),(0,r.kt)("li",{parentName:"ul"},"You ",(0,r.kt)("strong",{parentName:"li"},"are not")," allowed to use your own source code, e.g. prepared beforehand\nor from the seminars."),(0,r.kt)("li",{parentName:"ul"},"You have ",(0,r.kt)("strong",{parentName:"li"},"5 minutes")," to read through the assignment and ask any follow-up\nquestions should be there something unclear."),(0,r.kt)("li",{parentName:"ul"},"You have ",(0,r.kt)("strong",{parentName:"li"},"60 minutes")," to work on the assignment, afterward your work will be\ndiscussed with your seminar tutor."))),(0,r.kt)("p",null,"You have gotten into a trouble during your regular upgrade of your archLinux",(0,r.kt)("sup",{parentName:"p",id:"fnref-1"},(0,r.kt)("a",{parentName:"sup",href:"#fn-1",className:"footnote-ref"},"1")),"\ninstallation\u2026 You've been carelessly running the upgrades for months and forgot\nabout clearing up the caches."),(0,r.kt)("p",null,"Your task is to write a program ",(0,r.kt)("inlineCode",{parentName:"p"},"garbage_collect")," that will evaluate the shell\nhistory provided as a file and will try to find files or directories that are\nsuspiciously big and decide which of them should be deleted to free some space."),(0,r.kt)("h2",{id:"format-of-the-shell-history"},"Format of the shell history"),(0,r.kt)("p",null,"You are provided one file consisting of the captured buffer of the terminal. You\ncan see only two commands being used:"),(0,r.kt)("ol",null,(0,r.kt)("li",{parentName:"ol"},(0,r.kt)("p",{parentName:"li"},(0,r.kt)("inlineCode",{parentName:"p"},"cd \u2039somewhere\u203a")," that changes the current working directory."),(0,r.kt)("p",{parentName:"li"},"At the beginning you start in the root of the filesystem (i.e. ",(0,r.kt)("inlineCode",{parentName:"p"},"/"),")."),(0,r.kt)("p",{parentName:"li"},"You are ",(0,r.kt)("strong",{parentName:"p"},"guaranteed")," that ",(0,r.kt)("inlineCode",{parentName:"p"},"\u2039somewhere\u203a")," is:"),(0,r.kt)("ul",{parentName:"li"},(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},".")," that is a current working directory (i.e. does nothing),"),(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},"..")," that moves you up one level (in case you are in ",(0,r.kt)("inlineCode",{parentName:"li"},"/"),", does nothing), or"),(0,r.kt)("li",{parentName:"ul"},"is a valid directory in the current working directory.")),(0,r.kt)("admonition",{parentName:"li",type:"caution"},(0,r.kt)("p",{parentName:"admonition"},"There are no guarantees or restrictions on the names of the files or\ndirectories!"))),(0,r.kt)("li",{parentName:"ol"},(0,r.kt)("p",{parentName:"li"},(0,r.kt)("inlineCode",{parentName:"p"},"ls")," that will list files in the current working directory and their\nrespective sizes. If there is a directory in the current working it has ",(0,r.kt)("inlineCode",{parentName:"p"},"dir"),"\ninstead of the size."))),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre"},"$ ls\ndir a\n14848514 b.txt\n8504156 c.dat\ndir d\n$ cd a\n$ cd .\n$ cd .\n$ cd .\n$ ls\ndir e\n29116 f\n2557 g\n62596 h.lst\n$ cd e\n$ ls\n584 i\n$ cd ..\n$ cd ..\n$ cd d\n$ ls\n4060174 j\n8033020 d.log\n5626152 d.ext\n7214296 k\n")),(0,r.kt)("p",null,"For this input, you will get following file system:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre"},"- / (dir, size=48381165)\n - a (dir, size=94853)\n - e (dir, size=584)\n - i (file, size=584)\n - f (file, size=29116)\n - g (file, size=2557)\n - h.lst (file, size=62596)\n - b.txt (file, size=14848514)\n - c.dat (file, size=8504156)\n - d (dir, size=24933642)\n - j (file, size=4060174)\n - d.log (file, size=8033020)\n - d.ext (file, size=5626152)\n - k (file, size=7214296)\n")),(0,r.kt)("h2",{id:"format-of-the-output"},"Format of the output"),(0,r.kt)("p",null,"Your program should support 2 switches:"),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},"-gt \u2039min_size\u203a")," that will print out suspiciously big files."),(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},"-f \u2039total_size\u203a \u2039min_unused\u203a")," that will print out a file to be deleted.")),(0,r.kt)("h3",{id:"-gt-min_size"},(0,r.kt)("inlineCode",{parentName:"h3"},"-gt \u2039min_size\u203a")),(0,r.kt)("p",null,"With this switch you are provided one additional argument:"),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},"min_size")," that is the lower bound (inclusive) for size of any file or\ndirectory that is supposed to be listed.")),(0,r.kt)("p",null,"When your program is being run with this switch, it is is supposed to print out\nall files ",(0,r.kt)("strong",{parentName:"p"},"and")," directories that are bigger than the provided ",(0,r.kt)("inlineCode",{parentName:"p"},"min_size"),"."),(0,r.kt)("h3",{id:"-f-total_size-min_unused"},(0,r.kt)("inlineCode",{parentName:"h3"},"-f \u2039total_size\u203a \u2039min_unused\u203a")),(0,r.kt)("p",null,"With this switch you are provided two additional arguments:"),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},"total_size")," that is a total size of the filesystem",(0,r.kt)("sup",{parentName:"li",id:"fnref-2"},(0,r.kt)("a",{parentName:"sup",href:"#fn-2",className:"footnote-ref"},"2")),"."),(0,r.kt)("li",{parentName:"ul"},(0,r.kt)("inlineCode",{parentName:"li"},"min_unused")," that is a minimum of free space required for an upgrade.")),(0,r.kt)("p",null,"Your program should find ",(0,r.kt)("strong",{parentName:"p"},"exactly one")," file or a directory that is of the\nsmallest size, but big enough to free enough space for the upgrade to proceed."),(0,r.kt)("p",null,"In other words, if that file or directory is deleted, following should hold:"),(0,r.kt)("div",{className:"math math-display"},(0,r.kt)("span",{parentName:"div",className:"katex-display"},(0,r.kt)("span",{parentName:"span",className:"katex"},(0,r.kt)("span",{parentName:"span",className:"katex-mathml"},(0,r.kt)("math",{parentName:"span",xmlns:"http://www.w3.org/1998/Math/MathML",display:"block"},(0,r.kt)("semantics",{parentName:"math"},(0,r.kt)("mrow",{parentName:"semantics"},(0,r.kt)("mrow",{parentName:"mrow"},(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"t"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"o"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"t"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"a"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"l"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"_"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"s"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"i"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"z"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"e")),(0,r.kt)("mo",{parentName:"mrow"},"\u2212"),(0,r.kt)("mrow",{parentName:"mrow"},(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"u"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"s"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"e"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"d")),(0,r.kt)("mo",{parentName:"mrow"},"\u2265"),(0,r.kt)("mrow",{parentName:"mrow"},(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"m"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"i"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"n"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"_"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"u"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"n"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"u"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"s"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"e"),(0,r.kt)("mi",{parentName:"mrow",mathvariant:"monospace"},"d"))),(0,r.kt)("annotation",{parentName:"semantics",encoding:"application/x-tex"},"\\mathtt{total\\_size} - \\mathtt{used} \\geq \\mathtt{min\\_unused}")))),(0,r.kt)("span",{parentName:"span",className:"katex-html","aria-hidden":"true"},(0,r.kt)("span",{parentName:"span",className:"base"},(0,r.kt)("span",{parentName:"span",className:"strut",style:{height:"0.7063em",verticalAlign:"-0.0951em"}}),(0,r.kt)("span",{parentName:"span",className:"mord"},(0,r.kt)("span",{parentName:"span",className:"mord mathtt"},"total_size")),(0,r.kt)("span",{parentName:"span",className:"mspace",style:{marginRight:"0.2222em"}}),(0,r.kt)("span",{parentName:"span",className:"mbin"},"\u2212"),(0,r.kt)("span",{parentName:"span",className:"mspace",style:{marginRight:"0.2222em"}})),(0,r.kt)("span",{parentName:"span",className:"base"},(0,r.kt)("span",{parentName:"span",className:"strut",style:{height:"0.7719em",verticalAlign:"-0.136em"}}),(0,r.kt)("span",{parentName:"span",className:"mord"},(0,r.kt)("span",{parentName:"span",className:"mord mathtt"},"used")),(0,r.kt)("span",{parentName:"span",className:"mspace",style:{marginRight:"0.2778em"}}),(0,r.kt)("span",{parentName:"span",className:"mrel"},"\u2265"),(0,r.kt)("span",{parentName:"span",className:"mspace",style:{marginRight:"0.2778em"}})),(0,r.kt)("span",{parentName:"span",className:"base"},(0,r.kt)("span",{parentName:"span",className:"strut",style:{height:"0.7063em",verticalAlign:"-0.0951em"}}),(0,r.kt)("span",{parentName:"span",className:"mord"},(0,r.kt)("span",{parentName:"span",className:"mord mathtt"},"min_unused"))))))),(0,r.kt)("h2",{id:"example-usage"},"Example usage"),(0,r.kt)("p",null,"You can have a look at the example usage of your program. We can run your\nprogram from the shell like"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre"},"$ ./garbage_collect shell_history.txt -gt 10000000\n24933642 /d\n14848514 /b.txt\n48381165 /\n\n$ ./garbage_collect shell_history.txt -f 70000000 30000000\n24933642 /d\n")),(0,r.kt)("h2",{id:"requirements-and-notes"},"Requirements and notes"),(0,r.kt)("ul",null,(0,r.kt)("li",{parentName:"ul"},"Define ",(0,r.kt)("strong",{parentName:"li"},"structures")," (and ",(0,r.kt)("strong",{parentName:"li"},"enumerations"),", if applicable) for the parsed\ninformation from the files."),(0,r.kt)("li",{parentName:"ul"},"For keeping the \u201crecords\u201d, use some ",(0,r.kt)("strong",{parentName:"li"},"dynamic")," data structure.",(0,r.kt)("ul",{parentName:"li"},(0,r.kt)("li",{parentName:"ul"},"Don't forget to consider pros and cons of using ",(0,r.kt)("em",{parentName:"li"},"specific")," data structures\nbefore going through implementing."))),(0,r.kt)("li",{parentName:"ul"},"You ",(0,r.kt)("strong",{parentName:"li"},"are not required")," to produce 1:1 output to the provided examples, they\nare just a hint to not waste your time tinkering with a user experience."),(0,r.kt)("li",{parentName:"ul"},"If any of the operations on the input files should fail,\n",(0,r.kt)("strong",{parentName:"li"},"you are expected to")," handle the situation ",(0,r.kt)("em",{parentName:"li"},"accordingly"),"."),(0,r.kt)("li",{parentName:"ul"},"Failures of any other common functions (e.g. functions used for memory\nmanagement) should be handled in ",(0,r.kt)("strong",{parentName:"li"},"the same way")," as they were in the\nhomeworks and seminars."),(0,r.kt)("li",{parentName:"ul"},"Your program ",(0,r.kt)("strong",{parentName:"li"},"must free")," all the resources before exiting.")),(0,r.kt)("div",{className:"footnotes"},(0,r.kt)("hr",{parentName:"div"}),(0,r.kt)("ol",{parentName:"div"},(0,r.kt)("li",{parentName:"ol",id:"fn-1"},"Also applies to Fedora, but\u2026 we use arch btw \ud83d\ude09",(0,r.kt)("a",{parentName:"li",href:"#fnref-1",className:"footnote-backref"},"\u21a9")),(0,r.kt)("li",{parentName:"ol",id:"fn-2"},"duh!",(0,r.kt)("a",{parentName:"li",href:"#fnref-2",className:"footnote-backref"},"\u21a9")))))}c.isMDXComponent=!0}}]);