Hi guys. Sorry for the late response. The code was in my laptop and I had to wait till I go home to fetch it. Its part of a bigger shared project. What I am trying to do here is trying to get a snapshot of what pages are currently in memory using the mincore linux system call. It takes in an char * argument along with size and starting page address. If I haven't already mentioned, the compilation went without any erros. But again I dont have the whole code as this is a shared project. My part is to get a snapshot of the application pages in memory.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/mman.h>
unsigned char *mincore_pages(unsigned long *addr, unsigned long bytes){
unsigned long length_of_vector = ((bytes /getpagesize()) + 1);
unsigned char *ptr = NULL; /* initialize to NULL */
if((ptr = malloc( (sizeof(unsigned char)) * length_of_vector )) == NULL){
fprintf(stderr, "Unable to allocate requested memory");
fprintf(stdout,"exiting now...");
exit(1);
}
else{
/* note that mincore function writes the page information in the address pointed to by ptr. 1 byte is used per page. the last bit of the byte is set if the page is in memory else it remains 0 . so I made sure that I allcated enough bytes using malloc to account for each page of my bytes size chunk using malloc() */
if((mincore(addr, bytes, ptr)) == -1){
fprintf(stderr, "mincore failed with error: %s", strerror(errno));
free(ptr);
exit(1);
}
}
return ptr;
}
so that is the file whose function gets called by my main() when mincore flag is set when invoking the main as a cmd line arg
Code:
/* my part of main() */
if (mincore){
unsigned char *ptr;
unsigned long *p = allocate_page_segment(bytes); /* defined in a file written by my fellow project mate. it returns a valid segment. I checked that using ipcs on my host system by stoping the execution at this line using gdb */
if(ptr = mincore_pages(p,bytes) == (void *) -1){
perror("mincore failed\n");
exit(1);
}
/* if successful, I just want to free the ptr for now. later, I will try to may be print it out or something */
free(ptr);
}