5
$\begingroup$

I was studying operating system concepts from Silberschatz, Galvin and Gagne's book (sixth edition) and I have some questions about the flow of execution of a program. A figure explains the processing of the user program:

program flow diagram

We get an executable binary file when we reach the linkage editor point. As the book says,

The program must be brought into memory and placed within a process for it to be executed.

I have several questions about this flow:

  1. Before the program is loaded into the memory, the binary executable file generated by the linkage editor is stored in the hard disk. The address where the binary executable file is stored in the hard disk is the logical address as generated by the CPU ?

  2. If the previous answer is yes, Why CPU has to generate the logical address ? I mean the executable file is stored somewhere in the hard disk which pertains to an address, why does CPU has to separately do the stuff ? CPU's main aim is processing after all!

  3. Why does the executable file needs to be in the physical memory i.e ram and can not be executed in the hard disk? Is it due to speed issues ?

$\endgroup$
4
$\begingroup$

First of all, you are fundamentally confusing the purpose of RAM and hard disk. RAM is the only part of memory that CPU can access. Hard disk exists to expand the capacity of RAM as well as store the data permanently (data on RAM is transient).

How it works -

  1. The computer generates an executable and stores it in the hard disk. It is just a passive program right now.
  2. Another process (already in memory and currently running) calls the program you just created using the access path (full psth + name) in the filesystem. This is a system call; an interrupt is issued and control is transferred to OS.
  3. OS decodes the path to get the actual location(s) on hard disk where the program data blocks are stored. Hard disk is treated as any other I/O device; it vouches for CPU time like any other I/O device. Here, OS has two choices:
    • either it needs to fetch data from hard disk each time hard disk has some to give (hard disk announces it by issuing interrupts) and store it in RAM. But with ever faster hard disk speeds nowadays, frequency of interrupts can be high enough to be inefficient for CPU.
    • second option is that CPU, while issuing the read command for program data blocks, also specifies the RAM memory addresses to which hard disk should directly copy its data (Direct Memory Access). Since CPU, I/O devices (including hard disk) and memory all share common bus, data can be transferred one at a time. Moreover, CPU meanwhile also has other processes (in RAM) to run while data is being copied by hard disk to RAM. Thus, both CPU and hard disk take turns when copying data to RAM. Here, DMA would be faster if CPU is idle or requires less memory accesses. When hard disk has completed writing data to RAM as specified by CPU, it issues interrupt to CPU to tell it that it has completed its task.
  4. Take note above that CPU has never accessed the data of hard disk directly. The hard disk works the same way as any other device like keyboard or modem would work (via interrupts etc). Now after hard disk has copied data and interrupt generated, OS assigns stack memory and other resources for this newly created process and inserts it into the queue of active processes.

That is why when creating small beginner programs, we can never save the state of programs. We create some variables while the process is running and modify them, but never explicitly store them on the hard disk (unless engaging with the file handling aspect). When we destroy the program all the memory is lost. and all the read, write operations of a language are merely wrappers around the system calls provided by the OS so as to make the code portable across several operating systems (since system calls of each are different). These read/write/open/close system calls are requests for OS to let us access a file on hard disk, different from the RAM.

As to your question about logical addresses, it is a little ambiguous as to what you want to say. Logical addresses are used in reference to RAM by programs, and usually refer to the program's view of its memory. For a program, its logical addresses may start from 0, but that doesn't translate into the fact that all programs start at physical address 0 of RAM! You are right that CPU has to process data, but it needs some data at all to process, with is stored in RAM. The addresses provided by program code are logical (say, get data stored in location '7 + start of program') and CPU has to calculate its actual physical address to work upon (i.e. replace 'start of program' with the base address of process code blocks in RAM).

Thus, logical addresses have nothing to do with hard disks.

$\endgroup$
  • $\begingroup$ thanks a lot for the answer.. Things are much simple now .. I guess as far as logical addresses are concerned.. they are literally " logical " and required because the physical memory is less and code and data pertaining to a particular program need to be accessed by the ram .. this requires that every data is stored logically in the form of "pages"! And then be brought to the memory when required. I guess i am correct!? $\endgroup$ – Chandeep Oct 20 '12 at 16:45
  • $\begingroup$ Exactly. Yet the concept of a logical address is wider than that. It is simply an address wrt a computer program. OS provides the actual mapping between logical and physical addresses. What this mapping would be depends on how OS manages it. It could be as simple as what I told in my answer (simply add a base address to the logical address) or as complex as the paging you mentioned. Here the program view of memory is invariant, and is wholly unrelated to the mapping to physical addresses which is decided by the OS. $\endgroup$ – Anurag Kalia Oct 21 '12 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.