October 22, 2024
Chicago 12, Melborne City, USA
C#

Understanding virtual memory via background jobs


Understanding virtual memory in Operating Systems: Concepts and Implementation

Unexpected behavior when running multiple instances of a memory allocation program.

I’m learning about operating systems and following the OSTEP (Operating Systems: Three Easy Pieces) textbook. I’m trying to understand memory virtualization by running multiple instances of a program simultaneously. Here’s the code I’m working with:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"

int main(int argc, char *argv[]) {
    int *p = malloc(sizeof(int)); // a1
    assert(p != NULL);
    printf("(%d) address pointed to by p: %p\n", getpid(), p); // a2
    *p = 0; // a3
    while (1) {
        Spin(1);
        *p = *p + 1;
        printf("(%d) p: %d\n", getpid(), *p); // a4
    }
    return 0;
}

According to the textbook, when run with the command ./mem &; ./mem &, this program should demonstrate memory virtualization by running two instances simultaneously. The text states:

We see from the example that each running program has allocated memory at the same address (0x200000), and yet each seems to be updating the value at 0x200000 independently! It is as if each running program has its own private memory, instead of sharing the same physical memory with other running programs. Indeed, that is exactly what is happening here as the OS is virtualizing memory.

However, when I run this program locally, I get the following output:

PS C:\Users\malan\Desktop\os\Virtualizing_the_Cpu> ./mem &; ./mem

Id   Name             PSJobTypeName   State
--   ----             -------------   -----
13   Job13            BackgroundJob   Running
(8776) address pointed to by p: 007B1990
(8776) p:1
(8776) p:2
(8776) p:3
(8776) p:4
(8776) p:5
(8776) p:6
(8776) p:7
(8776) p:8
(8776) p:9
(8776) p:10
(8776) p:11
(8776) p:12
(8776) p:13

The two instances aren’t running simultaneously as expected. What could be causing this behavior? Am I missing something in my setup or understanding of how this should work?

Additional information:

  • Operating System: Microsoft Windows 11 Home Single Language
  • Operating System Version:10.0.22631 N/A Build 22631
  • Compiler: gcc (MinGW.org GCC-6.3.0-1) 6.3.0



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video