Basics of Computer Systems Lecture 4 PDF
Document Details
Uploaded by EfficientCentaur4176
Tags
Summary
This document is a lecture on computer systems, focusing on operating systems, program versus process concepts, process scheduling, and memory management. The lecture provides a foundational understanding of these key computer science topics and discusses practical examples and use cases.
Full Transcript
Basics of Computer Systems Lecture 4 In the previous session, we have discussed what an Operating System (OS) is and what it does. - The OS is situated between the computer hardware and application programs, makes the computer convenient to use by offering generic services to programs...
Basics of Computer Systems Lecture 4 In the previous session, we have discussed what an Operating System (OS) is and what it does. - The OS is situated between the computer hardware and application programs, makes the computer convenient to use by offering generic services to programs, and takes care of control and isolation of the hardware. Within this session, we will take a more in depth look at some of the operating system's functionality and the various management tasks in particular Operating System Functionality More in particular, today we will discuss the role of the operating system, on providing the following functionality Management of programs in execution Management of main memory (RAM) File systems Device management Networking Program versus process Two weeks ago, we talked about computer programs at an elementary level. These are long sequences, or compositions, of basic instructions. Programs are stored on disk and can be downloaded from the internet. A program = a static, inactive, entity. It is just a listing of instructions stored somewhere. Now what if we want to execute a program? Program versus process (2) The execution of a program starts with a request to the OS: “Hi, I would like to run this program.” - With “this” refer to the location of the program, we want to run on the storage medium. In turn the OS will take the necessary actions to bring the program to execution: Reserve space in the computer’s main (working) memory Load the program into memory Create a program counter, which remembers the current instruction of the program that we are executing Start execution at the first instruction of the program, Program versus process (3) So, to execute a program, we need more than just the program (lists of instructions): some areas in memory, a program counter Whereas a program is an inactive entity, a program in execution is a dynamic and active entity ➔ A program in execution is called a process We have multiple active instances (processes) of the same program Creation of processes To run a program, we need to create a new process This is a key service offered by any operating system. It is often received through a system call. - The specifics differ per operating system and are for now not a concern for us. Each process is assigned a unique identifier (PID: Process Identifier), often simply a number Creation of processes (2) Commonly, each process can create new processes. We say that a parent process can create child processes. - In turn, the children can create children again This leads to a tree structure, a tree of processes Process scheduling A simple CPU can execute one program at a time: or work on one process at a time. Contemporary CPUs have a certain number of cores. For example, your smartphone can have 6 cores. - Each core can execute a program, independently - So a 6-core smartphone can work on 6 processes simultaneously Process scheduling (2) Still a modern computer has many active processes. A typical laptop has 100-200 processes in memory. If the laptop is in active use, 10-20 processes could be actively making progress. - However, if our laptop only has 6 cores, how do we go about this? We need some procedure to decide which process to run when and on which core = referred to as process scheduling Moreover, computers are switching between different processes to give each active process a change to make progress. Something that is known as multi-tasking (next lecture) Process termination We need to have the ability to stop, or rather terminate, a process In many cases we indicate within an application that we want to quit using it. We quit the application. - The application will save files, clean up and then ask the operating system to be terminated (another system call) - This is a case of voluntary process termination Process termination (2) Naturally, the other case is known as involuntary termination. We need this functionality to terminate processes that are misbehaving or that are working a on a computation which result is no longer needed. All of us have likely seen examples of misbehaving applications: - On macOS you might have had to “Force Quit” an application because it no longer responses - Windows calls this “End Task”, see task manager - Also on iOS and Android it is possible t force quit misbehaving apps Memory management The working memory (RAM) is an essential component of a computer and it is managed by the operating system When the computer starts up, the OS determines how much memory is present in the syste (example: 16 gigabytes) The OS reserves some memory for its ow operation The remainder can be handed out to processes. This is done by requests (= through system calls) Memory management (2) When a program is brought into memory to create a new process, some memory is already allocated This is commonly done according to some pre-determined structure, see figure - The text “segment” contains the instructions of the program - The data segment contains initialization data for the program - Memory that is allocated by the program during execution can be hosted by the heap or stack, which grows towards each other Memory management (3) Many programs allocate memory during their execution: dynamic memory allocation Simplified example: the program would like to show an image to the user. - The image is to be loaded from a disk or from the internet - We need to determine the size of the image in bytes - The program then requests an area of memory to be allocated that fits the amount of bytes. - The OS informs the program at which location in memory the space was allocated Memory management (4) When managed by human programmers, dynamic memory allocation leads to various problems All memory areas that are allocated, also need to be released, to indicate that the allocated area is no longer needed (and can be used by somebody else). Humans are forgetful and human programmers are as well. We can bee what happens now: forgetting to release memory is a common problem Memory leaks Let’s see why forgetting to release memory is a problem: - Assume we have a long running program (hours / days / weeks). - The program occasionally allocates memory. - The program never forgets to release allocated memory (extreme case). - Consider that memory allocations take memory from a pool of available memory; release puts back memory into the pool. - So, if the program only allocates and never releases, at some point the pool will be empty! - The system then runs out of memory and none of the processes can allocate any more memory Programmers refer to this as a “process that leaks memory” or “memory leaks” Buffer overflow Another common problem with dynamic memory allocations managed by humans are buffer overflows. These also have implications for computer security For example, consider that after some allocations, a program has the following data arrangement in memory: The program now receives a message. It will read the message until a termination character that indicates the end of the message and store it in the allocated space (a "buffer") for a received message until further use. Turns out the message was 150 characters in size. The program only allocated 128. The program has now written beyond the end of the space for the message and has written the 22 remaining bytes over other data in the "information for program execution". Buffer overflow (2) The problem here is the following The program allocated space for 128 bytes When the program started writing characters to this space, it never checked whether it went beyond the maximum of 128 bytes - Certain programming languages don’t perform these checks automatically. And human programmers are forgetful What should have happened is that the program checked the length of the received message before starting to write to the buffer; or stopping to write when the end of the buffer was reached Buffer overflow (3) The consequences can be serious and depend on what data is stored adjacent (next to) of our overrun buffer In our example this data is "information for program execution information". Overwriting this data may Cause the program to malfunction or crash later on Change the course of the program execution It is even possible that the program will start executing instructions that were contained in the received message (!) Buffer overflow (4) You might have heard of instances where somebody's phone was hacked or infiltrated by means of a WhatsApp message. Essentially, how this happens is as follows: - By analysis of the WhatsApp program, some weakness is found (e.g. forgot to check the length). - A hacker creates a specially crafted message that abuses this weakness. For example, the message causes a buffer overflow and causes the program to start executing program instructions sent by the hacker. - The hacker now has the victim's phone partly under control. Swapping technique Mian memory (RAM) is a finite resource: at some point it will be full. This puts a limit on the number of programs that can be present in main memory at the same time. Many OS implement techniques to make the main memory seem bigger than it actually is One such technique is to only load the part of the program that is currently used into the memory. Other parts are loaded on demand. - You can compare this to old games, where you were shown a "Loading..." screen when transitioning to another level or another area of the game world. Swapping technique (2) Another technique is to move the memory of processes that are currently inactive to the hard drive (secondary storage) This frees up main memory and as a consequence, another process can be brough back from the hard drive to main memory While different processes take turn, we keep moving the memory content back and forth form the drive = We refer to this as swapping Swapping Technique in smartphones Smartphones typically do not implement swapping. You might, however, have observed the following: - When switching to an app you have used recently it shows up quickly. - Switching to an app you have not used for a while appears to you to be still loaded into memory, however there is a slight delay in the app being loaded to the front and become fully active again. In the latter case, the OS was faced with a situation that the main memory was almost full. It unloaded processes (apps) that have not been used for a while from the main memory. - When you switch to the app, the program is loaded again from the phone's flash storage to the main memory. This causes the slight delay. File management Processes can access files on storage devices attached to the computer The typical flow is that a process opens a file, then performs read and write instructions and finally closes a file when done. For each process the OS maintains a table of files that the process has opened. - For example, in this table it is maintained where in the file a process is with reading the file. Kind of like a bookmark when reading a book The OS takes care of storing all the files and folders on a storage device, and also retrieving this data in the future (lecture 8) Device management An OS fulfils two tasks: 1. The OS enables processes to be able to work with many different devices in a generic way a. When you connect a new device, a device driver is registered with the OS. This driver is a program code that the OS can use to work with a device it previously did not know 2. It coordinates access to devices. By being an intermediary between the hardware and the software, the OS can ensure that access to devices happen in an orderly manner. Device management (2) In device management, two classes of devices are distinguished: Random Access Devices: in this case, a device is able to handle requests from different processes intertwined Example: a hard drive. When two processes are reading data (a file) from a hard drive, the hard drive can read a piece for process A, then a piece for process B, then A, B, and so on. Character devices: if multiple processes read/write from such a device, the accesses data will be intermixed. Example: a printer. When two processes send data to the printer at the same time, the sent data will appear intermixed on the sheets of paper. Solution: A technique called spooling is used: first we capture all data sent by process A. Then all this data is sent at once to the printer. Only after that, we will send the data of another process. Networking Just as with opening files, processes can open network connections to other computers - A process can open a network connection, read and write (receive and send) characters or bytes, and close the network connection This is used for example to: Send a message to another computer Or to retrieve a web page from a webserver What is happening in these cases is that two processes on different computers are communicating with each other