페이지

2022년 4월 22일 금요일

1.5.2 Memory Management

 As discussed in Section 1.2.2, the main memory is central to the operation of a modern computer system. Main memory is a large array of bytes, ranging in size from hundreds of thousands to billions. Each byte has its own address. Main memory is a repository of quickly accessible data shared by the CPU and I/O devices. The CPU reads instructions from main memory during the  instruction-fetch cycle (on a von Neumann architecture). As noted earlier, the main memory is generally the only large storage device that the CPU is able to address and access directly. For example, for the CPU to process data from disk, those data must first be transferred to main memory by CPU-generated I/O calls. In the same way, instructions must be in memory for the CPU to execute them.

For a program to be executed, it must be mapped to absolute addresses and loaded into memory. As the program executes, it accesses program instructions and data from memory by generating these absolute addresses. Eventually, the program terminates, its memory space is declared available, and the next program can be loaded and executed.

To improve both the utilization of the CPU and the speed of the computer's in memory, creating a need for memory management. Many different memory management schemes are used. These schemes relect various approaches, and the effectiveness of any given algorithm depends on the situation. In selecting a memory-management scheme for a specific system, we must take into account many factors-especially the hardware design of the system. Each algorithm requires its own hardware support.

The operating system is responsiblie for the following activities in connection with memory management:

- Keeping track of which parts of memory are currently being used and which process is using them

- Allocating and deallocating memory space as needed

- Deciding which processes (or parts of processes) and data to move into and out of memory

Memory-management techniques are discussed in Chapter 9 and Chapter 10.

2022년 4월 19일 화요일

1.5.1 Process Management

 A program can do nothing unless its instructions are executed by a CPU. A program in execution, as mentioned, is a process. A program such as a compiler is a process, and a word-processing program being run by  an individual user on a PC is a process. Similarly, a social media app on a mobile device is a process. For now, you can consider a process to be an instance of a program in execution, but later you will wee that the concept is more general. As described in Chapter 3, it is possible to provide system calls that allow processes to create subprocesses to execute concurrently.

A process needs certain resources-including CPU time, memory, files, and I/O devices-to accomplish its task. These resources are typically allocated to the process while it is running. In addition to the various physical and logical resources that a process obtains when it is created, various initlization data(input) may be passed along. For example, consider a process running a web browser whose function is to display the contents of a web page on a screen. The process will be given the URL, as an input and will execute the appropriate instructions and system calls to obtain and display the desired information on the screen. When the process terminates, the operaing system will reclaim any reusable resources.

We emphasize that a program by itself is not a process. A program is a passive entity, like the contents of a file stored on disk, whereas a process is an active entity. A single-threaded process has one program counter specifying the next instruction to execute.(Threads are coverd in Chapter 4). The execution of such a process must be sequential. The CPU executes one instuction of the process after another, until the process completes. Further, at any time, one instuction at most is executed on behalf of the process. Thus, although two process may be associated with the same program, they are nevertheless considered two separate execution sequences. A multithreaded process has multiple program counters, each pointing to the next instruction to execute for a given thread.

A process is the unit of work in a system. A system consists of a collection of processes, some of which are operating-system processes (those that execute system code) and the rest of which are user processes (those that execute user code). All these processes can potentially execute concurrently-by multiplexing on a single CPU core-or in paralled across multiple CPU cores.

The operaing system is responsible for the following activities in connection with process management:

- Creating an deleting both user and system processes

- Scheduling processes and threads on the CPUs

- Supending and resuming processes

-Providing mechanisms for process synchronization

- Providing mechanisms for process communication

We discuss process-management techniques in Chapter 3 through Chapter 7.


1.5 Resource Management

 As we have seen, an operating system is a resource manager. The system's CPU, memory space, file-storage space, and I/O devices are among the reources that the operating system must manage.

LINUX TIMERS

 On Linux systems, the kernel configuration parameter HZ specifies the frequency of timer interrupts. An HZ value of 250 means that the timer generates 250 interrupts per second, or one interrupt every 4 milliseconds. The value of HZ depends upon how the kernel is configured, as well the machine type and architecture on which it is running. A related kernel variable is jiffies, which represent the number of timer interrupts that have occurred since the system was booted. A programming project in Chapter 2 further explores timing in the Linux kernel.

1.4.3 Timer

 We must ensuire that the operating system maintains control over the CPU. We cannot allow a user program to get stuck in an  infinite loop or to fail to call system services and never return control to the operating system. To accomplish the goal, we can use a timer. A timer can be set to interrupt the computert after a specifed period. The period may be fixed (for example, 1/60 second) or variable (for example, from 1 millisecond to 1 second). A variable timer is generally implemented by a fixed-rate clock and a counter. The operating system sets the counter. Every time the clock ticks, the counter is decremented. When the counter reaches 0, an interrupts at intervals from 1 millisecond to 1,024 millisecond clock allows interrupts at intervals from 1 millisecond to 1,024 milliseconds, in steps of 1 millisecond.

Before turning over control to the user, the operating system ensures that the timer is set to interrupt. If the timer interrupts, control transfers automatically to the operating system, which may treat the interrupt as a fatal error or may give the program more time. Clearly, instructions that modify the content of the timer are privileged.

2022년 4월 18일 월요일

1.4.2 Dual-Mode and Multimode Operation

 Since the operating system and its users share the hardware and sofware resources of the computer system, a properly designed opoerating system must ensure that an incorrect (or malicious) program cannot cause other programs-or the operating system itself-to execute incorrectly. In order to ensure the proper execution of the system, we must be able to distinuish between the execution of operating-system code and user-defined code. The approach taken by most computer systems is to provide hardware support that allows differentiation among various modes of execution.

At the vary least, we need two separate modes of operating; user mode and kernel mode(also called supervisor mode, system mode, or privileged mode). A bit, called the mode bit, is added to the hardware of the computer to indicate the current mode; kernel(0) or user(1). With the mode bit, we can distinguish between a task that is executed on behalf of the operating system and one that is executed on behalf of the user. When the computer system is executing on behalf of a user application, the system is in user mode. However, when a user application requests a service from the operating system(via a system call), the system must transition from user to kernel mode to fulfill the request. This is shown in Figure 1.13. As we shall see, this architectural enhancement is useful for many other aspects of system operation as well.

At system boot time, the hardeare starts in kernel mode. The operating system is then loaded and starts user applications in user mode. Whenever a trap or interrupt occurs, the hardware switches from user mode to kernel mode(that is, changes the state of the mode bit to 0). Thus, whenever the operating system gains control of the computer, it is in kernel mode. The system always switches to user mode(by setting the mode bitg to 1)before passing control to a user program.

The dual mode of operation provides us with the means for protecting the operating system from errant users-and errant users from one another. We accomplish this protection by designating  some of the machine instructions that may cause harm as privileged instructions. The hardware allows privileged instructions to be executed only in kernel mode. If an attempt is made to execute a privileged instruction in user mode. the hardware does not execute the instruction but rather treats it as illegal and traps it to the operating system.

The instruction to switch to kernel mode is an example of a privileged instruction. Some other examples incude I/O control. timer management, and interrupt management. Many additional privileged instructions are discussed throughout the text.

The concept of modes can be extended beyond two modes. For example Intel processors have four separate protection rings, where ring 0 is kernel mode and ring 3 is user mode. (Although rings 1 and 2 could be used for various operating-system services, in practice they are rarely used.) ARMv8 systems have seven modes. CPUs that support virtualization (Section 18.1) frequently have a separate mode to indicate when the virtual machine manager(VMM)is in control of the system. In this mode, the VMM has more privileges than user processes but fewer than the kernel. It needts that level of provilege so it can create and manage virtual machines, changing the CPU state to do so.

We can now better understand the  life cycle of instruction execution in a computer system. Initial control resides in the operating system, where instructions are executed in kernel mode. When control is given to a suer application, the mode is set to user mode. Eventually, control is switched back to the operating system via an interrupt, trap, or a system call. Most contem porary operating systems-such as Microsoft Windows, Unix, and Linux-take advantage of this dual0-mode feature and provide greater protection for the operating system.

System calls provide the means for a user program to ask the operating system to perform tasks rewserved for the operating system on the user program's behalf. A system call is invoked in a variety of ways, depending on the functionality provided by the underlying processor. In all forms, it is the method used by a process to request action by the operating system. A system call usually takes the form of a trap to a specific location in the interrupt vector. This trap can be executed by a generic trap instruction, although some systems have a specific syscall inst5ruction to invoke a system call.

When a system call is executed, it is typically treated by the hardware as a software interrupt. Control passes though the interrupt vector to a service rouine in the operating system, and the mode gbit is set to kernel mode. The system-call service routine is a part of  the operating system. The kernel examines the interrupting instruction to determine what system call has occurred; a parameter indicates what type of service the user program is requesting Additional information needed for the request may be passed in registers, on the stack, or in memory (with pointers to the memory locations passed in registers). The kernel verifies that the parameters are correct and legal, executes the request, and returns control to the instruction following the system call. We describe system calls more fully in Section 2.3.

Once hardware protection is in place, it detects errors that violate modes. These errors are normally handled by the operating system. If a user program fails in some way-such as by making an attempt either to execute an illegal instruction or to access memory that is not in the user's address space-than the hardware traps to the operating system. The trap transfers control through the interrupt vector to the operating system. The trap transfers control through the interrupt vector to the operating system, just as an interrupt does. Wehn a program error occurs, the operating system must terminate the program abnormally. This situaltion is handled by the same code as a user-requested abnormal termination. An appropriate error message is given, and the memory of the program may be dumped. The memroy dump is usually written to a file so that the user or programmer can examine it and perhaps conrrect it and restart the program.



1.4.1 Multiprogramming and Multitasking

 One of the most important aspects of operating systems is the ability to run multiple programs, as a single program cannot, in general, keep either the CPU or the I/O devices busy at all times. Furthemore, users typically want to run more than one program at a time as well. Multiprotramming increases CPU utilization, as well as keeping users satisfied, by oranizing programs so that the CPU always has one to execute. In a multiprogrammed system, a program in execution is termed a process.

The idea is as follows: The operaing system keeps several processes in memory simultaneously (Figure 1.12). The operating system picks and begins to execute one of these processes. Eventually, the process may have to wait for some task, such as an I/O operation, to complete. In a non-multiprogrammed system, the CPU would sit idle. In a multiprogrammed system, the operating system simply switches to, and executes, another process. When that process needs to wait, the CPU switches to another process, and so on. Eventurally, the first process finishes waiting and gets the CPU back, As long as at least one process needs to execute, t he CPU is never idle.

This idea is common in other life situations. A lawyer does not work for only one client at a time, for example. While one case is waiting to go to trial or have papers typed, the lawyer can work on another case. If she has enough clients, the lawyer will never be idle for lack of work. (Idle lawyers tend to become politicians, so there is a certain social value in keeping lawyers busy.)

Multitasking is a logical extension of multiprogramming. In multitasking systems, the CPU execute multiple processes by switching among them, but the switches occur frequently, providing the user with a fast response time. Consider that when a process executes, it typically executes for only a short time before it either finishes or needs to perform I/O. I/O may be interactive; that is, output goes to a sidplay for the user, and input comes from a user keyboard, mouse, or touch screen. Since interactive I/O typically runs at "people speeds," it may taske a long time to complete. Input, for example, may be bounded by the user's typing speed; seven characters per secound is fast for people but incredibly slow for computers. Rather than let the CPU sit idle as this interactive input taskes place, the operating system will repidly switch the CPU to another process.

Having serral processes in memory at the same time requires some form of memory management, which we cover in Chapter 9 and Chapter 10. In addition, if serveral processes are ready to run at the same time, the system must choose which process will run next. Making this desicion is CPU scheduling, which is discussed in Chapter 5. Finally, running multiple processes concurrently requires that their ability to affect one another be limited in all phases of the operating system, including process scheduling, disk storage, and memory management. We discuss these considerations throughout the text.

In a multitasking system, the operating system must ensure reasonable response time. A common method for doing so is virtual memroy, a technique that allows the execution of a process that is not completely in memory(Chapter 10). The main advantage of this scheme is that it enables users to run programs that are larger than actural physical memory. Futher, it abstracts main memory into a large, uniform array of storage, separating logical memory as viewed by the user from physical memory. This arragement frees programmers from concern over memory-storage limitations.

Multiprogramming and multitasking system must also provide a file system(Chapter 13, Chapter 13, and Chapter 15). The file system resides on a secondary storage; hence, storage management must be provided(Chapter 11). In addition, a system must protect resources from inappropriate use (Chapter 17). To ensure orderly execution, the system must also provide mechanisms for process synchronization and communication(Chater 6 and Chapter7), and it may ensure that processes do not get stuck in a deadlock, forever waiting for one another(Chapter 8).