A program is a sequence of machine-language instructions stored in a file. Running a program means loading that list of machine-language instructions into memory and having the CPU execute the instructions, one by one.

Let's spend 1 minute to find out what is a process and how it is loaded into the memory.

A process is the memory space and settings with which the program runs.

The column marked S shows the status of each process. R is running, S is sleeping. WCHAN shows why a process is sleeping.

A process has a size, shown in the SZ column. This numberrepresents the amount of memory the process is using. The size of a process change as it runs. A program that allocates memory when it runs can get larger.Memory in a Unix system is divided into kernel space and user space. Processes live in user space.

Columns marked PRI and NI contain priority and nicesness levels of the process. The kernel uses these values to decide when to run processes or the priority of the processes.

TTY shows which terminal the process is connected to. If no tty, the process is started at startup.

Among the above processes, there are two instances of program "./multi", one runs under user "shan" and another runs under "root". The one under "root" pid 16749's nice value is "-20" and priority is "60", which has much higher priority than the one runs under "shan". That's because the root started multi with nice value of -20. You can use command "renice" to adjust the nice value of a process.#nice -n -20 ./multi

The commands "ps ax" or "ps lax" (in linux) will show you all the running system processes, you will realize that a lot of processes don't have terminal ( TTY column marked ?) because they are started when the system starts up not typed by a user at command line.

C. The shell loads the program from the disk into the process;(done by execvp( const *prog, const char *argv[])

D. The program runs in its process until it is done.

Note: The kernel loads the program from the disk into CURRENT process, replacing the coe and data of current process.

In the following code, the message "ls is done. bye." will NOT be displayed, because it was erased by execvp.

The exec system call clears out the current program code from the current process, then, in the now empty process, puts the code of the program named in the exec call, and then runs that new program. exec also changes the memory allocation of the process to fit the space requirement of the new program. Note, the following code printf("* * * ls is done. bye\n") will not be executed./* exec1.c */

1. Text Segment.This is the machine instructions that are executed by CPU.

2. Initialized data segment.It contains variables that are specifically initialized in the program.3. Uninitialized data segment or "bss" (block started by symbol).Data in this segment is initialized by the kernel (0 or null pointers) before the program starts executing. e.g. C declaration long sum[2000];4. Stack.This is where automatic variables are stored, along with information that is saved each time a function is called.

Now, let's see how process (a running program) is loaded into the memory:

* In Linux processes loaded from a file system (using either the execve() or spawn() system calls) are in ELF format.

* If the file system is on a block-oriented device, the code and data are loaded into main memory.

* If the file system is memory mapped (e.g. ROM/Flash image), the code needn't be loaded into RAM, but may be executed in place.

* This approach makes all RAM available for data and stack, leaving the code in ROM or Flash. In all cases, if the same process is loaded more than once, its code will be shared.

* Before we can run an executable, firstly we have to load it into memory.

* This is done by the loader, which is generally part of the operating system. The loader does the following things (from other things):

1. Memory and access validation - Firstly, the OS system kernel reads in the program file’s header information and does the validation for type, access permissions, memory requirement and its ability to run its instructions. It confirms that file is an executable image and calculates memory requirements.

2. Process setup includes:

1) Allocates primary memory for the program's execution.2) Copies address space from secondary to primary memory.3) Copies the .text and .data sections from the executable into primary memory.4) Copies program arguments (e.g., command line arguments) onto the stack.5) Initializes registers: sets the esp (stack pointer) to point to top of stack, clears the rest.6) Jumps to start routine, which: copies main()'s arguments off of the stack, and jumps to main().

* Address space is memory space that contains program code, stack, and data segments or in other word, all data the program uses as it runs.

* The memory layout, consists of three segments (text, data, and stack), see the attached figure.

* The dynamic data segment is also referred to as the heap, the place dynamically allocated memory (such as from malloc() and new) comes from. Dynamically allocated memory is memory allocated at run time instead of compile/link time.

* This organization enables any division of the dynamically allocated memory between the heap (explicitly) and the stack (implicitly). This explains why the stack grows downward and heap grows upward.

Below is a multithreaded C code, let's compile it and dump the object file content using readelf program.