Ñ§Ï°Íø¿¼ÊÔѧϰ×ÊÁÏ

Gzu521.com

Linux²Ù×÷ϵͳÄں˺ÍÉ豸Îļþ¶Ô»°(1)

LINUX½Ì³Ì   µã»÷£º´Î   ·¢²¼Ê±¼ä£º2006-11-17   ¡¾×ÖÌ壺´ó ÖРС¡¿   À´Ô´£ºGzu521.com
Gzu521.comÎÒµÄÑ§Ï°Íø
É豸ÎļþÊÇÓÃÀ´´ú±íÎïÀíÉ豸µÄ¡£¶àÊýÎïÀíÉ豸ÊÇÓÃÀ´½øÐÐÊä³ö»òÊäÈëµÄ£¬ËùÒÔ±ØÐëÓÉijÖÖ»úÖÆÊ¹µÃÄÚºËÖеÄÉ豸Çý¶¯´Ó½ø³ÌÖеõ½Êä³öË͸øÉ豸¡£Õâ¿ÉÒÔͨ¹ý´ò¿ªÊä³öÉ豸Îļþ²¢ÇÒдÈë×öµ½£¬¾ÍÏëдÈëÒ»¸öÆÕͨÎļþ¡£ÔÚÏÂÃæµÄÀý×ÓÀÕâÓÉdevice_writeʵÏÖ¡£  

€XDyd1 L1(×Ê/ÁÏÀ´.Ô´,ÓÚ:£çzu£µ2£±Ñ§;ϰ/Íø:µçÄÔ¿ÎÌÃ;LINUX½Ì³Ì ]£çzu£µ2£±.£ão£í€XDyd1 L1


¡¡¡¡Õâ²»ÊÇ×ÜÄÜ×àЧµÄ¡£ÉèÏëÄãÓëÒ»¸öÁ¬ÏòmodemµÄ´®¿Ú£¨¼¼ÊÇÄãÓÐÒ»¸öÄÚ裬´Ócpu¿´À´ËüÒ²ÊÇ×÷Ϊһ¸ö´®¿ÚʵÏÖ£¬ËùÒÔÄã²»ÐèÒªÈÏΪÕâ¸öÉèÏëÌ«À§ÄÑ£©¡£×î×ÔȻҪ×öµÄÊÂÇé¾ÍÊÇʹÓÃÉ豸Îļþ°ÑÄÚÈÝдµ½modemÉÏ£¨ÎÞÂÛÓÃmodemÃüÁÊǵ绰Ïߣ©»òÕß´Ómodem¶ÁÐÅÏ¢£¨Í¬Ñù¿ÉÒÔ´ÓmodemÃüÁî»Ø´ð»òÕßͨ¹ýµç»°Ïߣ©¡£µ«ÊÇÕâÁôϵÄÎÊÌâÊǵ±ÄãÐèÒªºÍ´®¿Ú±¾Éí¶Ô»°µÄʱºòÐèÒªÔõÑù×ö£¿±ÈÈç·¢ËÍÊý¾Ý·¢ËͺͽÓÊÕµÄËÙÂÊ¡£  
¡¡¡¡»Ø´ðÊÇunixʹÓÃÒ»¸ö½Ð×öioctl(input output controlµÄ¼òд)µÄÌØÊ⺯Êý¡£Ã¿¸öÉ豸¶¼ÓÐ×Ô¼ºµÄioctlÃüÁÕâ¸öÃüÁî¿ÉÒÔÊÇioctl¶ÁµÄ£¬Ò²¿ÉÒÔÊÇдµÄ£¬Ò²¿ÉÒÔÊÇÁ½Õß¶¼ÊÇ»ò¶¼²»ÊÇ¡£ioctlº¯ÊýÓÉÈý¸ö²ÎÊýµ÷ÓãºÊʵ±É豸µÄÃèÊö×Ó£¬ioctlÊý£¬ºÍÒ»¸ö³¤ÕûÐͲÎÊý£¬¿ÉÒÔ¸³ÓèÒ»¸ö½ÇÉ«ÓÃÀ´´«µÝÈκζ«Î÷¡£  

¡¡¡¡ioctlÊý¶ÔÉ豸Ö÷Âë¡¢ioctlÀàÐÍ¡¢±àÂë¡¢ºÍ²ÎÊýµÄÀàÐͽøÐбàÂë¡£ioctlÊýͨ³£ÔÚÍ·ÎļþÓÉÒ»¸öºêµ÷Óã¨_io£¬_ior£¬_iow»ò_iowr¡ª¡ª¾ö¶¨ÓÚÀàÐÍ£©¡£Õâ¸öÍ·Îļþ±ØÐë°üº¬ÔÚʹÓÃioctl£¨ËùÒÔËüÃÇ¿ÉÒÔ²úÉúÕýÈ·µÄioctl¡¯s£©³ÌÐòºÍÄÚºËÄ£¿é£¨ËùÒÔËü¿ÉÒÔÀí½â£©ÖС£ÔÚÏÂÃæµÄÀý×ÓÀÕâ¸öÍ·ÎļþÊÇchardev.h£¬Ê¹ÓÃËüµÄ³ÌÐòÊÇioctl.c¡£  

¡¡¡¡Èç¹ûÄãÏ£ÍûÔÚÄã×Ô¼ºµÄÄÚºËÄ£¿éÖÐʹÓÃioctl¡¯s£¬×îºÃÈ¥½ÓÊÜÒ»·ÖÕýʽµÄioctlְ룬ÕâÑùÄã¾Í¿ÉÒԵõ½±ðÈ˵Äioctl¡¯s£¬»òÕßËûÃǵõ½Ä㣬Äã¾Í¿ÉÒÔÖªµÀÄÄÀï³öÁË´íÎó¡£Èç¹ûÏëµÃµ½¸ü¶àµÄÐÅÏ¢£¬µ½¡¯documentation/ioctl-number.txt¡¯Öв鿴ÄÚºËÔ´ÎļþÊ÷¡£  

ex chardev.c   

/* chardev.c   
*   
* create an input/output character device   
*/   


/* copyright (c) 1998-99 by ori pomerantz */   

  

/* the necessary header files */   

/* standard in kernel modules */   
#include /* were doing kernel work */   
#include /* specifically, a module */   

/* deal with config_modversions */   
#if config_modversions==1   
#define modversions   
#include   
#endif   

/* for character devices */   

/* the character device definitions are here */   
#include   

/* a wrapper which does next to nothing at   
* at present, but may help for compatibility   
* with future versions of linux */   
#include   


/* our own ioctl numbers */   
#include "chardev.h"   


/* in 2.2.3 /usr/include/linux/version.h includes a   
* macro for this, but 2.0.35 doesnt - so i add it   
* here if necessary. */   
#ifndef kernel_version   
#define kernel_version(a,b,c) ((a)*65536+(b)*256+(c))   
#endif   

  

#if linux_version_code >= kernel_version(2,2,0)   
#include /* for get_user and put_user */   
#endif   

  

#define success 0   


/* device declarations ******************************** */   


/* the name for our device, as it will appear in   
* /proc/devices */   
#define device_name "char_dev"   


/* the maximum length of the message for the device */   
#define buf_len 80   

/* is the device open right now? used to prevent   
* concurent access into the same device */   
static int device_open = 0;   

/* the message the device will give when asked */   
static char message[buf_len];   

/* how far did the process reading the message get?   
* useful if the message is larger than the size of the   
* buffer we get to fill in device_read. */   
static char *message_ptr;   


/* this function is called whenever a process attempts   
* to open the device file */   
static int device_open(struct inode *inode,   
struct file *file)   
{   
#ifdef debug   
printk ("device_open(%p)\n", file);   
#endif   

/* we dont want to talk to two processes at the   
* same time */   
if (device_open)   
return -ebusy;   

/* if this was a process, we would have had to be   
* more careful here, because one process might have   
* checked device_open right before the other one   
* tried to increment it. however, were in the   
* kernel, so were protected against context switches.   
*   
* this is not the right attitude to take, because we   
* might be running on an smp box, but well deal with   
* smp in a later chapter.   
*/   

ÏÂ Ò» Ò³
±¾ÎĹ²5Ò³: µÚ [1] [2] [3] [4] [5] Ò³

ÔðÈα༭£ºgzu521

µçÄÔ¿ÎÌ÷ÖÀà
Windows 2000½Ì³Ì
Windows XP½Ì³Ì
Windows 2003½Ì³Ì
Windows Vista½Ì³Ì
LINUX½Ì³Ì
Èí¼þ½Ìѧ
°ì¹«Èí¼þ
Ó²¼þDIY
·ÖÀàÍÆ¼öÐÅÏ¢
¸ü¶à...
´óÀà×îÐÂÎÄÕÂ
¸ü¶à...