Semaphore

Kristof Provost kristof at sigsegv.be
Fri Feb 24 04:28:53 EST 2012


On 2012-02-24 11:25:00 (+0200), Kosta Zertsekel <zertsekel at gmail.com> wrote:
> On Fri, Feb 24, 2012 at 11:04 AM, Kristof Provost <kristof at sigsegv.be> wrote:
> > On 2012-02-24 09:07:40 (+0200), Kosta Zertsekel <zertsekel at gmail.com> wrote:
> >> >> > Imagine a driver which only one app can use at a time (perhaps a serial
> >> >> > port). The kernel will take a lock when the user space app open()s the
> >> >> > device node and release it when it close()s.
> >> >> > If the app segfaults in between the open() and close() the lock will
> >> >> > still get released. The kernel always cleans up open files for stopped
> >> >> > processes, regardless of how they stop. During this cleanup the kernel
> >> >> > will close() the device node, and as a result the driver will release
> >> >> > the lock.
> >>
> >> Can you please point to some code in Linux Kernel that does the job?
> >
> > In kernel/exit.c, look at do_exit(). It cleans up a process after it's
> > terminated (for whatever reason).
> > It does a lot of cleanup, but through exit_files() -> put_files_struct()
> > -> close_files() it ends up iterating over all open file descriptors and
> > closing them.
> > What's done for each close depends on what the process had open. Normal
> > files will just be closed, or a TCP socket might be closed, or if a
> > device node was opened the corresponding drivers close() function will
> > be called.
> 
> I meant that I don't see any semaphore related stuff in do_exit().
> It seems that semaphore just remains there in the system after a user
> land task is killed...
> 
In this (fictional, I've not looked at the serial driver code) example I'm 
assuming a semaphore owned by a driver. The user space application is using 
the driver through a device node. The semaphore is managed by the driver, 
and released when the close is called.
The driver does not know how (or even if) the application stopped, only
that it closed the file descriptor.

What specific type of semaphore are you interested in? Who acquires it,
what does it protect?

Regards,
Kristof




More information about the Kernelnewbies mailing list