# IB/Group 4/Computer Science/Resource Management

A computer generally has one operating system that becomes active and takes control when the system is turned on. Computer hardware is wired to initially load a small set of system instructions that is stored in permanent memory. You've likely used at least one operating system before. The various versions of Microsoft Windows (from NT, to XP, and Vista to 8) are popular choices for personal computers. UNIX has been a favourite of serious programmers for years, and a version of UNIX called Linux is popular for personal computer systems.
6.1.1 Identify the resources that need to be managed within a computer system.
The various roles of an operating system generally revolve around the idea of "sharing nicely." An operating system manages resources, and these resources are often shared in one way or another among the various programs that want to use them: multiple programs executing concurrently share the use of main memory; they take turns using the CPU; and they compete for an opportunity to use input and output devices. The operating system acts as the playground monitor, making sure that everyone cooperates and gets a chance to play.

## System resources
### Memory management

6.1.7 Outline OS resource management techniques: scheduling, policies, multitasking, virtual memory, paging, interrupt, polling.
An executing program resides in main memory and its instructions are processed one after another in the fetch-decode-execute cycle. Multiprogramming is the technique of keeping multiple programs in main memory at the same time; these programs compete for access to the CPU so that they can do their work. All modern operating systems employ multiprogramming to one degree or another. An operating system must therefore perform memory management to keep track of what programs are in memory and where in memory they reside.

#### Primary memory

| 0 | ... |
| 1 | ... |
| 2 | ... |
| 20347 | ... |
| 20348 | ... |
| 20349 | ... |
Memory is a continuous set of bits referenced by specific addresses.
All programs are stored in main memory when they are executed. All data referenced by those programs are also stored in main memory so that they can be accessed. Main memory can be thought of as a big continuous chunk of space divided into groups of 8, 16, and 32 bits. Each byte or word of memory has a corresponding address, which is simply an integer that uniquely identifies that particular part of memory. To the right there is a representation of memory, where the first memory address is 0.
6.1.5 Explain the role of the operating system in terms of managing memory, peripherals and hardware interfaces.
Earlier it was stated that in a multiprogramming environment, multiple programs (and their data) are stored in main memory at the same time. Thus, operating systems must employ techniques to:
* track where and how a program resides in memory, and
* convert logical program addresses into actual memory addresses.
A program is filled with references to variables and to other parts of the program code. When the program is compiled, these references are changed into the addresses in memory where the data and code resides. But since one doesn't know exactly where a program will be loaded into main memory, how can one know what address to use for anything?
The solution is to use two kinds of addresses: logical addresses and physical addresses. A logical address (sometimes called a virtual or relative address) is a value that specifies a generic location, relative to the program but not to the reality of main memory. A physical address is an actual address in the main memory device − again shown in the figure.
When a program is compiled, a reference to an identifier (such as a variable name) is changed to a logical address. When the program is eventually loaded into memory, each logical address finally corresponds to a specific physical address. The mapping of a logical address to a physical address is called address binding. Logical addresses allow a program to be moved around in memory or loaded in different places at different times. As long as one keeps track of where the program is stored, one is always able to determine the physical address that corresponds to any given logical address.
The following sections examine the underlying principles of there main memory management techniques. To simplify examples, all address-binding calculations are performed in base 10.

##### Paged memory management
#### Virtual memory

6.1.7 Outline OS resource management techniques: scheduling, policies, multitasking, virtual memory, paging, interrupt, polling.
The demand paging approach gives rise to the idea of virtual memory, the illusion that there are no restrictions on the size of a program (because the entire program is not necessarily in memory at the same time). In all of the memory management techniques examined earlier, the entire process had to be brought into memory as a continuous whole. Therefore there is always an upper bound on process size. Demand paging removes that restriction.
However, virtual memory comes with lots of overhead during the execution of a program. With other memory management techniques, once a program was loaded into memory, it was all there and ready to go. With the virtual memory approach, one constantly has to swap pages between main and secondary memory. This overhead is usually acceptable - while one program is waiting for a page to be swapped, another process can take control of the CPU and make progress. Excessive page swapping is called thrashing and can seriously degrade system performance.

##### Limitations and Consequences

6.1.3 Identify the limitations of a range of resources in a specified computer system
6.1.4 Describe the possible problems resulting from the limitations of the resources in a computer system.
Limitations in the resources of a specific computer system are often closely related to the hardware of the system and its capabilities. Limitations could include:
| Component | Possible Limitations | Possible Consequences |
| RAM | Most computers today have 4 GB of RAM. The main possible limitation of RAM is running out of memory space if a program requires a large amount of memory. More rarely, the RAM may not be reliable (non-ECC) or too slow (high timing values). | All operating systems require a minimum amount of RAM to operate. For example, Windows 8.1 x86_64 requires at least 2 GB of RAM, without the consequences would be that the computer system would not function. Insufficient RAM may also cause problems with multitasking, as having virtual memory write to disk is far slower than RAM access. Finally, some applications (video processing) require large amounts of RAM. Without this, the programs will still run, but waste large amounts of user time on loading. |
| CPU | Most computers today have between 2-4 processors. A processor could have a clock speed that is too low. Alternatively, a CPU may have an insufficient number of threads. | A slow CPU will limit the number of calculations that can be performed per second. Without multiple cores, multitasking will be slower. Additionally, a 32 bit CPU will not run 64-bit OS or applications, which some specialized users require. |
| Graphics Card | A graphics card may not have enough processing power to need the demands of a power user. Additionally, the GPU may not have enough video memory to be compatible with a high resolution display. | Consequences include the card not functioning with higher resolution displays (especially with 4K displays currently entering the market). The GPU directly affects the number of frames that can be rendered per second. This has an impact on how smooth videos appear. A weak GPU may not be able to fit the demands of specialized users, such as video editors or gamers. |
| Network Card | A network card can be limited by its speed. Most ethernet cables are 1 Gbps, but old ones can be 10/100 Mbps. This may be insufficient for some applications. Additionally, a network card need to be reliable. | A slow network card may not have enough bandwidth for certain uses, such as streaming videos. A unreliable network card will drop many cards and be unsuitable for VoIP. |

#### Secondary memory

The most widely used secondary storage device is the Hard Disk. It includes both the hard drives that are found in the computer's main casing and disks that are portable and can be moved easily between computers. Storing data onto secondary storage requires a mechanism of organising the electron medium. As such, the use of individual files are used, and from the user's point of view, the smallest amount of data that can be written to secondary memory. A file system is the natural solution to managing these files, and is the logical view that an operating system provides so that users can manage data as a collection of files. A file system is often organized by grouping files into directories. Additionally, a file is a generic concept. Different types of files are managed in different ways. A file, in general, contains a program (in some form) or data (of one type or another). Some files have a very rigid format; others are more flexible.
The most important hardware device used as secondary memory is the magnetic disk drive. File systems stored on these drives must be accessed in an efficient manner. It turns out that transferring data to and from secondary memory is the worst bottleneck in a general computer system.
The speed of the CPU and the speed of main memory are much faster than the speed of data transfer to and from secondary memory such as a magnetic disk. That's why a process that must perform I/O to disk is made to wait while that data is transferred, to give another process a chance to use the CPU.
Because secondary I/O is the slowest aspect of a general computer system, the techniques for accessing data on a disk drive are of crucial importance to file systems. As computer deals with multiple processes over a period of time, requests to access the disk accumulate. The technique that the operating system uses to determine which requests to satisfy is called disk scheduling.
A magnetic disk drive is organized as a stack of platters, where each platter is divided into tracks, and each tracks is divided into sectors. The set of corresponding tracks on all platters is called a cylinder.

##### Polling

6.1.7 Outline OS resource management techniques: scheduling, policies, multitasking, virtual memory, paging, interrupt, polling.

##### Paging

6.1.7 Outline OS resource management techniques: scheduling, policies, multitasking, virtual memory, paging, interrupt, polling.

##### Limitations

6.1.3 Identify the limitations of a range of resources in a specified computer system

###### Consequences

6.1.4 Describe the possible problems resulting from the limitations of the resources in a computer system.

### Process management

Another important resource that an operating system must manage is the use of the CPU by individual processes. TO understand how an operating system manages processes, one must recognise the stages that a process goes through during its computational life and understand the information that must be managed to keep a process working correctly in a computer system.

### The process states

6.1.5 Explain the role of the operating system in terms of managing memory, peripherals and hardware interfaces.
Processes move through specific stages as they are managed in a computer system. That is, a process enters the system, is ready to be executed, is executing, is waiting for a resource, or is finished. Below is flow chart that represents these process states.
In the figure, each box represents a state a process might be in, and the arrows indicate how and why a process might move from one state to another.
Examining what's happening to a process in each state:
* In the new state, a process is being created. It may, for instance, be a login process created by a user logging onto a timesharing system, an application process created when a user submits a program for execution, or a system process created by the operating system to accomplish a specific system task.
* A process that has no barriers to its execution is in the ready state. A process in the ready state is not waiting for an event to occur or for data to be brought in from secondary memory. Instead, it is waiting for its chance to use the CPU.
* A process in the running state is currently being executed by the CPU. Its instructions are being processed in the fetch-execute cycle.
* A process in the waiting state is currently waiting for resources (other than the CPU). For example, a process in the waiting state may be waiting for a page of its memory to be brought in from secondary memory or for another process to send it a signal that it may continue.
* A process in the terminate state has completed its execution and is no longer active. At this point the operating system no longer needs to maintain the information regarding the process.
Note that many processes may be in the ready state or the waiting state at the same time, but only one process can be in the running state.
After the process is created, the operating system admits it to the ready state. When the CPU scheduling algorithm dictates, a process is dispatched to the running state.
While running, the process might be interrupted by the operating system to allow another process to have its chance on the CPU. In that case, the process simply returns to the ready state. Alternatively, a running process might request a resource that is not available or requires I/O to retrieve a newly referenced part of the process, in which case it is moved to the waiting state. A running process may finally get enough CPU time to complete its processing and terminate normally; otherwise, it may generate an unrecoverable error and terminate abnormally.
When a waiting process gets the resources it is waiting for, it moves to the ready state again.

#### The process control block

The operating system must manage a large amount of data for each active process. Usually those data are stored in a data structured called a process control block (PCB). Generally, each state is represented by a list of PCBs, one for each process in that state. When a process moves from one state to another, its corresponding PCB moves from one state list to another in the operating system. A new PCB is created when a process is first created (the new state) and persists until the process terminates.

#### Interrupts

6.1.7 Outline OS resource management techniques: scheduling, policies, multitasking, virtual memory, paging, interrupt, polling.

#### Limitations

6.1.3 Identify the limitations of a range of resources in a specified computer system

##### Consequences

6.1.4 Describe the possible problems resulting from the limitations of the resources in a computer system.

### CPU scheduling

6.1.7 Outline OS resource management techniques: scheduling, policies, multitasking, virtual memory, paging, interrupt, polling.
Related to the ideas of memory management and process management is the need for CPU scheduling, which determines which process in memory is executed by the CPU at any given point. The problem of how to capitalise on computers' greater capabilities and speed has led to the concept of timesharing. A timesharing system allows multiple users to interact with a computer at the same time. Multiprogramming allowed multiple processes to be active at once, which gave rise to the ability for programmers to interact with the computer system directly, while still sharing it resources, whereas timesharing systems create the illusion that each user has exclusive access to the computer. That is, each user does not have to actively compete for resources, though that is exactly what is happening behind the scenes. One user may actually know they are sharing the machine with other users, but does not have to do anything special to allow for it. The operating system manages the sharing of the resources, including the CPU, behind the scenes.
CPU scheduling is the act of determining which process in the ready state should be moved to the running state. That is, CPU scheduling algorithms decide which process should be given over the CPU so that it can make computational progress.
CPU scheduling decisions are made when a process switches from the running state to the waiting state, or when a program terminates. This type of CPU scheduling is called nonpreemptive scheduling, because the need for a new CPU process is the result of the activity of the current executing process.
CPU scheduling decisions may also be made when a process moves from the running state to the ready state or when a process moves from the waiting state to the ready state. These are examples of preemptive scheduling, because the currently running process (through not fault of its own) is preempted by the operating system.
Scheduling algorithms are often evaluated using particular metrics, such as the turnaround time for a process. This is the amount of time between the time a process arrives in the ready state to the time it exits the running state for the last time. One would like, on average, for the turnaround time for a processes to be as small as possible.
There are various approaches that can be used to determine which process gets chosen first to move from the ready state to the running state. Here there is an examination of three:

#### First-come, first-served

In first-come, first-served (FCFS) scheduling approach, processes are moved to the CPU in the order in which they arrive in the running state. FCFS scheduling is nonpreemptive. Once a process is given access to the CPU, it keeps it unless it makes a request that forces it to wait, such as request that forces it to wait, such as a request for a device in use by another process.

#### Shortest job next

The shortest-job-next {SJN} CPU scheduling algorithm looks at all processes in the ready state and dispatches the one with the smallest service time. Like FCFS, it is generally implemented as a nonpreemptive algorithm.

#### Round Robin

6.1.5 Explain the role of the operating system in terms of managing memory, peripherals and hardware interfaces.
Round-robin CPU scheduling distributes the processing time equitably among all ready processes. The algorithm establishes a particular time slice (or time quantum), which is the amount of time each process receives before being preempted and return to the ready state to allow another process its turn. Eventually the preempted process will be given another time slice on the CPU. This procedure continues until the process eventually gets all the time it needs and terminates.
Note that the round-robin algorithm is preemptive. The expiration of a time slice is an arbitrary reason to move a process from the CPU. This action is presented by the transition from the running state to the ready state.

#### Limitations

6.1.3 Identify the limitations of a range of resources in a specified computer system

##### Consequences

6.1.4 Describe the possible problems resulting from the limitations of the resources in a computer system.

### Network connectivity and bandwidth management

6.1.7 Outline OS resource management techniques: scheduling, policies, multitasking, virtual memory, paging, interrupt, polling.
Operating systems must also take into account the fact that computers are usually connected to networks. Today with the World Wide Web one would take network communication for granted, however, the vastly connected and interactive world requires management on the data level. Operating systems will be able to process the inputs and outputs to and from the physical computer - usually through a network card of sorts. This communication between the network card and the computer is usually managed through a Network Interface Controller (NIC), a small component that handles two of the OSI layers; the physical and data link layer.

#### Limitations

6.1.3 Identify the limitations of a range of resources in a specified computer system

##### Consequences

6.1.4 Describe the possible problems resulting from the limitations of the resources in a computer system.

### Sound and graphic management

Along with other peripherals, the most fundamental aspect which operating systems must deal with is that of graphics and graphic management. Computer graphics can be very generally described as the setting of pixel values on the computer screen. As such, computer graphics play a role in many aspects in computer science, the most common application being the graphical user interface (GUI) of the modern operating systems. Files and folders are represented as icons on the screen, with the icon indicating the file type, for instance. The operating system must account for the whereabouts and calculation of computer graphics, and is typically done using its own individual Graphics Processing Unit (GPU).

#### Limitations

6.1.3 Identify the limitations of a range of resources in a specified computer system

##### Consequences

6.1.4 Describe the possible problems resulting from the limitations of the resources in a computer system.

### Miscellaneous
#### Cache management

A HTTP Cookie is a type of cache.
Aside from the obvious, there are various other miscellaneous system concepts that require management, which may not be entirely obvious. One is that of cache. Cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere. If requested data is contained in the cache, this request can be served by simply reading the cache, which is comparatively faster. Otherwise, the data has to be recomputed or fetched from its original storage location, which is comparatively slower. Hence, the greater the number of requests that can be served from the cache, the faster the overall system performance becomes.

## The role of the operating system
### Operating system types

6.1.2 Evaluate the resources available in a variety of computer systems.

### Physical systems

An operating system is responsible for communicating with a variety of devices. Usually that communication is accomplished with the help of a device driver, a small program that "knows" the way a particular device expects to receive and deliver information. With device drivers, every operating system no longer needs to know about every device with which it might possible be expected to communicate in the future. It's another beautiful example of abstraction. An appropriate device driver often comes with new hardware, and the most up-to-date drivers can often be downloaded for free from the manufacturing company's website.

#### Dedicated operating systems

6.1.8 Discuss the advantages of producing a dedicated operating system for a device.
Mobile devices, such as smartphones and tablet computers, run operating systems that are tailored to their needs. The memory constraints and the smaller set of peripherals involved, for example, are different than those of a typical desktop or laptop computer. Apple's iPod Touch, iPhone, and iPad all run the iOS mobile operating system which is derived from Mac OS. The Android operating system, developed by Google, is an open source project as part of the Open Handset Alliance. It has become the most popular OS for a variety of mobile devices. Android and iOS dominate the current market for mobile operating systems, though there are other competitors.

### Hiding complexity

6.1.9 Outline how an operating system hides the complexity of the hardware from users and applications.
Modern computer hardware is incredibly complex. Luckily, the operating system hides this complexity through the use of abstraction and high-level APIs. For example, if an application wants to create a file in a particular location, it orders the OS to create that file. The program doesn't need to be concerned with what filesystem the disk is running (FAT, NTFS, etc), which disk it is, whether it's a network server or local drive. The OS hides all these details. Other examples include virtualizing real devices, such as drive letters, virtual memory, input devices, the Java virtual machine.