Regarding MMAP, MLOCK etc..

Hi
I want to lock or prevent a portion of memory which I allocated. So I tried MLOCK, MPROTECT and some like this. But all these functions works only on page border. Can I know why that so.

Is that possible to protect a portion of memory which is in middle of the page.

Example.
int A[10];

Here I want to protect this memory from writing or reading?

Because that's how virtual memory works.

All those permissions take memory too, and have to be stored somewhere. Imagine how much memory would be required to remember access permissions for each individual byte of memory on your system! They had to simplify it somewhere, and pages is how they did that. Cut memory into larger, more manageable chunks.

If you need to individually protect parts, why not keep pointers to pages in your structure?

struct blarg
{
  int a, b;
  int *A;
  int c;
};

int main(void)
{
        struct  blarg g;
        g.a=0;
        g.b=1;
        posix_memalign(&(g.a), getpagesize(), getpagesize());
        g.A[0]=32;
        // Make the memory read-only
        mprotect(g.A, getpagesize(), PROT_READ);
}

No.

By the way: It's against the rules to spam multiple topics of the same question rephrased across different forums. If you're not answered immediately, wait -- we're not "on call".

P.S. It's possible to cause a bus error in the middle of a page by mapping in a file that's shorter than a page, but you only get errors trying to read/write past EOF, nowhere else.