Language: EN

programacion-ciclo-vida-programa

Program Lifecycle

The lifecycle, or execution cycle, refers to the different stages a program goes through from its creation to its completion. All of this process is what we call the program’s execution cycle.

The lifecycle can be very simple, as in a console application,

ciclo-vida-programa

In this case, when we launch a program it:

  • Starts
  • Does things for a while
  • Closes

But it can become much more complex as in the case of a mobile device application, or a game that runs on a game engine.

For example, the lifecycle of an Android application looks like this.

ciclo-vida-android

Which as we can see, is no longer as simple as starting, executing, and closing. There are different ways to end a program, it can even be suspended and reactivated later.

The role of the operating system

During the lifecycle of a program, the operating system (OS) plays a fundamental role in each stage. When I say OS we should think “whatever we are using to run our program”.

On simpler machines, such as an Arduino, the “OS” is simply a bootloader that launches the program we have executed. Other more complex machines use lightweight operating systems such as FreeRTOS.

If we talk about computers or mobile devices, we will have OSs such as Windows, or Linux, or Android, (or whatever). Even if we are running a game engine, this will be an additional layer of software between our program and the hardware.

In any case, we see that our program will run “on top” of several layers of software. The subject is quite complex and we will not go into further details today.

But it is important that you know when your program’s execution life begins, how it runs, and when it ends.

Entry Point

The lifecycle of an application begins at the entry point, which is the starting point of the program’s execution.

At this stage, the OS is responsible for loading the program into memory and allocating the resources necessary for its execution. The OS also establishes the execution environment and provides the command-line arguments passed to the program, such as input parameters.

In many programming languages, like C++ or Java, the entry point is usually a function or method with a specific name, which indicates where the code should start executing.

If we take an example in C++, we see that it has a main() method as the entry point.

#include <iostream>

int main() {
    // here is the code that will run at the beginning
}

This would be very similar in C# or in Java, for example. In these languages, the program’s entry point is indicated with the main() method.

It is usually possible to change the name of the entry point in the project’s configuration or as options when compiling

In the case of interpreted (or semi-interpreted) languages, in addition to the OS we have an additional layer of software which is the language interpreter. Therefore, normally, in interpreted languages the program’s entry point is simply the first line of the program itself.

For example, in JavaScript it is enough to do,

console.log("Hello, world!");

No further indications are necessary because the interpreter is ready to run the code as it is.

Code Execution

Once the program has been loaded into memory and the execution environment has been established, the code execution is carried out. The program follows the instructions and performs the operations defined in the source code.

During this stage, the OS is responsible for managing the runtime, allocate additional resources as needed, and ensure a safe and secure execution environment.

The OS also provides access to various system functionalities and services, such as communication with input/output devices, file management, and interaction with other running programs. It also monitors the program for possible errors or abnormal behaviors that may affect the system’s stability.

Finally, in the case of interpreted languages, we also have the interpreter doing its tasks, which usually include memory management, or inputs and outputs.

Exit Code

At some point the program will stop running. This can be either because the machine where it runs is turned off or restarted, or because the program ends. Let’s ignore the part about turning off the machine, because it is not really a controlled “termination” of a program.

When a program finishes, it informs the OS that it has no more pending tasks to perform, and that it has completed its function. To do this, it produces an exit code that indicates the state in which the program ended.

The exit code is returned to the OS and can be used by other programs or scripts to make decisions or take actions based on the execution result.

The exit code generally has an integer value, where a value of 0 indicates that the program ran correctly without errors. Other non-null values may indicate different types of errors or exceptional conditions, and their interpretation may vary depending on the program and the operating system.

After the program ends, with the exit code, the OS can take appropriate actions. For example, release the memory that it had reserved for the process, or record if an error occurred during execution.

In interpreted languages, the operating OS is generally not informed that the program has finished. Because, actually, the OS is running the interpreter, not the program. So the process’s termination management is performed by the interpreter itself, but it continues to run.