ºìÁªLinuxÃÅ»§
Linux°ïÖú

É豸Çý¶¯µÄ²¢·¢¿ØÖÆ£¨Ëᦻª£©ÊµÀý²Ù×÷£¨2.6Äں˵ıàÒëͨ¹ý£©

·¢²¼Ê±¼ä:2012-08-09 16:25:00À´Ô´:ºìÁª×÷Õß:linux08071151
ÔÚÇý¶¯³ÌÐòÖУ¬µ±¶à¸öÏß³Ìͬʱ·ÃÎÊÏàͬµÄ×ÊԴʱ£¨Çý¶¯³ÌÐòÖеÄÈ«¾Ö±äÁ¿ÊÇÒ»ÖÖµäÐ͵Ĺ²Ïí×ÊÔ´£©£¬¿ÉÄÜ»áÒý·¢¡°¾ºÌ¬¡±£¬Òò´ËÎÒÃDZØÐë¶Ô¹²Ïí×ÊÔ´½øÐв¢·¢¿ØÖÆ¡£ LinuxÄÚºËÖнâ¾ö²¢·¢¿ØÖƵÄ×î³£Ó÷½·¨ÊÇ×ÔÐýËøÓëÐźÅÁ¿£¨¾ø´ó¶àÊýʱºò×÷Ϊ»¥³âËøʹÓã©¡£×ÔÐýËøÓëÐźÅÁ¿¡°ÀàËƶøÀࡱ£¬ÀàËÆ˵µÄÊÇËüÃǹ¦ÄÜÉϵÄÏàËÆÐÔ£¬¡°²»Àࡱָ´úËüÃÇÔÚ±¾ÖʺÍʵÏÖ»úÀíÉÏÍêÈ«²»Ò»Ñù£¬²»ÊôÓÚÒ»Àà¡£×ÔÐýËø²»»áÒýÆðµ÷ÓÃÕß˯Ãߣ¬Èç¹û×ÔÐýËøÒѾ­±»±ðµÄÖ´Ðе¥Ôª±£³Ö£¬µ÷ÓÃÕß¾Íһֱѭ»·
²é¿´ÊÇ·ñ¸Ã×ÔÐýËøµÄ±£³ÖÕßÒѾ­ÊÍ·ÅÁËËø£¬¡°×ÔÐý¡±¾ÍÊÇ¡°ÔÚÔ­µØ´òת¡±¡£¶øÐźÅÁ¿ÔòÒýÆðµ÷ÓÃÕß˯Ãߣ¬Ëü°Ñ½ø³Ì´ÓÔËÐжÓÁÐÉÏÍϳöÈ¥£¬³ý·Ç»ñµÃËø¡£Õâ¾ÍÊÇËüÃǵġ°²»Àࡱ¡£µ«ÊÇ£¬ÎÞÂÛÊÇÐźÅÁ¿£¬»¹ÊÇ×ÔÐýËø£¬ÔÚÈκÎʱ¿Ì£¬×î¶àÖ»ÄÜÓÐÒ»¸ö±£³ÖÕߣ¬¼´ÔÚÈκÎʱ¿Ì×î¶àÖ»ÄÜÓÐÒ»¸öÖ´Ðе¥Ôª»ñµÃËø¡£Õâ¾ÍÊÇËüÃǵġ°ÀàËÆ¡±¡£¼øÓÚ×ÔÐýËøÓëÐźÅÁ¿µÄÉÏÊöÌص㣬һ°ã¶øÑÔ£¬×ÔÐýËøÊʺÏÓÚ±£³Öʱ¼ä·Ç³£¶ÌµÄÇé¿ö£¬Ëü¿ÉÒÔÔÚÈκÎÉÏÏÂÎÄʹÓã»ÐźÅÁ¿ÊʺÏÓÚ±£³Öʱ¼ä½Ï³¤µÄÇé¿ö£¬»áÖ»ÄÜÔÚ½ø³ÌÉÏÏÂÎÄʹÓá£Èç¹û±»±£»¤µÄ¹²Ïí×ÊÔ´Ö»ÔÚ½ø³ÌÉÏÏÂÎÄ·ÃÎÊ£¬Ôò¿ÉÒÔÒÔÐźÅÁ¿À´±£»¤¸Ã¹²Ïí×ÊÔ´£¬Èç¹û¶Ô¹²Ïí×ÊÔ´µÄ·ÃÎÊʱ¼ä·Ç³£¶Ì£¬×ÔÐýËøÒ²ÊǺõÄÑ¡Ôñ¡£µ«ÊÇ£¬Èç¹û±»±£»¤µÄ¹²Ïí×ÊÔ´ÐèÒªÔÚÖжÏÉÏÏÂÎÄ·ÃÎÊ£¨°üÀ¨µ×°ë²¿¼´Öжϴ¦Àí¾ä±úºÍ¶¥°ë²¿¼´ÈíÖжϣ©£¬¾Í±ØÐëʹÓÃ×ÔÐýËø¡£

ÓëÐźÅÁ¿Ïà¹ØµÄ
APIÖ÷ÒªÓУº

¶¨ÒåÐźÅÁ¿
struct semaphore sem£»

³õʼ»¯ÐźÅÁ¿
void sema_init (struct semaphore *sem, int val);
¸Ãº¯Êý³õʼ»¯ÐźÅÁ¿£¬²¢ÉèÖÃÐźÅÁ¿
semµÄֵΪ val
void init_MUTEX (struct semaphore *sem); ¸Ãº¯ÊýÓÃÓÚ³õʼ»¯Ò»¸ö»¥³âËø£¬¼´Ëü°ÑÐźÅÁ¿ semµÄÖµÉèÖÃΪ 1£¬µÈͬÓÚ sema_init (struct semaphore *sem, 1)£»
void init_MUTEX_LOCKED (struct semaphore *sem); ¸Ãº¯ÊýÒ²ÓÃÓÚ³õʼ»¯Ò»¸ö»¥³âËø£¬µ«Ëü°ÑÐźÅÁ¿ semµÄÖµÉèÖÃΪ 0£¬µÈͬÓÚ sema_init (struct semaphore *sem, 0)£»

»ñµÃÐźÅÁ¿

void down(struct semaphore * sem); ¸Ãº¯ÊýÓÃÓÚ»ñµÃÐźÅÁ¿ sem£¬Ëü»áµ¼ÖÂ˯Ãߣ¬Òò´Ë²»ÄÜÔÚÖжÏÉÏÏÂÎÄʹÓã»
int down_interruptible(struct semaphore * sem);
¸Ãº¯Êý¹¦ÄÜÓë downÀàËÆ£¬²»Í¬Ö®´¦Îª£¬ down²»Äܱ»ÐźŴò¶Ï£¬µ« down_interruptibleÄܱ»ÐźŴò¶Ï£»
int down_trylock(struct semaphore * sem);
¸Ãº¯Êý³¢ÊÔ»ñµÃÐźÅÁ¿ sem£¬Èç¹ûÄܹ»Á¢¿Ì»ñµÃ£¬Ëü¾Í»ñµÃ¸ÃÐźÅÁ¿²¢·µ»Ø 0£¬·ñÔò£¬·µ»Ø·Ç 0Öµ¡£Ëü²»»áµ¼Öµ÷ÓÃÕß˯Ãߣ¬¿ÉÒÔÔÚÖжÏÉÏÏÂÎÄʹÓá£

ÊÍ·ÅÐźÅÁ¿


void up(struct semaphore * sem); ¸Ãº¯ÊýÊÍ·ÅÐźÅÁ¿ sem£¬»½ÐѵȴýÕß¡£Óë×ÔÐýËøÏà¹ØµÄ APIÖ÷ÒªÓУº

¶¨Òå×ÔÐýËø
spinlock_t spin;


³õʼ»¯×ÔÐýËø
spin_lock_init(lock) ¸ÃºêÓÃÓÚ¶¯Ì¬³õʼ»¯×ÔÐýËø lock

»ñµÃ×ÔÐýËø
spin_lock(lock) ¸ÃºêÓÃÓÚ»ñµÃ×ÔÐýËø lock£¬Èç¹ûÄܹ»Á¢¼´»ñµÃËø£¬Ëü¾ÍÂíÉÏ·µ»Ø£¬·ñÔò£¬Ëü½«×ÔÐýÔÚÄÇÀֱµ½¸Ã×ÔÐýËøµÄ±£³ÖÕßÊÍ·Å£»


spin_trylock(lock) ¸Ãºê³¢ÊÔ»ñµÃ×ÔÐýËø lock£¬Èç¹ûÄÜÁ¢¼´»ñµÃËø£¬Ëü»ñµÃËø²¢·µ»ØÕ棬·ñÔòÁ¢¼´·µ»Ø¼Ù£¬Êµ¼ÊÉϲ»ÔÙ¡°ÔÚÔ­µØ´òת¡±£»

ÊÍ·Å×ÔÐýËø

spin_unlock(lock) ¸ÃºêÊÍ·Å×ÔÐýËø lock£¬ËüÓë spin_trylock»ò spin_lockÅä¶ÔʹÓ㻳ý´ËÖ®Í⣬»¹ÓÐÒ»×é×ÔÐýËøʹÓÃÓÚÖжÏÇé¿öÏ嵀 API¡£

//bingfa.c
#include
#include
#include
#include
#include

MODULE_LICENSE("GPL");

#define MAJOR_NUM 265

static ssize_t bingfa_read(struct file *, char *, size_t, loff_t*);
static ssize_t bingfa_write(struct file *, const char *, size_t, loff_t*);
static int bingfa_open(struct inode *inode, struct file *filp);
static int bingfa_release(struct inode *inode, struct file *filp);


struct file_operations bingfa_fops =
{

read: bingfa_read, write: bingfa_write, open: bingfa_open, release:
bingfa_release,
};

static int global_var = 0;
static int bingfa_count = 0;
static struct semaphore sem;
static spinlock_t spin = SPIN_LOCK_UNLOCKED;


static int __init bingfa_init(void)


{
int ret;
ret = register_chrdev(MAJOR_NUM, "bingfa", &bingfa_fops);
if (ret)
{

printk("bingfa register failure");
}
else
{


printk("bingfa register success");

init_MUTEX(&sem);
}
return ret;


}

static void __exit bingfa_exit(void)

{
unregister_chrdev(MAJOR_NUM, "bingfa");
}

static int bingfa_open(struct inode *inode, struct file *filp)
//»ñµÃ×ÔÑ¡Ëø
spin_lock(&spin);

//ÁÙ½ç×ÊÔ´·ÃÎÊ
if (bingfa_count)
{

spin_unlock(&spin);

return -EBUSY;
}
bingfa_count++;
//ÊÍ·Å×ÔÑ¡Ëø
spin_unlock(&spin);
return 0;
}

static int bingfa_release(struct inode *inode, struct file *filp)
{
bingfa_count--;
return 0;

}

static ssize_t bingfa_read(struct file *filp, char *buf, size_t len, loff_t
*off)
{
if (down_interruptible(&sem))
{
return -ERESTARTSYS;
}
if (copy_to_user(buf, &global_var, sizeof(int)))
{

up(&sem);
return -EFAULT;
}
up(&sem);
return sizeof(int);
}
//
static ssize_t bingfa_write(struct file *filp, const char *buf, size_t len, loff_t *off)
{
if (down_interruptible(&sem))
{

return -ERESTARTSYS;
}
if (copy_from_user(&global_var, buf, sizeof(int)))
{

up(&sem);
return -EFAULT;
}
up(&sem);
return sizeof(int);
}

module_init(bingfa_init);
module_exit(bingfa_exit);

//²âÊÔ´úÂëchardev_test.c
#include
#include
#include
#include
main()
{

int fd, num;
//´ò¿ª¡°/dev/bingfa¡±
fd = open("/dev/bingfa", O_RDWR, S_IRUSR | S_IWUSR);
if (fd != -1 )
{
//
//
//³õ´Î¶Á bingfa
read(fd, &num, sizeof(int));
printf("The bingfa is %d\n", num);
//дbingfa
printf("Please input the num written to bingfa\n");
scanf("%d", &num);
write(fd, &num, sizeof(int));
//
//
// //ÔٴζÁ bingfa
read(fd, &num, sizeof(int));
printf("The bingfa is %d\n", num);
//
//
// //¹Ø±Õ¡°/dev/bingfa¡±
//
close(fd);
}
else
{
//
//
printf("Device open failure\n");
}
}
²âÊÔ½á¹û:[attach]38762[/attach]
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2 ÌõÆÀÂÛ

  1. Oracleust ÓÚ 2012-08-09 21:34:08·¢±í:

    Ç¿ÈË

  2. °ë²½ÌìÑÄ ÓÚ 2012-08-09 18:19:06·¢±í:

    ÍÛÈû£¬Ä¿Ç°¿´²»¶®¡£ ÂýÂýѧ°É¡£