Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sharing between different processes using driver

Posted on 2004-08-17
6
Medium Priority
?
354 Views
Last Modified: 2008-03-17
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?
0
Comment
Question by:stauffec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:scn
ID: 11828102
Is the user space variable buf correctly allocated by the process before the call to read?
0
 

Author Comment

by:stauffec
ID: 11837384
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.
0
 
LVL 3

Expert Comment

by:scn
ID: 11838767
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.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:stauffec
ID: 11840023
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?
0
 
LVL 3

Accepted Solution

by:
scn earned 900 total points
ID: 11840392
data is the same variable. You can check it easily by putting some content to data in the init function and display it with a printk in read function. Below is a complete example:

kernel module
---------------
// includes ...
char *data;

static int my_open(struct inode *inode, struct file *file) {
      return 0;
}

static int my_release(struct inode *inode, struct file *file) {
      return 0;
}

static ssize_t my_read(struct file *file, char *buf, size_t count, loff_t *ppos){
      printk(KERN_INFO "Data values [%s]\n", data);
      copy_to_user(buf,data,100);
}

static struct file_operations my_fops = {
      owner:  THIS_MODULE,
      open:   my_open,
      release:my_release,
      read:   my_read,
};

static int __init my_init(void) {
      int ret;
  if (ret=register_chrdev(62, "/dev/mydrv", &my_fops)) {
    printk(KERN_ERR "Failed to register driver, errno %d\n", ret);
            return -1;
  }
      data = kmalloc(100, GFP_KERNEL);
      memcpy(data,"a string",9);
      return 0;
}

static void __exit my_exit(void) {
}

module_init(my_init);
module_exit(my_exit);


Process source:
-----------------
// includes ...
int main() {
      int fd,nb;
      char *buf;

      fd = open("/dev/mydrv", O_RDONLY);
      if (fd<0) {printf("OPEN error\n"); exit (0);}
      buf=(char*)malloc(100);
      nb=read(fd, buf, 100);
      if (nb<0) printf("Read error %d\n", errno);
      else      printf("Read [%s]\n", buf);
      close(fd);
}

Creation of the device node: mknod /dev/mydrv c 62 0

When executing ./u, the line "Data values [astring]" is added to /var/log/messages (if syslog configured for it) and the process displays on stdout "Read [astring]".
Hope that helps.

0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 11899271
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/

0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question