Linux - KernelThis forum is for all discussion relating to the Linux kernel.
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.
ssize_t dev_write(struct file *filp,const char *buf,size_t count,loff_t *offset)
{
//printk(KERN_ALERT"\nsorry,byebye");
int j;
//msg_ptr = kmalloc(count,GFP_KERNEL);
//for(j=0;j<count;j++)
if(count>100)
return -1;
j = copy_from_user(chr_arr.array,buf,count);
//printk(KERN_ALERT"msg_ptr:%s",msg_ptr);
return j;
}
static int dev_open(struct inode *inode,struct file *filp)
{
filp->private_data = inode->i_cdev;
if(down_interruptible(&chr_arr.sem))
{
printk(KERN_INFO " could not hold semaphore");
return -1;
}
//printk(KERN_ALERT"ah ha the device is open !now we can go further");
return 0;
}
static int dev_release(struct inode *inode,struct file *filp)
{
up(&chr_arr.sem);
return 0;
//module_put(THIS_MODULE);
return 0;
}
static int init_device(void)
{
int result;
dev_t dev_no,dev;
result = alloc_chrdev_region(&dev_no,0,1,"chr_dev");
if(result < 0)
{
printk("sorry no major number left");
return result;
}
major = MAJOR(dev_no);
dev = MKDEV(major,0);
cdev = cdev_alloc();
cdev->ops = &dev_ops;
sema_init(&chr_arr.sem,1);
printk("the major number allocated is %d\n",major);
result = cdev_add(cdev,dev,1);
if(result < 0 )
{
printk(KERN_INFO "Unable to allocate cdev");
return result;
}
/*dev_t dev =0;
dev_major = register_chrdev(dev,"chr_dev",&dev_ops);
printk(KERN_ALERT"MAJOR NUMBER IS:%d and minor is:%d",dev_major,dev_minor);*/
return 0;
}
static void clean_device(void)
{
//dev_t devno = MKDEV(dev_major, dev_minor);
//printk(KERN_ALERT"removing MAJOR NUMBER IS:%d and removing minor is:%d",dev_major,dev_minor);
cdev_del(cdev);
unregister_chrdev_region(major,1);
do I want to specify the device major number, minor number, semaphore, opening the device function(dev_open). I want to send a timestamp value of the kernel to user space using procfs but I found the above code in this link. but I am confused do I want to use so many extra things or is it mandatory ??
another question : there is a another source code in kernel : dev.c - I modified the code and calculated the time at which it is interrupted i.e extern double interrupttime = ktime_get_real();
i will get the timestamp at which the data is received. I want to send this interrupt time to the user application. so someone please tell me how to send this interrupt time via the above code(procfs). I want to send it via read function.
file_operations is for writing your own filesystem. If you just want to add an entry in the existing proc filesystem then you are looking in the wrong direction. Go to the first question on this forum: List of Linux Kernel Development tutorials/books and start from there instead of just cutting and pasting code. When you get stuck, post a specific question.
if (copy_to_user(buf,chr_arr.array,len))
return -EFAULT;
return len;
}
I want to read a value from kernel and use it in a user application, so i am using procfs api to read from the kernel and use it in a user space.
The above is the read function to read from the kernel and store it in a user buffer(buf). But If i want to read the output from user application then where will be value read from kernel stored in a user space ?? could someone help me in this ??
if (copy_to_user(buf,chr_arr.array,len))
return -EFAULT;
return len;
}
I want to read a value from kernel and use it in a user application, so i am using procfs api to read from the kernel and use it in a user space.
The above is the read function to read from the kernel and store it in a user buffer(buf). But If i want to read the output from user application then where will be value read from kernel stored in a user space ?? could someone help me in this ??
There's already a /proc/kmem, available only to root, but nevertheless you don't want to take this approach ... and here is why: timing.
Processes (both kernel and user) run asynchronously, often on several CPUs and/or cores. When you are retrieving data that (as it happens) is being served-up by the kernel, it is necessary to guarantee that the correct data is retrieved (nothing else is retrievable), and that the data thus-retrieved is both complete and stable.
Therefore, you need a truly stable API of your own. A /proc filesystem entry could be used ... every single one of the entries in that pseudo-filesystem is at its heart "an example of this idea." Or, you could define your own custom device-driver that, as it happens, doesn't actually correspond to any physical device but that establishes a /dev entry anyway.
On the kernel side (as with /proc drivers right now), the kernel validates the request, acquires whatever locks or semaphores may be needed, transfers the data in some form to a validated area within the user memory space. It thus assures that the data is reliable, that it is delivered only to intended recipients, and that no race conditions occur.
for exmaple : when the interrupt occurs in the kernel and If I am reading a timestamp in the kernel. I am reading the timestamp from kernel to the user via procfs. where that interrupt time value will be stored ?? how should the user read that value from the user space ??
Last edited by hemanth1989; 04-20-2014 at 09:56 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.