Basic Operating System services and structures.
Tags: computerscience, explainlikeimfive
Published: 2019-05-27 07:52:32

Operating System(OS) provides an interface to the user in order to communicate with the hardware.

OS has mainly three functions:-
1. To provide services
2. System calls
4. User Interface

####**Services**

Services like file maintenance, I/O, resource allocation, communication etc. These services are done with the help of system calls.

####**System calls**

System calls provide an interface for the services provided by the OS. Few of the system calls are:
* Process control - Any program loaded into memory and ready for execution is known as __process__. OS provides control on these processes like reading inputs, error detection etc.
* File manipulation - Reading, writing, deleting file etc.
* Device manipulation - Reading from devices, Setting appropriate parameters in the memory to write to devices etc.
* Information manipulation - Transferring information between user program and OS such as _time()_ , _date()_ etc.
* Communication - Setting up communication protocol for passing of data between devices. This can be done in 2 ways : **message passing model** and **shared memory model**. In the former model the message is passed between devices using _hostid()_ and _clientid_. In the latter model, data is placed in shared memory. Message sharing model is mainly used for smaller amount of data as it takes more time to communicate than the shared memory model as in the latter model the data is shared at the speed of memory transfer. 
* Protection & security - Protecting system from malicious softwares, errors etc.

####**User and OS interface**

User Interface are basically of 3 types: **command line UI**(eg. UNIX), **Batch**, **GUI**(eg. Windows)

OS interface consists of the programs which are used to read the user command and act accordingly. This is done through command interpreter. There are 2 methods through which command interpreter can work:
1. The command interpreter itself contains the code and sets the parameter accordingly.
Eg:- A command to delete a file may cause the command interpreter to jump to a section of its own code that sets up parameter for deleting and then make appropriate system calls.
2. Command interpreter uses the system calls to get any work done.
Eg:- Lets say a command `rm file.txt` is given.
The interpreter doesn't understand the code in any way. It will search for the file named `rm` and load the file into memory, and execute it with the parameter `file.txt`.

####**OS structure**

1.Simple structure - DOS has this type of structure.

Structure of DOS: 
![alt text](http://faculty.salina.k-state.edu/tim/ossg/_images/dos_struct.png)

In DOS, user application may bypass the OS and write into ROM itself which is more prone to errors.

2.Layered approach - The OS services are modeled into layers with hardware as the lowest layer and user interface as the topmost layer.

Layered structure: 
![alt text](https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/images/Chapter2/2_13_Laered_OS.jpg)

Here, one thing is to note that the top layer can communicate with lower layers only. So, layer planning has to be done properly. Also, debugging is more easy in this structure as debugging is done layer by layer and the exact location of problem can be ascertained.

3.Microkernel - Kernel is made smaller to make the OS fast to reboot and avoid conflicts. Microkernel is written on ROM so that it cant be modified. Only the most important system services and calls are embedded into the microkernel and the rest of the system calls are written and loaded onto the disk.

4.Modules - In the microkernel approach, there is drawback of system calls being present in the disk which is more volatile as user application are also present there and thus prone to be modified and susceptible to fatal error. To get around this, system calls are written onto disk and in form of modules. These modules when needed are loaded with the kernel which is written in EPROM(Erasable programmable ROM) instead of ROM so that it can be modified when necessary.

####**OS debugging**

Whenever an error is encountered, OS writes error information into a **log file** which can be later used to debug. OS can also take a **core dump** - a capture of the memory of the process- and store into a file for later analysis. A failure in kernel is called **crash**. When crash occurs, error information is saved to a log file, and the memory state is saved to a **crash dump**.

**Performance tuning** also forms a part of debugging which is done through many tools such as Windows task manager, DTrace etc.

####**System boot**

The process of starting a computer by loading the kernel is knows as **booting** the system. On most computer, a small piece of code known as the **bootstrap loader** locates the kernel, loads it into the memory, and starts its execution.
At this point the system is said to be **running**.