coperating-systemriscvxv6

What does madvise() do in virtual memory?


Following code was excuted in xv6(risc-v). I'm little confused. WHy we need to madvise() after malloc()? Is it for page table entry swaping in/out?

PS. vmprint() print the page table.

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/vm.h"

#define PG_SIZE 4096
#define NR_PG 16

int main(int argc, char *argv[]) {
  vmprint();
  char *ptr = malloc(NR_PG * PG_SIZE);
  vmprint();
  madvise((uint64) ptr + 10*PG_SIZE, 2*PG_SIZE , MADV_NORMAL);
  vmprint();
  madvise((uint64) ptr + 10*PG_SIZE, 2*PG_SIZE , MADV_DONTNEED);
  vmprint();
  madvise((uint64) ptr + 10*PG_SIZE, 2*PG_SIZE , MADV_WILLNEED);
  vmprint();
  exit(0);
}

Solution

  • After allocating this memory block by malloc() function, the madvise() function is used to advise the kernel about the usage of specific memory regions.