Address Spaces, Explained | Introduction to Operating Systems

UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 4.6 Free with a free account

Address Spaces

Introduction to Operating Systems · Chapter 4: Memory ManagementSection 4.6 · lesson 30 of 838 min · free with a free account

← Why Memory Management MattersNext lesson: Virtual Memory →

A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →

Earn 3 college credits for Introduction to Operating SystemsRecommended for credit by NCCRS · Computer Science 354 · self-paced · one payment, lifetime access

In this lesson

How base and limit registers translate and protect program addresses.

  • Distinguish logical addresses from physical addresses
  • Translate a logical address with base and limit registers and spot a trap
  • Compare address binding at compile time, load time and run time
  • Calculate field and element offsets that stay the same from run to run

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (1,075 words). Read along, or search it.

Opening Analogy

House Numbers Need a Street

Every house on a street has a number. But number 12 means nothing, until you know which street. A program is in the same position. It thinks its first byte of memory is at address 0. Yet in real RAM, it might sit at 30,000, or at 60,000. Something has to translate between the numbers a program uses and where its bytes really are. And that same translation keeps programs apart.

Definitions

Physical and Logical Addresses

RAM chips hold bytes in numbered places. A place's number is its physical address: the address the memory hardware actually uses. Programs don't use physical addresses. Each one uses logical addresses, also called virtual addresses. They start at 0 and run up to the program's size. The whole range of logical addresses a program may use is its address space. It's the program's private street, numbered from 0, however the town is laid out.

Why Split Them

The Memory Management Unit Translates

Why split them? Because now the OS can put a program wherever there's room. And it can run several programs that all believe they start at 0. The translation from logical to physical is done by hardware: the memory management unit, or MMU. The operating system sets it up.

Base and Limit

Check the Limit, Then Add the Base

The simplest scheme uses two hardware registers, set for the program that's running. The base register holds the physical address where the program's space begins. The limit register holds the size of that space: how many addresses are allowed. On every memory access, the hardware does two steps. First comes the protection check: is the logical address below the limit? If not, the hardware raises a trap, as in lesson 2.3. The OS takes over, and usually ends the program. If the check passes, step two is translation: the physical address is the base, plus the logical address.

Worked Example

A Shop Program at Base 30,000

Try it. Sam, who runs a small online bakery, has a shop program loaded at base 30,000, with a limit of 12,000. Logical 500 passes the check, and becomes physical 30,500. Logical 11,999 is the last legal address. It lands at physical 41,999. Logical 12,000 is already out of range, so the hardware traps. So does 15,000.

Protection

Each Program Walled Into Its Range

Ivy, who looks after the computers at a school, runs an editor on the same machine. Its base is 50,000, and its limit is 5,000. The editor's logical 4,999 becomes physical 54,999. But a stray logical 20,000 is not below 5,000. It traps before it can touch any memory outside the editor's own range. Each program is walled into its own range.

Relocation

Change Only the Base Register

Now load the shop program at 60,000 instead. Logical 500 becomes physical 60,500. Nothing in the program changed. Only the base register did. That's the big advantage. A common mistake is to treat the limit as the highest physical address. It's a size, and it's compared with the logical address. With a limit of 12,000, the legal logical addresses are 0 to 11,999. So 12,000 is already outside.

Quick Check

Logical 6,500 With Base 42,000

Quick check. A program has base 42,000 and limit 8,000. It reads logical address 6,500. What happens? Does it read physical 35,500? Does the hardware trap? Or does it read physical 48,500? Pause, and decide. Physical 48,500. 6,500 is below the limit of 8,000, so the check passes. Then base plus logical: 42,000 plus 6,500 is 48,500. 35,500 subtracts instead of adding. And a trap would need a logical address of 8,000 or more.

Address Binding

Compile, Load or Run Time

When are addresses fixed? Source code uses names, like total, not numbers. Address binding is the step that ties each name to a real address. It can happen at compile time. If the compiler knows where the program will sit, it writes absolute physical addresses into the code. That's fast, but rigid: to move the program, you have to compile it again. It can happen at load time. The compiler writes relocatable code, counted from the program's start, and the loader adds the starting address. Once running, the program can't move. Or it can happen at run time. The code keeps logical addresses all the way, and the MMU translates every access, as base and limit do. The OS can even move a program while it runs. Modern systems work this way.

Offsets

Distances Stay Fixed Across Runs

Because the OS chooses where a program lands, its raw addresses change from one run to the next. Modern systems also shuffle them on purpose, a security feature called ASLR, in lesson 9.7. What stays the same are distances inside the program's own space. They're called offsets, and the compiler fixes them. Compiled and run on a Mac, a small C program reports that an int takes 4 bytes. So element 3 of an int array sits 12 bytes after element 0, wherever the array lands.

Code Example

Struct Fields and Padding

The same program has a struct for one order: a 1-byte tag, a 4-byte count, and an 8-byte price. It reports them at offsets 0, 4 and 8, and the whole struct takes 16 bytes. The compiler leaves a small gap after the tag, so the fields line up. So when you debug, print the differences between addresses, not the addresses. Differences stay the same when the OS moves the program.

Quick Check

Finding a Price in an Array

Quick check. That program keeps an array of these 16-byte orders. Call the spot where the array starts A, since it changes every run. Counting from 0, where does the price of the order at index 2 sit? A plus 34, A plus 40, or A plus 10? Pause, and decide. A plus 40. Orders 0 and 1 come first, 16 bytes each, gap included, so order 2 starts 32 bytes in. Its price is 8 bytes further: 40. A plus 34 forgets the gap, as if each order took 13 bytes. A plus 10 forgets the orders that come before it.

Real Case

Why Addresses Change Each Run

Later, Sam asked Ivy why the same program showed a different address on every run. It lands in a different place each time, she said. Your program only knows distances from its own start. The OS and the MMU handle where that start really is. Like house numbers, said Sam. I don't need to know where the street is in town.

Key Takeaways

Translate, Protect and Bind

To recap. A logical address is the program's own number, counted from 0. A physical address is the real place in RAM. Base and limit translate it: check that the address is below the limit, then add the base. Anything else traps. Binding can happen at compile, load or run time, and modern systems bind at run time. And raw addresses move from run to run, while offsets stay put.

About Introduction to Operating Systems at UPI Study

Introduction to Operating Systems (Computer Science 354) is a self-paced online course of 83 video lessons in 10 chapters, from what an operating system does and process scheduling to concurrency, memory, devices, file systems, networks, the internet, security and virtual machines. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: Linux vs Operating Systems

Why take it here

Why students take Introduction to Operating Systems with UPI Study

What happens under every app you runIntroduction to Operating Systems explains the software that runs a computer: processes and scheduling, threads and synchronization, deadlock, memory and paging, devices and storage, file systems, networking, security and virtualization. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Introduction to Operating Systems carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About Address Spaces

What is the difference between a logical address and a physical address?
A logical address is the number a program uses, counted from 0 within its own address space, while a physical address is the real location in RAM. The memory management unit, set up by the operating system, translates logical addresses to physical ones on every access. This lets the OS place a program anywhere in memory while the program behaves as if it starts at address 0.
How do base and limit registers work?
Base and limit registers translate and protect each memory access in two steps. First, the hardware checks that the logical address is below the limit; if it is not, the hardware traps to the operating system. Second, it adds the base to form the physical address. With base 30,000 and limit 12,000, logical 500 becomes physical 30,500, while logical 12,000 traps.
What are the three types of address binding?
Address binding can happen at compile time, load time or run time. Compile-time binding writes absolute physical addresses into the code, so moving the program means compiling it again. Load-time binding uses relocatable code, and the loader adds the starting address, but the program cannot move once running. Run-time binding keeps logical addresses and lets the MMU translate each access, so the OS can move a running program.
Is the limit register the highest physical address a program can use?
No, the limit register holds the size of the address space, and it is compared with the logical address, not a physical one. With a limit of 12,000, the legal logical addresses run from 0 to 11,999, so logical 12,000 is already out of range and traps. Treating the limit as a highest physical address is a common mistake in base-and-limit problems.

About the Introduction to Operating Systems course

How many college credits is Introduction to Operating Systems?
Introduction to Operating Systems (Computer Science 354) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Introduction to Operating Systems cost and what is included?
$250, paid once, with lifetime access: all 83 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Introduction to Operating Systems take?
It is self-paced with no deadlines. The 83 lessons total about 10 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Introduction to Operating Systems.

83 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Updated

Introduction to Operating Systems
3 credits · NCCRS
Enroll →