kernel memory char device

This is a discussion on kernel memory char device within the Linux Programming forums, part of the Platform Specific Boards category; I tried to ask this question here but it was kind of veiled (...so I just have to ask it ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Question kernel memory char device

    I tried to ask this question here but it was kind of veiled (...so I just have to ask it again).

    I'm learning device drivers, blah blah, and one basic exercise (google "linux scull") is to write a driver for a character device where the "device" is just a chunk of kernel memory (eg. you now have a 10 mb /dev/chunk which is universally accessable, could be locked, etc). Since the driver while loaded is part of the kernel, and the write operations to the device (eg, echo "some stuff" > /dev/chunk) are coded in the module, you could actually structure this as a kind of data server directly in the the "read" and "write" functions:
    Code:
    ssize_t write_onedev(struct file *file, const char __user *buf, size_t count, loff_t *pos) {
    	void *ptr=TOne.data+(long)*pos;
    	if (TBytes-((long)ptr-(long)TOne.data)<count) count=TBytes-((long)ptr-(long)TOne.data);
    	if (count<=0) { return -ECANCELED; }
    	printk("testone [write] pos=%ld count=%d\n",(long)*pos,(int)count);
    /* now instead of all this:
    	memcpy(ptr,(const void*)buf,count); 
    	*pos+=count;   // responsible for this
    	TOne.sz=*pos;	// sequential/linear 
    consider using the first byte as op code : */
            switch (buf[0]) {
                   case (66) :  serve some data
                   case (67) :  write incoming to specific area
           ...etc.
    	return count;
    }
    I don't really have a particular use for this right now, but I was wondering
    1) is this a good idea, or a bad idea and why.
    2) does anyone know of any actual implementations of this?

    Sincerely, Just Curious.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    The problem is that data exchange between processes is already well-understood with pipes and shared memory, and device control operations are well-understood with ioctl(). I don't think your approach is bad, but it's also gratuitously un-UNIX.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The reason I don't "have much of a use for it" is because I don't have a need to do any of the orthodox "shared memory" things that this might conceivably replace, and IPC to me is generally short messages easily done with sockets. So it's hard for me to judge whether it would really be useful or not.

    So thanks for your answer brewbuck, I think I see what you're saying.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21