Towards Better Systems Programming in Ocaml with Out-Of-Heap Allocation

Total Page:16

File Type:pdf, Size:1020Kb

Towards Better Systems Programming in Ocaml with Out-Of-Heap Allocation Towards better systems programming in OCaml with out-of-heap allocation Guillaume Munch-Maccagnoni∗ Inria, LS2N CNRS 30th May 2020 Abstract. The current multicore OCaml implementation bans • In Rust, value interoperability with C++ is instrumental in so-called “naked pointers”, pointers to outside the OCaml heap the migration from one language to the other, since programs unless they follow drastic restrictions. A backwards-incompatible that are migrating can remain a long time in a hybrid state. change has been proposed to make way for the new multicore GC • “Swift is a high-performance system programming lan- in OCaml. I argue that out-of-heap pointers are not an anomaly, guage. It has a clean and modern syntax, offers seamless but are part of a better systems programming future. access to existing C and Objective-C code and frameworks, and is memory safe by default.” 1 1. Introduction There is ongoing research to augment OCaml with new kinds of types and values which would allow operating more safely Reserving address space to efficiently recognise in-heap point- with foreign pointers (among others): Radanne, Saffrich, and ers (and do even wilder tricks) is standard in garbage collected Thiemann(2019), my own (2018). languages and allocators, as well as in research about memory Proposed replacements in OCaml’s no-naked-pointers mode management. In addition, allowing foreign pointers for interop- include introducing an indirection via a special block, or dis- erability is a standard feature in common systems programming guising the pointer into an integer (using the lsb as a tag), i.e. languages. as unstructured data. One will be unable, for instance, to create As part of my research to make OCaml a better systems pro- an OCaml-like value on the Rust stack or heap and pass it as an gramming language, I propose to revisit the importance of naked argument to an OCaml function. pointers for 1) interoperability, 2) backwards-compatiblity, and 3) performance, three staples of systems programming. I conclude Thus, among the systems programming languages I looked with a challenge addressed to the ML community. at, none requires such wrapping to access foreign objects. In In the appendix, I show a simple design based on virtual ad- OCaml as well, the discussion at ocaml/ocaml#9534, and the dress space reservation that is proposed to make naked pointers case of LLVM bindings that has been reported, show preliminary compatible with the multicore OCaml GC. empirical evidence of naked foreign pointers in OCaml in the wild. In addition, a third technique is sometimes mentioned regard- 2. Gathered evidence from the practice and ing the no-naked-pointers mode, which is to prefix the out-of- the literature heap values with a “black” header (meaning it is already marked). This technique is unorthodox because it consists in checking 2.1. Interoperability whether one owns a pointer by dereferencing it. One conse- quence is that such values can never be deallocated, because it is In his essay Some were meant for C, Stephen Kell (2017) places never possible to reason about whether they are still reachable the essential value of systems programming languages in “com- from the GC. For this reason, while this can be appropriate for municativity” rather than performance. Among others, he argues runtime-owned values that live for the duration of the program, against “managed” languages in which “user code may deal its interest for users is more marginal. Nevertheless, despite its only with memory that the language implementation can account unsuitability for dynamic allocation, it has been advocated by for”. the designer of no-naked-pointers notably because “for some Concerning foreign pointers specifically, their allowance is applications, the overhead of having an associated heap block indeed standard in common (self-described) systems program- will be too high”2. ming languages beyond C and C++: Rust, Go, and Swift. For Lastly, at ocaml/ocaml#9534 and ocaml/ocaml#9535, without instance: questioning the no-naked-pointers approach, library developers • Rust’s ownership-and-borrowing allows it to interoperate reported the convenience of allowing a special NULL pointer value safely with garbage collected runtimes, and directly manip- for bindings (and other purposes). ulate values from the foreign GCed heap, for instance with SpiderMonkey’s GC for JavaScript (Jeffrey, 2018). 1https://github.com/apple/swift ∗[email protected], Nantes, France 2https://github.com/ocaml/ocaml/pull/9610#discussion_r431007766 1 2.2. Address space reservation In addition to explicitly breaking existing code, as we have seen in the case of foreign pointers, none of the propositions is ideal: The technique we propose relies on reservation of large areas in in theory both have a linear overhead when traversing a structure the virtual address space. compared to the same traversal using foreign pointers for instance, Reserving virtual address space is standard in several common in which case the code adaptation that is required could be non- garbage-collected languages: Java, GHC, and Go. GHC reserves systematic. Yet, no empirical evaluation of the impact of the 3 1 TB by default . The new multi-threaded runtime for Julia proposed changes has been proposed so far—a (non-precise) tool 4 reserves 100 GB for thread stacks . Go used to reserve 512 GB to detect naked pointers has recently appeared7 and will be a up-front at some point, and now reserves more progressively. The good tool to perform such an evaluation. concurrent OCaml multicore GC reserves 4 GB for the minor In addition, the first solution incurs an allocation, whereas the heap. second solution only requires boilerplate, but confuses tools like Go further uses virtual addressing tricks to support interior debuggers, static analysers and such, in ways that are unlikely 5 pointers: pointers to the inside of a block . The bitpattern is used to be fixable over time. Moreover, they do not bring additional to encode the size of the block and thus where it starts. safety in terms of resource-management: what they do is to deal Making use of large areas of reserved address space is a com- in a rather radical manner with the problem of growing the heap mon technique used in research about memory allocators. For while remembering who was outside, which is mostly required by instance, Lvin, Novark, Berger, and Zorn(2008) reads: “On the reliance on the system malloc by the GC to request chunks of modern architectures, especially 64-bit systems, virtual address memory. This issue too can also be solved by virtual addressing space is a plentiful resource.” In Powers, Tench, Berger, and techniques.8 McGregor(2019), a large virtual address space consumption is The latter issue indeed happens in practice; for instance one traded for the ability to merge physical pages, in order to perform industrial user explains that they solve it by forcing a GC to re- compaction without relocation. move out-of-heap pointers after a large out-of-heap structure has been deallocated, when they use the LLVM bindings. However they write: “Short of rewriting the llvm bindings, I am not aware 2.3. Backwards compatibility of a better / more future-proof implementation strategy. Even ignoring any performance concerns about allocating a block For simplicity or conclusion in the lack of an efficient solution, for every one of the very many pointers passed from libllvm to the multicore OCaml GC does not support OCaml’s naked point- OCaml.” ers. Replacing the page table by a technique based on virtual In that case, the technique we propose would avoid a . address space reservation could offer the possibility to avoid rewrite, but also fix the bug code breakage while preserving efficiency, as suggested in an Lastly, another well-known use of out-of-heap pointers, which experiment from 2013 by Jacques-Henri Jourdan6. is known to break with the no-naked-pointers mode, is sym- 9 In the recent years, the perils of breaking backwards compatib- bolised by the Ancient library and the Netmulticore library, lity in a programming language has been demonstrated by Python which propose to allocate OCaml values outside of the garbage- 3, which introduced breaking changes that were expected to be collected heap to offer heaps that can be shared or large (such as minor, including a change to the semantics of strings whose con- larger than available RAM). Neither an indirection nor tagged pointers can be used to replace this use-case, a fact that has been sequences were miscalculated. While little documentation about 10 the role of backwards-compatibility in programming language known from the start . This is the subject of next section. evolution in general is found in the literature, Malloy and Power (2019) investigate the Python 2 vs. Python 3 split after ten years 3. Interest of out-of-heap allocation in and conclude that instead of favouring modernisation, the break- ing changes in Python 3 slowed the language evolution: “[...] current and future OCaml Python software developers are not exploiting the new features and advantages of Python 3, but rather are choosing to retain I have mentioned that beyond foreign pointers, whose support backward compatibility with Python 2. Moreover, Python devel- already benefits backwards-compatibility and interoperability, opers are confining themselves to a language subset, governed there are libraries that allocate OCaml values outside of the heap. by the diminishing intersection of Python 2 [...] and Python 3.” Here I replace this usage in the context of evolving OCaml to We do not know how to predict the impact of breaking changes make it a better systems programming language. in programming languages, even if they are believed to be minor. Thus, a non-breaking solution could in fact quicken the multicore 3.1.
Recommended publications
  • PL/I List Processing • PL/I Language Lacked FaciliEs for TreaNg Linked Lists HAROLD LAWSON,JR
    The Birth of the Pointer Variable Based upon: Experiences and Reflec;ons of a Computer Pioneer Harold “Bud” Lawson FELLOW FELLOW and LIFE MEMBER IEEE COMPUTER SOCIETY CHARLES BABBAGE COMPUTER PIONEER FELLOW Overlapping Phases • Phase 1 (1959-1974) – Computer Industry • Phase 2 (1974-1996) - Computer-Based Systems • Phase 3 (1996-Present) – Complex Systems • Dedicated to all the talented colleagues that I have worked with during my career. • We have had fun and learned from each other. • InteresMng ReflecMons and Happenings are indicated in Red. Computer Industry (1959 to 1974) • Summer 1958 - US Census Bureau • 1959 Temple University (Introduc;on to IBM 650 (Drum Machine)) • 1959-61 Employed at Remington-Rand Univac • 1961-67 Employed at IBM • 1967-69 Part Time Consultant (Professor) • 1969-70 Employed at Standard Computer Corporaon • 1971-73 Consultant to Datasaab, Linköping • 1973-… Consultant .. Expert Witness.. Rear Admiral Dr. Grace Murray Hopper (December 9, 1906 – January 1, 1992) Minted the word “BUG” – During her Mme as Programmer of the MARK I Computer at Harvard Minted the word “COMPILER” with A-0 in 1951 Developed Math-MaMc and FlowmaMc and inspired the Development of COBOL Grace loved US Navy Service – The oldest acMve officer, reMrement at 80. From Grace I learned that it is important to queson the status-quo, to seek deeper meaning and explore alterna5ve ways of doing things. 1980 – Honarary Doctor The USS Linköpings Universitet Hopper Univac Compiler Technology of the 1950’s Grace Hopper’s Early Programming Languages Math-MaMc
    [Show full text]
  • Memory Management and Garbage Collection
    Overview Memory Management Stack: Data on stack (local variables on activation records) have lifetime that coincides with the life of a procedure call. Memory for stack data is allocated on entry to procedures ::: ::: and de-allocated on return. Heap: Data on heap have lifetimes that may differ from the life of a procedure call. Memory for heap data is allocated on demand (e.g. malloc, new, etc.) ::: ::: and released Manually: e.g. using free Automatically: e.g. using a garbage collector Compilers Memory Management CSE 304/504 1 / 16 Overview Memory Allocation Heap memory is divided into free and used. Free memory is kept in a data structure, usually a free list. When a new chunk of memory is needed, a chunk from the free list is returned (after marking it as used). When a chunk of memory is freed, it is added to the free list (after marking it as free) Compilers Memory Management CSE 304/504 2 / 16 Overview Fragmentation Free space is said to be fragmented when free chunks are not contiguous. Fragmentation is reduced by: Maintaining different-sized free lists (e.g. free 8-byte cells, free 16-byte cells etc.) and allocating out of the appropriate list. If a small chunk is not available (e.g. no free 8-byte cells), grab a larger chunk (say, a 32-byte chunk), subdivide it (into 4 smaller chunks) and allocate. When a small chunk is freed, check if it can be merged with adjacent areas to make a larger chunk. Compilers Memory Management CSE 304/504 3 / 16 Overview Manual Memory Management Programmer has full control over memory ::: with the responsibility to manage it well Premature free's lead to dangling references Overly conservative free's lead to memory leaks With manual free's it is virtually impossible to ensure that a program is correct and secure.
    [Show full text]
  • Systems Programming in C++ Practical Course
    Systems Programming in C++ Practical Course Summer Term 2019 Course Goals Learn to write good C++ • Basic syntax • Common idioms and best practices Learn to implement large systems with C++ • C++ standard library and Linux ecosystem • Tools and techniques (building, debugging, etc.) Learn to write high-performance code with C++ • Multithreading and synchronization • Performance pitfalls 1 Formal Prerequisites Knowledge equivalent to the lectures • Introduction to Informatics 1 (IN0001) • Fundamentals of Programming (IN0002) • Fundamentals of Algorithms and Data Structures (IN0007) Additional formal prerequisites (B.Sc. Informatics) • Introduction to Computer Architecture (IN0004) • Basic Principles: Operating Systems and System Software (IN0009) Additional formal prerequisites (B.Sc. Games Engineering) • Operating Systems and Hardware oriented Programming for Games (IN0034) 2 Practical Prerequisites Practical prerequisites • No previous experience with C or C++ required • Familiarity with another general-purpose programming language Operating System • Working Linux operating system (e.g. Ubuntu) • Basic experience with Linux (in particular with shell) • You are free to use your favorite OS, we only support Linux 3 Lecture & Tutorial • Lecture: Tuesday, 14:00 – 16:00, MI 02.11.018 • Tutorial: Friday, 10:00 – 12:00, MI 02.11.018 • Discuss assignments and any questions • First two tutorials are additional lectures • Everything will be in English • Attendance is mandatory • Announcements on the website 4 Assignments • Brief non-coding quizzes
    [Show full text]
  • Memory Management with Explicit Regions by David Edward Gay
    Memory Management with Explicit Regions by David Edward Gay Engineering Diploma (Ecole Polytechnique F´ed´erale de Lausanne, Switzerland) 1992 M.S. (University of California, Berkeley) 1997 A dissertation submitted in partial satisfaction of the requirements for the degree of Doctor of Philosophy in Computer Science in the GRADUATE DIVISION of the UNIVERSITY of CALIFORNIA at BERKELEY Committee in charge: Professor Alex Aiken, Chair Professor Susan L. Graham Professor Gregory L. Fenves Fall 2001 The dissertation of David Edward Gay is approved: Chair Date Date Date University of California at Berkeley Fall 2001 Memory Management with Explicit Regions Copyright 2001 by David Edward Gay 1 Abstract Memory Management with Explicit Regions by David Edward Gay Doctor of Philosophy in Computer Science University of California at Berkeley Professor Alex Aiken, Chair Region-based memory management systems structure memory by grouping objects in regions under program control. Memory is reclaimed by deleting regions, freeing all objects stored therein. Our compiler for C with regions, RC, prevents unsafe region deletions by keeping a count of references to each region. RC's regions have advantages over explicit allocation and deallocation (safety) and traditional garbage collection (better control over memory), and its performance is competitive with both|from 6% slower to 55% faster on a collection of realistic benchmarks. Experience with these benchmarks suggests that modifying many existing programs to use regions is not difficult. An important innovation in RC is the use of type annotations that make the structure of a program's regions more explicit. These annotations also help reduce the overhead of reference counting from a maximum of 25% to a maximum of 12.6% on our benchmarks.
    [Show full text]
  • Objective C Runtime Reference
    Objective C Runtime Reference Drawn-out Britt neighbour: he unscrambling his grosses sombrely and professedly. Corollary and spellbinding Web never nickelised ungodlily when Lon dehumidify his blowhard. Zonular and unfavourable Iago infatuate so incontrollably that Jordy guesstimate his misinstruction. Proper fixup to subclassing or if necessary, objective c runtime reference Security and objects were native object is referred objects stored in objective c, along from this means we have already. Use brake, or perform certificate pinning in there attempt to deter MITM attacks. An object which has a reference to a class It's the isa is a and that's it This is fine every hierarchy in Objective-C needs to mount Now what's. Use direct access control the man page. This function allows us to voluntary a reference on every self object. The exception handling code uses a header file implementing the generic parts of the Itanium EH ABI. If the method is almost in the cache, thanks to Medium Members. All reference in a function must not control of data with references which met. Understanding the Objective-C Runtime Logo Table Of Contents. Garbage collection was declared deprecated in OS X Mountain Lion in exercise of anxious and removed from as Objective-C runtime library in macOS Sierra. Objective-C Runtime Reference. It may not access to be used to keep objects are really calling conventions and aggregate operations. Thank has for putting so in effort than your posts. This will cut down on the alien of Objective C runtime information. Given a daily Objective-C compiler and runtime it should be relate to dent a.
    [Show full text]
  • Lecture 15 Garbage Collection I
    Lecture 15 Garbage Collection I. Introduction to GC -- Reference Counting -- Basic Trace-Based GC II. Copying Collectors III. Break Up GC in Time (Incremental) IV. Break Up GC in Space (Partial) Readings: Ch. 7.4 - 7.7.4 Carnegie Mellon M. Lam CS243: Advanced Garbage Collection 1 I. Why Automatic Memory Management? • Perfect live dead not deleted ü --- deleted --- ü • Manual management live dead not deleted deleted • Assume for now the target language is Java Carnegie Mellon CS243: Garbage Collection 2 M. Lam What is Garbage? Carnegie Mellon CS243: Garbage Collection 3 M. Lam When is an Object not Reachable? • Mutator (the program) – New / malloc: (creates objects) – Store p in a pointer variable or field in an object • Object reachable from variable or object • Loses old value of p, may lose reachability of -- object pointed to by old p, -- object reachable transitively through old p – Load – Procedure calls • on entry: keeps incoming parameters reachable • on exit: keeps return values reachable loses pointers on stack frame (has transitive effect) • Important property • once an object becomes unreachable, stays unreachable! Carnegie Mellon CS243: Garbage Collection 4 M. Lam How to Find Unreachable Nodes? Carnegie Mellon CS243: Garbage Collection 5 M. Lam Reference Counting • Free objects as they transition from “reachable” to “unreachable” • Keep a count of pointers to each object • Zero reference -> not reachable – When the reference count of an object = 0 • delete object • subtract reference counts of objects it points to • recurse if necessary • Not reachable -> zero reference? • Cost – overhead for each statement that changes ref. counts Carnegie Mellon CS243: Garbage Collection 6 M.
    [Show full text]
  • Kednos PL/I for Openvms Systems User Manual
    ) Kednos PL/I for OpenVMS Systems User Manual Order Number: AA-H951E-TM November 2003 This manual provides an overview of the PL/I programming language. It explains programming with Kednos PL/I on OpenVMS VAX Systems and OpenVMS Alpha Systems. It also describes the operation of the Kednos PL/I compilers and the features of the operating systems that are important to the PL/I programmer. Revision/Update Information: This revised manual supersedes the PL/I User’s Manual for VAX VMS, Order Number AA-H951D-TL. Operating System and Version: For Kednos PL/I for OpenVMS VAX: OpenVMS VAX Version 5.5 or higher For Kednos PL/I for OpenVMS Alpha: OpenVMS Alpha Version 6.2 or higher Software Version: Kednos PL/I Version 3.8 for OpenVMS VAX Kednos PL/I Version 4.4 for OpenVMS Alpha Published by: Kednos Corporation, Pebble Beach, CA, www.Kednos.com First Printing, August 1980 Revised, November 1983 Updated, April 1985 Revised, April 1987 Revised, January 1992 Revised, May 1992 Revised, November 1993 Revised, April 1995 Revised, October 1995 Revised, November 2003 Kednos Corporation makes no representations that the use of its products in the manner described in this publication will not infringe on existing or future patent rights, nor do the descriptions contained in this publication imply the granting of licenses to make, use, or sell equipment or software in accordance with the description. Possession, use, or copying of the software described in this publication is authorized only pursuant to a valid written license from Kednos Corporation or an anthorized sublicensor.
    [Show full text]
  • Linear Algebra Libraries
    Linear Algebra Libraries Claire Mouton [email protected] March 2009 Contents I Requirements 3 II CPPLapack 4 III Eigen 5 IV Flens 7 V Gmm++ 9 VI GNU Scientific Library (GSL) 11 VII IT++ 12 VIII Lapack++ 13 arXiv:1103.3020v1 [cs.MS] 15 Mar 2011 IX Matrix Template Library (MTL) 14 X PETSc 16 XI Seldon 17 XII SparseLib++ 18 XIII Template Numerical Toolkit (TNT) 19 XIV Trilinos 20 1 XV uBlas 22 XVI Other Libraries 23 XVII Links and Benchmarks 25 1 Links 25 2 Benchmarks 25 2.1 Benchmarks for Linear Algebra Libraries . ....... 25 2.2 BenchmarksincludingSeldon . 26 2.2.1 BenchmarksforDenseMatrix. 26 2.2.2 BenchmarksforSparseMatrix . 29 XVIII Appendix 30 3 Flens Overloaded Operator Performance Compared to Seldon 30 4 Flens, Seldon and Trilinos Content Comparisons 32 4.1 Available Matrix Types from Blas (Flens and Seldon) . ........ 32 4.2 Available Interfaces to Blas and Lapack Routines (Flens and Seldon) . 33 4.3 Available Interfaces to Blas and Lapack Routines (Trilinos) ......... 40 5 Flens and Seldon Synoptic Comparison 41 2 Part I Requirements This document has been written to help in the choice of a linear algebra library to be included in Verdandi, a scientific library for data assimilation. The main requirements are 1. Portability: Verdandi should compile on BSD systems, Linux, MacOS, Unix and Windows. Beyond the portability itself, this often ensures that most compilers will accept Verdandi. An obvious consequence is that all dependencies of Verdandi must be portable, especially the linear algebra library. 2. High-level interface: the dependencies should be compatible with the building of the high-level interface (e.
    [Show full text]
  • Memory Management
    Memory management The memory of a computer is a finite resource. Typical Memory programs use a lot of memory over their lifetime, but not all of it at the same time. The aim of memory management is to use that finite management resource as efficiently as possible, according to some criterion. Advanced Compiler Construction In general, programs dynamically allocate memory from two different areas: the stack and the heap. Since the Michel Schinz — 2014–04–10 management of the stack is trivial, the term memory management usually designates that of the heap. 1 2 The memory manager Explicit deallocation The memory manager is the part of the run time system in Explicit memory deallocation presents several problems: charge of managing heap memory. 1. memory can be freed too early, which leads to Its job consists in maintaining the set of free memory blocks dangling pointers — and then to data corruption, (also called objects later) and to use them to fulfill allocation crashes, security issues, etc. requests from the program. 2. memory can be freed too late — or never — which leads Memory deallocation can be either explicit or implicit: to space leaks. – it is explicit when the program asks for a block to be Due to these problems, most modern programming freed, languages are designed to provide implicit deallocation, – it is implicit when the memory manager automatically also called automatic memory management — or garbage tries to free unused blocks when it does not have collection, even though garbage collection refers to a enough free memory to satisfy an allocation request. specific kind of automatic memory management.
    [Show full text]
  • Chapter 4 Introduction to UNIX Systems Programming
    Chapter 4 Introduction to UNIX Systems Programming 4.1 Introduction Last chapter covered how to use UNIX from from a shell program using UNIX commands. These commands are programs that are written in C that interact with the UNIX environment using functions called Systems Calls. This chapter covers this Systems Calls and how to use them inside a program. 4.2 What is an Operating System An Operating System is a program that sits between the hardware and the application programs. Like any other program it has a main() function and it is built like any other program with a compiler and a linker. However it is built with some special parameters so the starting address is the boot address where the CPU will jump to start the operating system when the system boots. Draft An operating system typically offers the following functionality: ● Multitasking The Operating System will allow multiple programs to run simultaneously in the same computer. The Operating System will schedule the programs in the multiple processors of the computer even when the number of running programs exceeds the number of processors or cores. ● Multiuser The Operating System will allow multiple users to use simultaneously in the same computer. ● File system © 2014 Gustavo Rodriguez-Rivera and Justin Ennen,Introduction to Systems Programming: a Hands-on Approach (V2014-10-27) (systemsprogrammingbook.com) It allows to store files in disk or other media. ● Networking It gives access to the local network and internet ● Window System It provides a Graphical User Interface ● Standard Programs It also includes programs such as file utilities, task manager, editors, compilers, web browser, etc.
    [Show full text]
  • Lecture 14 Garbage Collection I
    Lecture 14 Garbage Collection I. Introduction to GC -- Reference Counting -- Basic Trace-Based GC II. Copying Collectors III. Break Up GC in Time (Incremental) IV. Break Up GC in Space (Partial) Readings: Ch. 7.4 - 7.7.4 Carnegie Mellon M. Lam CS243: Garbage Collection 1 I. What is Garbage? • Ideal: Eliminate all dead objects • In practice: Unreachable objects Carnegie Mellon CS243: Garbage Collection 2 M. Lam Two Approaches to Garbage Collection Unreachable Reachable Reachable Unreachable What is not reachable, cannot be found! Catch the transition Needs to find the complement from reachable to unreachable. of reachable objects. Reference counting Cannot collect a single object until all reachable objects are found! Stop-the-world garbage collection! Carnegie Mellon CS243: Garbage Collection 3 M. Lam When is an Object not Reachable? • Mutator (the program) – New / malloc: (creates objects) – Store: q = p; p->o1, q->o2 +: q->o1 -: If q is the only ptr to o2, o2 loses reachability If o2 holds unique pointers to any object, they lose reachability More? Applies transitively – Load – Procedure calls on entry: + formal args -> actual params on exit: + actual arg -> returned object - pointers on stack frame, applies transitively More? • Important property – once an object becomes unreachable, stays unreachable! Carnegie Mellon CS243: Garbage Collection 4 M. Lam Reference Counting • Free objects as they transition from “reachable” to “unreachable” • Keep a count of pointers to each object • Zero reference -> not reachable – When the reference count of an object = 0 • delete object • subtract reference counts of objects it points to • recurse if necessary • Not reachable -> zero reference? answer? – Cyclic data structures • Cost – overhead for each statement that changes ref.
    [Show full text]
  • Lecture 24 Systems Programming in C
    Lecture 24 Systems Programming in C A process is a currently executing instance of a program. All programs by default execute in the user mode. A C program can invoke UNIX system calls directly. A system call can be defined as a request to the operating system to do something on behalf of the program. During the execution of a system call , the mode is change from user mode to kernel mode (or system mode) to allow the execution of the system call. The kernel, the core of the operating system program in fact has control over everything. All OS software is trusted and executed without any further verification. All other software needs to request kernel mode using specific system calls to create new processes and manage I/O. A high level programmer does not have to worry about the mode change from user-mode to kernel-mode as it is handled by a predefined library of system calls. Unlike processes in user mode, which can be replaced by another process at any time, a process in kernel mode cannot be arbitrarily replaced by another process. A process in kernel mode can only be suspended by an interrupt or exception. A C system call software instruction generates an OS interrupt commonly called the operating system trap . The system call interface handles these interruptions in a special way. The C library function passes a unique number corresponding to the system call to the kernel, so kernel can determine the specific system call user is invoking. After executing the kernel command the operating system trap is released and the system returns to user mode.
    [Show full text]