<br><br><div class="gmail_quote">On Wed, Sep 28, 2011 at 11:11 AM, Michael Blizek <span dir="ltr"><<a href="mailto:michi1@michaelblizek.twilightparadox.com">michi1@michaelblizek.twilightparadox.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi!<br>
<div class="im"><br>
On 21:41 Tue 27 Sep , Venkatram Tummala wrote:<br>
> On Tue, Sep 27, 2011 at 9:19 PM, rohan puri <<a href="mailto:rohan.puri15@gmail.com">rohan.puri15@gmail.com</a>> wrote:<br>
</div>...<br>
<div class="im">> > in device_open() -><br>
> ><br>
> > if(var)<br>
> > return -EBUSY<br>
> > var++<br>
> ><br>
> > &<br>
> ><br>
> > in device_release() -><br>
> ><br>
> > var--<br>
> ><br>
> ><br>
> > I think this should do the job.<br>
> ><br>
> This will prevent other processes to open the file until a process releases<br>
> it. This is not what i need. Only the threads in a process shouldn't be able<br>
> to open the file if it is already opened in the process. Other processes<br>
> should be able to open it.<br>
<br>
</div>You could create something like this:<br>
<br>
DEFINE_MUTEX(pidlist_lock);<br>
LIST_HEAD(pidlist);<br>
<br>
struct pidlist_node{<br>
struct list_head lh;<br>
pid_t pid;<br>
}<br>
<br>
static struct pidlist_node *get_pin(void)<br>
{<br>
struct list_head *curr = pidlist.next;<br>
while (curr != pidlist) {<br>
struct struct pidlist_node *pin = container_of(curr, struct pidlist_node, lh);<br>
if (pin->pid == current->pid) {<br></blockquote><div>Instead of pid check, AFAIK here tgid comparison should be done. Threads in a single process may have different pid but same tgid and we want to restrict access to only one thread in a single process. Right? <br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
return pin;<br>
}<br>
}<br>
return 0;<br>
}<br>
<br>
int open(void)<br>
{<br>
struct pidlist_node *pin;<br>
<br>
mutex_lock(&pidlist_lock);<br>
<br>
pin = get_pin();<br>
if (pin != 0) {<br>
mutex_unlock(&pidlist_lock);<br>
return -EBUSY;<br>
}<br>
<br>
pin = kmalloc(sizeof(struct pidlist_node), GFP_KERNEL);<br>
if (pin == 0) {<br>
mutex_unlock(&pidlist_lock);<br>
return -ENOMEM;<br>
}<br>
<br>
pin->pid = current->pid;<br>
list_add(&(pin->lh), &pidlist);<br>
<br>
mutex_unlock(&pidlist_lock);<br>
}<br>
<br>
int close(void)<br>
{<br>
struct pidlist_node *pin;<br>
<br>
mutex_lock(&pidlist_lock);<br>
<br>
pin = get_pin();<br>
if (pin != 0) {<br>
list_del(&(pin->lh));<br>
kfree(pin);<br>
}<br>
<br>
mutex_unlock(&pidlist_lock);<br>
}<br>
<br>
-Michi<br>
<font color="#888888">--<br>
programing a layer 3+4 network protocol for mesh networks<br>
see <a href="http://michaelblizek.twilightparadox.com" target="_blank">http://michaelblizek.twilightparadox.com</a><br>
<br>
</font></blockquote></div><br>