pipe implementation : link # http://programming-fr34ks.net/forum/...?showtopic=435
اهو فضيان بقي :angry_red:
module بسيط تقدر تستخدمه زي pipe لاي application بتاعتك
ال user يقدر يبعت 265kb array وانت تستقبلها وتقدر تقرأها اول ماتتبعت
طبعا استخدم completion method الي موجودده في ال كيرنيل
كان ممكن semaphore بس كنت عايز اجرب ديت
اهو فضيان بقي :angry_red:
module بسيط تقدر تستخدمه زي pipe لاي application بتاعتك
ال user يقدر يبعت 265kb array وانت تستقبلها وتقدر تقرأها اول ماتتبعت
طبعا استخدم completion method الي موجودده في ال كيرنيل
كان ممكن semaphore بس كنت عايز اجرب ديت
كود PHP:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/moduleparam.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/stat.h>
#include <linux/types.h>
#include <linux/cdev.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <linux/completion.h>
static int major;
static int minor;
static dev_t device_node;
static char buffer[265];
module_param(major , int , S_IWUSR | S_IRUSR);
module_param(minor , int , S_IWUSR | S_IRUSR);
struct cdev device;
struct completion complete_task;
static int pipe_open(struct inode *node , struct file *filp)
{
/* No defice specification Will be Done */
return 0;
}
static int pipe_read(struct file *filp , char __user *buf , size_t size , loff_t *pos)
{
wait_for_completion(&complete_task);
if(copy_to_user(buf , buffer , strlen(buffer)) > 0)
return -1;
else
return strlen(buffer);
}
static int pipe_write(struct file *filp ,
const char __user *buf ,
size_t size ,
loff_t *pos)
{
int count = size;
if(count > 264)
count = 264;
if(copy_from_user(buffer , buf , count) > 0)
return -1;
complete(&complete_task);
return count;
}
static int pipe_release(struct inode *node , struct file *filp)
{
return 0;
}
struct file_operations fops =
{
.open = pipe_open ,
.read = pipe_read ,
.write = pipe_write ,
.release = pipe_release,
};
static int __init pipe_init(void)
{
int result;
if(major)
{
device_node = MKDEV(major , minor);
result = register_chrdev_region(device_node , 1 , "pipe");
}else
{
result = alloc_chrdev_region(&device_node , 1 , 1 , "pipe");
}
if(result == -1)
{
printk(KERN_INFO"Failed To Register Device Number\n\n");
return result;
}
printk(KERN_INFO
"Registred with major:minor %d:%d\n",
MAJOR(device_node) , MINOR(device_node));
cdev_init(&device , &fops);
device.owner = THIS_MODULE;
result = cdev_add(&device , device_node , 1);
if(result == -1)
{
printk(KERN_INFO"Failed To register Char device\n\n");
return result;
}
printk(KERN_INFO"Device Is live\n\n");
/* initialize Complete Structure */
init_completion(&complete_task);
return 0;
}
static void __exit pipe_exit(void)
{
unregister_chrdev_region(device_node , 1);
cdev_del(&device);
printk(KERN_INFO"Device Is Removed\n\n");
}
module_init(pipe_init);
module_exit(pipe_exit);
كود:
[email protected]:~/Desktop/linux.programming/kernel/pipe# insmod pipe_char.ko major=100 minor=0 [email protected]:~/Desktop/linux.programming/kernel/pipe# mknod /dev/pipe_char c 100 0 [email protected]:~/Desktop/linux.programming/kernel/pipe# echo "It's only after we have lost every thing that we are free to do anything" > /dev/pipe_char [email protected]:~/Desktop/linux.programming/kernel/pipe# cat /dev/pipe_char It's only after we have lost every thing that we are free to do anything
تعليق