cpowershelloperating-systemjobs

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:


Solution

  • Type it into bash and it will work.

    & means run in background in bash, but it means something else in powershell; I'm surprised you didn't get an error message.