Re: adding semaphores to my device driver

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Sep 14, 2010 at 9:50 PM, Bond <jamesbond.2k.g@xxxxxxxxx> wrote:
> I have written a small character device driver.
> I want to add a semaphore to it.
> Any idea as what should I be doing.
> How I have implemented my code is
> I defined four functions in
> struct file_operation
> as open,read,write,release
> I register my device using register_chrdev
> now how do I implement  a semaphore or mutex so that if 10 process try to
> access same device or write
> some thing to the file they do not do it.Which may give undesired results.
> Though I have written my driver I am not clear
> as how the user space program implements it.
> What my device is doing is
>
> echo -n ddt >> /dev/bond
> it will write ddt to /dev/bond
>
> I am not clear as when echo is used which system calls (fopen,fread,fwrite)
>
> are working
>  in back ground which in turn communicate with my device driver.
> My driver does define open,read,write,close function but how as user space
> system call is accessing it that I am not clear.
> So that I can implement some sort of lock in it.

Dear Bond,

As for as I know, you can write a sample application where you can
open your device (/dev/bond) with simple OPEN system call. However,
you need to create a NODE using MKNOD command, first. Then using that
node, you can open, write/read and close the device.

Sample code is as follows.
int fd;
fd = open("/dev/bond", O_WRONLY);
if (fd < 0)
{
                printf("Unable to open the specidifed file!\n");
	return;
}

/* writing the contents into the device */
if ( (count = write(fd, "bond", 4) > 0)
                printf("\nThe contents are written successfulle!\n\n");
else
	printf("\nThe contents are not written into the file!\n\n");

close(fd);

I generally use spinlocks for protecting my shared data. You can use
the spinlocks in WRITE method of your device driver code for
protecting data among 10 processes. You can initialize the spinlock
with spin_lock_init API and lock it using spin_lock() and unlock it
using spin_unlock() API. There are variants of spinlocks. As suggested
by Greg, you can go through the 5th Chanpter in LDD3.

Please let me know, if you need any additional information on this.

Thanks and Regards,
Ramya.

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ




[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux