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?
I think of user land program opening a socket and crashing on segmentation fault. In code 'socket' syscall does: sock_map_fd --> sock_alloc_file --> alloc_file --> ... get lost ... Where exactly in this case lock is held - I mean the lock that gets released when user land process dies? Thanks, --- KostaZ