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 12:20 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.

I think a mutex will do what you need.

Have you looked at:   Documentation/mutex-design.txt

You should find the Documentation folder at the top level of your
kernel source tree.

The mutex calls are at the bottom of the file.

> 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.

In your example, the shell is opening /dev/bond, so I don't know an
easy way to see what flags it is passing in.

As to what echo is doing, just use strace.  The first part can be
skipped because it's all about opening libraries and hooking into
them.

But the last 10 or so lines have what you're looking for:

I did "strace echo ddt > /dev/null" and below is the relevant info
from the end of the strace output.

===
fstat(1, {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 3), ...}) = 0
ioctl(1, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff5f1de050) = -1 ENOTTY
(Inappropriate ioctl for device)
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0x7f2b0e2b1000
write(1, "ddt\n", 4)                    = 4
close(1)                                = 0
munmap(0x7f2b0e2b1000, 4096)            = 0
close(2)                                = 0
exit_group(0)                           = ?
===

> 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.
>

I'd guess in your case you just need

DEFINE_MUTEX(name);  // declare at file scope
mutex_init(mutex);
mutex_lock(struct mutex *lock);
mutex_unlock(struct mutex *lock);

Look around at the kernel source for some example uses.  Especially
where to call mutex_init().  You obviously don't want to call that in
your write() routine.

Greg

--
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