'Can I call linux allocation API functions in C?
I want to allocate physical page frames using alloc_pages()
in C. I have tried that in kernel module in it works fine. I want to make the code running in user space, just how we execute c code normally. Below is the code:
#include <stdio.h>
#include <stdlib.h>
#include <linux/kernel.h>
#include <linux/module.h>
struct page *page1;
int main()
{
struct page *page;
page = alloc_pages(GFP_USER, 0);
printf("physical address: %p\n", page);
page1 = page;
return 0;
}
First it shows GFP_USER is undeclared and if I use any integer there instead of the flag, it shows:
undefined reference to `alloc_pages'
It is clear it can not find the functions and I am not very clear how to make it available to my code. How can I do that? Also I know I do not have enough knowledge in this so any reading material is appreciated.
Solution 1:[1]
User-space and kernel space on Unix systems are kept apart and system calls are the only way you can communicate with the kernel. There are as always some special case exception but this is the base-idea for Unix systems as Linux as a variant of.
You can allocate memory using libc malloc() and similiar calls, or you can use syscalls like mmap().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Stian Skjelstad |