Link to home
Start Free TrialLog in
Avatar of stauffec
stauffec

asked on

Sharing between different processes using driver

I've created a memory module that gets installed as a driver. I create a node for this module on the filesystem. Some applications open the node and write to it, while others read from it. Whenever a process writes to it, the module stores it in kernel memory, however when another process goes to read, it doesn't appear to see the same memory that was written, as if it has a separate instance of the module. How can I make it so it sees the same memory?

Code Example for the module:

char * data;

init_module
{
  data =(char *) kmalloc(100,GFP_KERNEL);
}

read(char * buf,.....)
{
  copy_to_user(buf,data,100);
}


So if "init_module" is called during the insert the module, it creates the memory space for the global variable "data", but then when process A  tries to call read, it gets a segmentation fault, as if the memory had never been allocated. Can anyone explain this?
Avatar of scn
scn

Is the user space variable buf correctly allocated by the process before the call to read?
Avatar of stauffec

ASKER

In the code snippet up top, the initialization of the module (insmod) causes the allocation of the memory and it is successfully freed at the removal of the module (rmmod). HOwever, any other process opening the driver and trying to read or write to it seems to get a segmentation fault, as if the memory wasn't allocated. What i'm not sure of is if that allocation of memory done on the insmod call is still visible when another process tries to call read, which is supposed to read data from that memory. If it cannot see it, how can I have a module that can allow one process to write into a memory space and then have another process read from that same kernel memory. I thought the module was the bridge for that.
kmalloc allocates memory into the kernel space so, while inside the module read function, any process can read this memory. Then, copy_to_user(buf,data,100) copies the "data" variable (belonging to kernel memory area) content to the buf variable (belonging to the process memory area) asuming that buf has already been allocated by the calling process.
OK, I understand that kmalloc allocates memory in the kernel, however, is the variable char pointer "data" that is assigned the memory allocation during module initilization the same variable that will be seen by another process when it calls the read function, or when the process opens the node and calls read, is a new variable "data" instantiated, for the process?
ASKER CERTIFIED SOLUTION
Avatar of scn
scn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Have you tried adding a printk after the memory is allocated to make sure that your init function is getting called? I have done what you are trying to do and had no problems.

Rubini has an example that I started with in his book on device drivers: http://www.xml.com/ldd/chapter/book/

and the examples from the book: http://examples.oreilly.com/linuxdrive2/