Unit 2 - Execution of Program, Interpreters and OS

 CBSE Revision Notes

Class-11 Computer Science (New Syllabus)
Unit 2: Computer Systems and Organisation (CSO)


Execution of Program, Interpreters and OS

Execution of a program

After we write a program there is process it goes through before it gets executed different programming languages use different mechanism to execute the program like java converts source code into byte code that then executes on any machine which has Java virtual machine on that and that byte code is architecture independent. If we talk about C or C++ they use different mechanism to execute the program at the time of compilation source code is converted into binary form and then that program is executed on that machine and this process is dependent on architecture like byte code we can’t carry binary file on any machine and execute that file as architecture of every machine is different. Let’s see a general view of how program is executed and what happens at every step.

Step 1: COMPILATION

We write our programs/source codes in the language we understand. But the computer understands only binary language. The compiler is a program that converts our pre-processed source program into the language understood by the computer. Compiler also checks for syntax errors. After compilation, what we get is an object file which has data that computer can understand.

Step 2: LINKING

The object file is linked with library files. Library files contain definitions (complete working code) of library functions and objects that we are using in our program. The linker extracts these required definitions from the library file. The output of the linking phase is an executable file.

Step 3: EXECUTION

The executable file is a file with .exe extension. This is the final running version of the program (in language which the computer understands). When this file is executed, the output is produced.

Interpreter

An interpreter is a computer program that is used to directly execute program instructions written using one of the many high-level programming languages. The interpreter transforms the high-level program into an intermediate language that it then executes, or it could parse the high-level source code and then performs the commands directly, which is done line by line or statement by statement.
Programming languages are implemented in two ways: interpretation and compilation.

As the name suggests, an interpreter transforms or interprets a high-level programming code into code that can be understood by the machine (machine code) or into an intermediate language that can be easily executed as well. The interpreter reads each statement of code and then converts or executes it directly. In contrast, an assembler or a compiler converts a high-level source code into native (compiled) code that can be executed directly by the operating system.
In most cases, a compiler is more favourable since its output runs much faster compared to a line-by-line interpretation. However, since interpretation happens per line or statement, it can be stopped in the middle of execution to allow for either code modification or debugging. Both have their advantages and disadvantages and are not mutually exclusive; this means that they can be used in conjunction as most integrated development environments employ both compilation and translation for some high-level languages.

Difference between interpreter and compiler

InterpreterCompiler
Translates program one statement at a time.Scans the entire program and translates it as a whole into machine code.
It takes less amount of time to analyse the source code but the overall execution time is slower.It takes large amount of time to analyze the source code but the overall execution time is comparatively faster.
No intermediate object code is generated, hence are memory efficient.Generates intermediate object code which further requires linking, hence requires more memory.
Continues translating the program until the first error is met, in which case it stops. Hence debugging is easy.It generates the error message only after scanning the whole program. Hence debugging is comparatively hard.
Programming language like Python, Ruby use interpreters.Programming language like C, C++ use compilers.

Running a program (OS)

Let’s see how an operating system helps in running the application before that let’s discuss how an operating system works.

Operating system: When you turn on the power to a computer, the first program that runs is usually a set of instructions kept in the computer's read-only memory (ROM). This code examines the system hardware to make sure everything is functioning properly. This power-on self-test (POST) checks the CPU, memory, and basic input-output systems (BIOS) for errors and stores the result in a special memory location. Once the POST has successfully completed, the software loaded in ROM (sometimes called the BIOS or firmware) will begin to activate the computer's disk drives. In most modern computers, when the computer activates the hard disk drive, it finds the first piece of the operating system: the bootstrap loader. The bootstrap loader is a small program that has a single function: It loads the operating system into memory and allows it to begin operation. In the most basic form, the bootstrap loader sets up the small driver programs that interface with and control the various hardware subsystems of the computer. It sets up the divisions of memory that hold the operating system, user information and applications. It establishes the data structures that will hold the myriad signals, flags and semaphores that are used to communicate within and between the subsystems and applications of the computer. Then it turns control of the computer over to the operating system.

The operating system's tasks, in the most general sense, fall into 5 categories:

  1. Processor management: It ensures that each process and application receives enough of the processor's time to function properly using as many processor cycles as possible for real work. The basic unit of software that the operating system deals with in scheduling the work done by the processor is either a process or a thread, depending on the operating system.
  2. Memory management: The first task requires the operating system to set up memory boundaries for types of software and for individual applications. Each process must have enough memory in which to execute, and it can neither run into the memory space of another process nor be run into by another process. The different types of memory in the system must be used properly so that each process can run most effectively.
  3. Device management: The path between the operating system and virtually all hardware not on the computer's motherboard goes through a special program called a driver. Much of a driver's function is to be the translator between the electrical signals of the hardware subsystems and the high-level programming languages of the operating system and application programs. Drivers take data that the operating system has defined as a file and translate them into streams of bits placed in specific locations on storage devices, or a series of laser pulses in a printer. Because there are such wide differences in the hardware, there are differences in the way that the driver programs function. Most run when the device is required, and function much the same as any other process. The operating system will frequently assign high-priority blocks to drivers so that the hardware resource can be released and readied for further use as quickly as possible.
  4. Application interface: Just as drivers provide a way for applications to make use of hardware subsystems without having to know every detail of the hardware's operation, application program interfaces (APIs) let application programmers use functions of the computer and operating system without having to directly keep track of all the details in the CPU's operation.
  5. User interface: Just as the API provides a consistent way for applications to use the resources of the computer system, a user interface (UI) brings structure to the interaction between a user and the computer. In the last decade, almost all development in user interfaces has been in the area of the graphical user interface (GUI), with two models, Apple's Macintosh and Microsoft's Windows, receiving most of the attention and gaining most of the market share. The popular open-source Linux operating system also supports a graphical user interface. There are other user interfaces, some graphical and some not, for other operating systems.
    How an operating system runs a program: Operating system acts as an interface between program and hardware. Operating system is responsible to allocate memory to the program and the resources it needs to get executed. When we try to run a program operating system loads the program in the memory and then assigns CPU to it. OS is responsible to allocate the resources to the program for example program needs an input from the user or it needs to get data from somewhere it’s operating system which helps it in doing it. OS also create a process control block(PCB) for the program which keeps track of the instructions that are being executed PCB also keeps track of the memory location from where it needs to fetch the data for that program.