无法从linux中的设备驱动程序调用Fork()

时间:2015-02-14 21:05:20

标签: c linux kernel fork device-driver

所以,我正在编写这个驱动程序,通过“写入”收到一个“命令”,它应该调用fork,让孩子在父母去世时完成工作。

当我编译它时,输出就是这个。

victor@victor-desktop:~/Área de Trabalho/mc504-linux/linux-3.17.2$ make -j 5 ARCH=i386
CHK     include/config/kernel.release
CHK     include/generated/uapi/linux/version.h
CHK     include/generated/utsrelease.h
CALL    scripts/checksyscalls.sh
CHK     include/generated/compile.h
CC [M]  drivers/ofd/ofcd-lastchar.o
drivers/ofd/ofcd-lastchar.c: In function ‘my_read’:
drivers/ofd/ofcd-lastchar.c:36:5: error: implicit declaration of function ‘fork’ [-Werror=implicit-function-declaration
 child_pid = fork();
 ^
cc1: some warnings being treated as errors
scripts/Makefile.build:263: recipe for target 'drivers/ofd/ofcd-lastchar.o' failed
make[2]: *** [drivers/ofd/ofcd-lastchar.o] Error 1
scripts/Makefile.build:404: recipe for target 'drivers/ofd' failed
make[1]: *** [drivers/ofd] Error 2
Makefile:929: recipe for target 'drivers' failed
make: *** [drivers] Error 2

我正在使用从here下载的Linux。

司机就是这个。它只有一个fork()调用。

#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/unistd.h>
#include <linux/sched.h>

static dev_t first; // Global variable for the first device number
static struct cdev c_dev; // Global variable for the character device structure
static struct class *cl; // Global variable for the device class

static char c;

static int my_open(struct inode *i, struct file *f)
{
  /*
  Abrir devera alocar um pedaço da memoria e salvar 
  */
  printk(KERN_INFO "Driver: open()\n");
  return 0;
}
static int my_close(struct inode *i, struct file *f)
{
  printk(KERN_INFO "Driver: close()\n");
  return 0;
}

static ssize_t my_read(struct file *f, char __user *buf, size_t len, loff_t *off)
{
    pid_t child_pid;
    child_pid = fork();
    if(child_pid == 0)
      printk(KERN_INFO "child: read()\n");
    else
      printk(KERN_INFO "father: read()\n");
    if (copy_to_user(buf, &c, 1) != 0)
        return -EFAULT;
    else
        return 1;
}
static ssize_t my_write(struct file *f, const char __user *buf, size_t len, loff_t *off)
{
    printk(KERN_INFO "Driver: write()\n");
    if (copy_from_user(&c, buf + len - 1, 1) != 0)
        return -EFAULT;
    else
        return len;
};

static struct file_operations pugs_fops =
  {
      .owner = THIS_MODULE,
      .open = my_open,
      .release = my_close,
      .read = my_read,
      .write = my_write
  };

static int __init ofcd_init(void) /* Constructor */
{
  printk(KERN_INFO "Namaskar: ofcd-lastchar registered");
  if (alloc_chrdev_region(&first, 0, 1, "ofcd-lastchar") < 0)
    {
      return -1;
    }
  if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL)
    {
      unregister_chrdev_region(first, 1);
      return -1;
    }
  if (device_create(cl, NULL, first, NULL, "ofcd-lastchar") == NULL)
    {
      class_destroy(cl);
      unregister_chrdev_region(first, 1);
      return -1;
    }
  cdev_init(&c_dev, &pugs_fops);
  if (cdev_add(&c_dev, first, 1) == -1)
    {
      device_destroy(cl, first);
      class_destroy(cl);
      unregister_chrdev_region(first, 1);
      return -1;
    }
  return 0;
}

static void __exit ofcd_exit(void) /* Destructor */
{
  cdev_del(&c_dev);
  device_destroy(cl, first);
  class_destroy(cl);
  unregister_chrdev_region(first, 1);
  printk(KERN_INFO "Alvida: ofcd-lastchar unregistered");
}

module_init(ofcd_init);
module_exit(ofcd_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Kumar Pugalia <email_at_sarika-pugs_dot_com>");
MODULE_DESCRIPTION("Our First Character Driver");

最大的问题是:可以从设备驱动程序调用{​​{1}}吗?

1 个答案:

答案 0 :(得分:5)

你不能在syscalls(2)内使用Linux kernel(因为syscalls是从用户级应用程序到kernel的接口),你真的不应该在里面启动一个进程一个driver,因为有一些痛苦的例外(如pid 1的/sbin/init,或者在某些特定情况下/sbin/hotplug等......)每个进程都由另一个进程调用{{} 3}}(或者fork(2)clone(2) ...)。加载后,您的内核模块将成为内核的一部分(例如,使用vfork(2) ...);但是,insmod(8)增加了图片的复杂性。

也许你应该考虑kernel threads

我建议在大胆编写一些内核模块之前,学习更多关于Linux用户区编程的知识(例如阅读FUSE)。

您可能会阅读Advanced Linux Programming&amp; continuations&amp; CPS。它可以建议您以不同的方式思考驱动程序代码。