How to use semaphore in linux driver and is it a spinlock mechanism?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <stdio.h>
#include <fcntl.h>
//#include <linux/ioctl.h>
//#include "ioctl_cmds.h"
int main()
{
int fp, arg = 54, ret;
char buff[] = "hello";
fp = open ("/dev/mydevice", O_RDWR);
if(fp < 0)
printf("file can't be opened %d\n", fp);
else
printf("file opened\n");
//close(fp);//don't release , run another same app. open call should fail maybe??
return 0;
}
I wanted to open the device file one at a time. When the app calls close, then only the node should be allowed to be used by another app.
Here is what observed,
1)After open call, release gets called even if I comment close system call.
2)If I remove down(&sem), and try to open the device node, Kernel crashes.
3)What is the proper way to do it?
4)I saw internally sema_init uses some spinlock mechanism to make a semaphore. So semaphore is a spinlock?
from semaphore.h
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.
Within the Linux kernel, some locks are spinlocks. These are used to coordinate activities between multiple CPUs or cores.
Specifically: a "spinlock" uses a hardware mechanism (appropriate to the hardware platform in question ...) which atomically seeks to set a certain memory-location to a certain value, such that no other core or CPU can attempt to do the same thing simultaneously. As long as the CPU/core finds that it is prevented from doing to, it "spins its wheels" (uselessly) until it finally can. This mechanism is tolerable only for the tiniest of "races" which are presumed to have a very high probability of immediate success and a low probability of actually "spinning." It is used only for synchronization between otherwise-independent CPUs/cores.
"Opening" and "closing" a semaphore provides basic access to it. The "up" and "down" actions are analogous to raising or lowering a flag. The purpose of any [kernel] semaphore is to enable a [kernel] process to efficiently "go to sleep" until a certain event occurs, as indicated by a change in the status of the semaphore by someone else. (Or, as the case may be, "to spin its wheels.") The same concepts also exist in "user-land" albeit implemented differently. (User-land processes never "spin.")
Last edited by sundialsvcs; 03-27-2017 at 07:23 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.