{"id":23491,"date":"2023-04-19T08:55:35","date_gmt":"2023-04-19T08:55:35","guid":{"rendered":"https:\/\/www.booksofall.com\/id\/?post_type=product&#038;p=23491"},"modified":"2023-04-19T08:55:35","modified_gmt":"2023-04-19T08:55:35","slug":"unix-lecture-notes-chapter-04","status":"publish","type":"product","link":"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/","title":{"rendered":"UNIX Lecture Notes &#8211; Chapter 04"},"content":{"rendered":"<h2>4.1 Overview<\/h2>\n<p>This chapter begins by examining how a program can exercise some control over the connections that it makes with disk \u001cfiles. It describes the data structures used by the kernel to manage open \u001cfiles, and explains how processes can interact with these fi\u001cles. It then explores device fi\u001cles and how they are used and structured, and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Device_driver\">device drivers<\/a> and their roles and structures. From there it looks at block and character device fi\u001cle differences, after which it turns to terminals. The remainder of the chapter is then devoted to controlling terminals.<\/p>\n<h2>4.2 Open Files<\/h2>\n<p>The programs that we have studied so far operate on disk files, which are fi\u001cles that reside, of course, on disks. You are by now well-acquainted with the general model of fi\u001cle I\/O: open the fi\u001cle, access it, and close it. Remember that this model works for any kind of \u001cfile, not just disk fi\u001cles. Here though, we are interested only in disk fi\u001cles.<\/p>\n<p>In this <a href=\"https:\/\/www.easefilter.com\/Forums_Files\/File_IO.htm\">model of fi\u001cle I\/O<\/a>, opening a fi\u001cle returns a reference to an object that can be used to access the fi\u001cle, either for reading or writing. When you use the open() system call, you get a \u001cle descriptor in return; when you use the fopen() C standard I\/O library call, you get a FILE pointer in return. Either way, you are getting a scalar object (i.e., a small integer or a pointer) that is associated with a hidden (kernel) data structure that allows you to access the \u001cfile. Here, we will explore how much control we can exercise over the way in which the program is connected to the fi\u001cle.<\/p>\n<p>When a process opens a disk \u001cfile using the open() system call, the kernel creates a data structure that represents the connection between the process and the \u001cfile. The returned \u001cfile descriptor can be used by the process to access the fi\u001cle. A fi\u001cle descriptor is simply an index into a per-process table that the kernel uses to locate that specific data structure. Different versions of UNIX call this data structure different things, and the data structure may have slightly di\u001bfferent members from one UNIX variant to another, but the basic members and purpose of the structure are invariant: its most important member is the fi\u001cle pointer, and its purpose is to store the position in the fi\u001cle from which the next operation will take place (whether it is a read or a write.)<\/p>\n<p id=\"vGRnpbL\"><img loading=\"lazy\" decoding=\"async\" width=\"345\" height=\"310\" class=\"alignnone size-full wp-image-23495 \" src=\"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2023\/04\/img_643fa8e5e87aa.png\" alt=\"\" \/><\/p>\n<p>This data structure is called a \u001cfile entry in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Berkeley_Software_Distribution\">BSD<\/a> UNIX and a fi\u001cle structure in Linux. I will call it a fi\u001cle structure in these notes. The connection between a process and a \u001cfile is completely characterized by the information contained in the fi\u001cle structure. Aside from the \u001cfile pointer, the kinds of information it typically contains include:<\/p>\n<ul>\n<li>whether or not the fi\u001cle is open for reading, writing, reading and writing, or appending,<\/li>\n<li>whether or not the I\/O is buffered or unbuffered, and<\/li>\n<li>whether or not the access is exclusive or whether other processes also access the \u001cle.<\/li>\n<\/ul>\n<p>as well as other information that is required by the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Kernel_(operating_system)\">kernel<\/a>. The information contained in the \u001cle structure characterizes the connection between the open fi\u001cle and the process; it is specific to this single connection. Other processes might have this \u001cle open with different attributes. Many of the attributes of the connection can be changed by the process; others cannot. Which can and which cannot, and how is it done? These are the questions we will answer.<\/p>\n<p>Let us begin by examining all of the data structures related to open \u001cles. Each process has a table that is usually called the open \u001cfile table. ( In 4.4BSD, this table was called the descriptor table, and each entry was called a \u001cfiledesc structure.) In Linux, this table is the fd array, which is part of a larger structure called the \u001cfiles_struct. The fi\u001cle descriptor returned by the open() system call is actually an index into the open fi\u001cle table of the process making the call. Recall that every process is given three \u001cfile descriptors when it is created: 0, 1, and 2, respectively, for standard input, output, and error. These are the fi\u001crst three indices in this table, as shown in Figure 4.1.<\/p>\n<p>When a process issues the open() system call, the kernel creates a new fi\u001cle structure and \u001cfills the lowest-numbered available slot in the process&#8217;s open fi\u001cle table with a pointer to that \u001cle structure. The \u001cfile structures for all open \u001cfiles are contained in a table called the \u001cfile structure table. which resides in the address space of the kernel. See Figure 4.2.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><iframe style=\"width: 100%; height: 750px; border: none;\" src=\"https:\/\/online.visual-paradigm.com\/share\/book\/unix-lecture-notes-chapter-04-1bs0xff6nb?p=1\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n","protected":false},"featured_media":23505,"template":"","meta":{"_yoast_wpseo_title":"","_yoast_wpseo_metadesc":"Learn more about the file control, data structures, device files, drivers, block\/character devices, and terminals with this note now!"},"product_brand":[],"product_cat":[376],"product_tag":[],"class_list":{"0":"post-23491","1":"product","2":"type-product","3":"status-publish","4":"has-post-thumbnail","6":"product_cat-unix-lecture-notes","8":"first","9":"instock","10":"shipping-taxable","11":"product-type-simple"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>UNIX Lecture Notes - Chapter 04 - BooksOfAll Indonesian<\/title>\n<meta name=\"description\" content=\"Learn more about the file control, data structures, device files, drivers, block\/character devices, and terminals with this note now!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/\" \/>\n<meta property=\"og:locale\" content=\"id_ID\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"UNIX Lecture Notes - Chapter 04 - BooksOfAll Indonesian\" \/>\n<meta property=\"og:description\" content=\"Learn more about the file control, data structures, device files, drivers, block\/character devices, and terminals with this note now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/\" \/>\n<meta property=\"og:site_name\" content=\"BooksOfAll Indonesian\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2023\/04\/UNIX-Lecture-Notes-cover-UNIX-Lecture-Notes-Chapter_04.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2023\/04\/UNIX-Lecture-Notes-cover-UNIX-Lecture-Notes-Chapter_04.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Estimasi waktu membaca\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 menit\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/\",\"url\":\"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/\",\"name\":\"UNIX Lecture Notes - Chapter 04 - BooksOfAll Indonesian\",\"isPartOf\":{\"@id\":\"https:\/\/www.booksofall.com\/id\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2023\/04\/UNIX-Lecture-Notes-cover-UNIX-Lecture-Notes-Chapter_04.jpg\",\"datePublished\":\"2023-04-19T08:55:35+00:00\",\"description\":\"Learn more about the file control, data structures, device files, drivers, block\/character devices, and terminals with this note now!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/#breadcrumb\"},\"inLanguage\":\"id\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"id\",\"@id\":\"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/#primaryimage\",\"url\":\"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2023\/04\/UNIX-Lecture-Notes-cover-UNIX-Lecture-Notes-Chapter_04.jpg\",\"contentUrl\":\"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2023\/04\/UNIX-Lecture-Notes-cover-UNIX-Lecture-Notes-Chapter_04.jpg\",\"width\":\"827\",\"height\":\"1169\",\"caption\":\"UNIX Lecture Notes - Chapter 04\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.booksofall.com\/id\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Categories\",\"item\":\"https:\/\/www.booksofall.com\/id\/categories\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"UNIX Lecture Notes &#8211; Chapter 04\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.booksofall.com\/id\/#website\",\"url\":\"https:\/\/www.booksofall.com\/id\/\",\"name\":\"BooksOfAll Indonesian\",\"description\":\"Biggest IT eBooks library and learning resources - Free eBooks for programming, computing, artificial intelligence and more.\",\"publisher\":{\"@id\":\"https:\/\/www.booksofall.com\/id\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.booksofall.com\/id\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"id\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.booksofall.com\/id\/#organization\",\"name\":\"BooksOfAll Indonesian\",\"url\":\"https:\/\/www.booksofall.com\/id\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"id\",\"@id\":\"https:\/\/www.booksofall.com\/id\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2022\/06\/booksofall-logo-2.png\",\"contentUrl\":\"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2022\/06\/booksofall-logo-2.png\",\"width\":166,\"height\":30,\"caption\":\"BooksOfAll Indonesian\"},\"image\":{\"@id\":\"https:\/\/www.booksofall.com\/id\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"UNIX Lecture Notes - Chapter 04 - BooksOfAll Indonesian","description":"Learn more about the file control, data structures, device files, drivers, block\/character devices, and terminals with this note now!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/","og_locale":"id_ID","og_type":"article","og_title":"UNIX Lecture Notes - Chapter 04 - BooksOfAll Indonesian","og_description":"Learn more about the file control, data structures, device files, drivers, block\/character devices, and terminals with this note now!","og_url":"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/","og_site_name":"BooksOfAll Indonesian","og_image":[{"url":"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2023\/04\/UNIX-Lecture-Notes-cover-UNIX-Lecture-Notes-Chapter_04.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_image":"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2023\/04\/UNIX-Lecture-Notes-cover-UNIX-Lecture-Notes-Chapter_04.jpg","twitter_misc":{"Estimasi waktu membaca":"4 menit"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/","url":"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/","name":"UNIX Lecture Notes - Chapter 04 - BooksOfAll Indonesian","isPartOf":{"@id":"https:\/\/www.booksofall.com\/id\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/#primaryimage"},"image":{"@id":"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/#primaryimage"},"thumbnailUrl":"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2023\/04\/UNIX-Lecture-Notes-cover-UNIX-Lecture-Notes-Chapter_04.jpg","datePublished":"2023-04-19T08:55:35+00:00","description":"Learn more about the file control, data structures, device files, drivers, block\/character devices, and terminals with this note now!","breadcrumb":{"@id":"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/#breadcrumb"},"inLanguage":"id","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/"]}]},{"@type":"ImageObject","inLanguage":"id","@id":"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/#primaryimage","url":"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2023\/04\/UNIX-Lecture-Notes-cover-UNIX-Lecture-Notes-Chapter_04.jpg","contentUrl":"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2023\/04\/UNIX-Lecture-Notes-cover-UNIX-Lecture-Notes-Chapter_04.jpg","width":"827","height":"1169","caption":"UNIX Lecture Notes - Chapter 04"},{"@type":"BreadcrumbList","@id":"https:\/\/www.booksofall.com\/id\/unix-lecture-notes-chapter-04\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.booksofall.com\/id\/"},{"@type":"ListItem","position":2,"name":"Categories","item":"https:\/\/www.booksofall.com\/id\/categories\/"},{"@type":"ListItem","position":3,"name":"UNIX Lecture Notes &#8211; Chapter 04"}]},{"@type":"WebSite","@id":"https:\/\/www.booksofall.com\/id\/#website","url":"https:\/\/www.booksofall.com\/id\/","name":"BooksOfAll Indonesian","description":"Biggest IT eBooks library and learning resources - Free eBooks for programming, computing, artificial intelligence and more.","publisher":{"@id":"https:\/\/www.booksofall.com\/id\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.booksofall.com\/id\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"id"},{"@type":"Organization","@id":"https:\/\/www.booksofall.com\/id\/#organization","name":"BooksOfAll Indonesian","url":"https:\/\/www.booksofall.com\/id\/","logo":{"@type":"ImageObject","inLanguage":"id","@id":"https:\/\/www.booksofall.com\/id\/#\/schema\/logo\/image\/","url":"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2022\/06\/booksofall-logo-2.png","contentUrl":"https:\/\/www.booksofall.com\/id\/wp-content\/uploads\/sites\/12\/2022\/06\/booksofall-logo-2.png","width":166,"height":30,"caption":"BooksOfAll Indonesian"},"image":{"@id":"https:\/\/www.booksofall.com\/id\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.booksofall.com\/id\/wp-json\/wp\/v2\/product\/23491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.booksofall.com\/id\/wp-json\/wp\/v2\/product"}],"about":[{"href":"https:\/\/www.booksofall.com\/id\/wp-json\/wp\/v2\/types\/product"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.booksofall.com\/id\/wp-json\/wp\/v2\/media\/23505"}],"wp:attachment":[{"href":"https:\/\/www.booksofall.com\/id\/wp-json\/wp\/v2\/media?parent=23491"}],"wp:term":[{"taxonomy":"product_brand","embeddable":true,"href":"https:\/\/www.booksofall.com\/id\/wp-json\/wp\/v2\/product_brand?post=23491"},{"taxonomy":"product_cat","embeddable":true,"href":"https:\/\/www.booksofall.com\/id\/wp-json\/wp\/v2\/product_cat?post=23491"},{"taxonomy":"product_tag","embeddable":true,"href":"https:\/\/www.booksofall.com\/id\/wp-json\/wp\/v2\/product_tag?post=23491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}