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

linux³ÌÐòÉè¼ÆÎļþĿ¼²Ù×÷¶ÁÊé±Ê¼Ç

·¢²¼Ê±¼ä:2007-10-14 21:03:55À´Ô´:ºìÁª×÷Õß:cights
±È½ÏÖØÒªµÄÉ豸ÎļþÓÐÈý¸ö£¬ËüÃÇÊÇ/dev/console,/dev/ttyºÍ/dev/null

1,/dev/consoleÉ豸£ºÏµÍ³¿ØÖÆ̨£¬³ö´íºÍÕï¶ÏÐÅϢͨ³£»á±»·¢Ë͵½Õâ¸öÉ豸¡£
2£¬/dev/ttyÉ豸£º½ø³Ì¿ØÖÆÖն˵ÄÒ»¸ö¼ÙÃû¡£
3£¬/dev/null:¿ÕÉ豸£¬ËùÒÔдÏòÕâ¸öÉ豸µÄÊä³ö¶¼½«±»¶ªÆú¡£¶ø¶ÁÕâ¸öÉ豸»áÁ¢¿Ì·µ»ØÒ»¸öÎļþβ±êÖ¾¡£

³£Ó÷ÃÎÊÎļþµÄº¯Êý£º
open£º
read£º
write£º
close£º
ioctl£º°Ñ¿ØÖÆÐÅÏ¢´«µÝµ½É豸Çý¶¯³ÌÐò¡£

ÎļþÃèÊö·û£º
0£º±ê×¼ÊäÈ룻
1£º±ê×¼Êä³ö£»
2£º±ê×¼´íÎó£»

openϵͳµ÷Óãº

½¨Á¢Ò»Ìõµ½Îļþ»òÕßÉ豸µÄ·ÃÎÊ·¾¶£¬Èç¹û²Ù×÷³É¹¦£¬Ëü½«·µ»ØÒ»¸öÎļþÃèÊö·û£¬ºóÐøµÄread,writeµÈϵͳµ÷Óþͽ«Ê¹ÓøÃÎļþÃèÊö·û¶Ô´ò¿ªµÄÄǸöÎļþ½øÐвÙ×÷¡£
#include
#include
#include
int open (const char *path,int oflags);
int open (const char *path,int oflags,mode_t mode);

oflags²ÎÊýÊÇͨ¹ý°ÑÒªÇóµÄÎļþ·ÃÎÊģʽÓëÆäËü¿Éѡģʽ°´Î»orµÃµ½µÄ¡£

Îļþ·ÃÎÊģʽ£ºO_RDONLY(Ö»¶Á£©O_WRONLY(ֻд£©O_RDWR(¶Áд£©
ÆäËü¿Éѡģʽ£º
O_APPEND(°ÑдÈëµÄÊý¾Ý×·¼ÓÔÚÎļþµÄĩ⣩
O_TRUNC(°ÑÎļþµÄ³¤¶ÈÉèÖÃΪ0£¬¶ªÆúÆäÖÐÏÖÓеÄÄÚÈÝ£©
O_CREAT(°´modeÖиø³öµÄģʽ´´½¨Îļþ£©
O_EXCL(Óëo_creatÒ»ÆðʹÓã¬Èç¹ûÎļþ´æÔÚ£¬open²Ù×÷ʧ°Ü£©

mode²ÎÊý£º
S_IRUSR:¶ÁȨÏÞ,ÎļþÊôÖ÷
S_IWUSR:д
S_IXUSR:Ö´ÐÐȨÏÞ£¬ÎļþÊôÖ÷
S_IRGRP:¶Á
S_IWGRP:д
S_IXGRP:Ö´ÐÐȨÏÞ£¬ÎļþËùÔÚ·Ö×é
S_IROTH:¶Á
S_IWOTH:д
S_IXOTH:Ö´ÐÐȨÏÞ£¬ÆäËüÓû§

È磺
open ("myfile", O_CREAT, S_IRUSR | S_IXOHT);
´´½¨Ò»¸öÃûΪmyfileµÄÎļþ£¬ÎļþµÄÊôÖ÷ÓµÓÐËüµÄ¶Á²Ù×÷ȨÏÞ£¬ÆäËüÓû§Ö»ÄÜÖ´ÐС£Mode»áÊÜumask¾ö¶¨¡£(chmod umask myfile)

closeϵͳµ÷Óãº

ÖÕÖ¹Ò»¸öÎļþÃèÊö·ûfildesÓëËüÎļþÖ®¼äµÄ¹ØÁª¡£
#inlcude
int close (int fildes);

writeϵͳµ÷Óãº
°Ñ»º³åÇøBUFÀïµÄÇ°nbytes¸ö×Ö½ÚдÈëÓëÎļþÃèÐð·ûfildesÏà¹ØÁªµÄÎļþÖÐÈ¥¡£
#include
size_t write(int fildes,const void *buf ,size_t nbytes);

readϵͳµ÷Óãº

ÓëÎļþÃèÐð·ûfildesÏà¹ØÁªµÄÎļþÖеĶÁÈëÇ°nbytes¸öÊý¾Ý¶Á£¬²¢°ÑËüÃǷŵ½×Ö½Ú»º³åÇøBUF¡£ËüµÄ·µ»ØÖµÊÇʵ¼Ê¶ÁÈëµÄ×Ö½ÚÊý£¬Ëü¿ÉÄÜ»áСÓÚnbytesµÄÖµ¡£

#include
size_t read(int fildes,const void *buf ,size_t nbytes);

ioctlϵͳµ÷Óãº

Ìṩһ¸ö¶ÔÉ豸ÐÐΪ£¬É豸ÃèÊö·û£¬É豸µ×²ã·þÎñµÄÅäÖù¤×÷µÈ·½Ãæ½øÐпØÖƵIJÙ×÷½çÃæ¡£
#include
int ioctl(int fildes,int cmd, ... );

lseek,fstat,stat,lstatϵͳµ÷Óã¬ÏÈÌø¹ý£»

±ê×¼IO¿â£º

fopenº¯Êý£º
#include
FILE *fopen(const char *filename,const char *mode);
´ò¿ªÄ£Ê½ÓУº
"r" "rb" Ö»¶Á·½Ê½´ò¿ª
"w" "wb" ÒÔд·½Ê½´ò¿ª£¬²¢°ÑÎļþ³¤¶È½Ø¶ÌΪÁã
"a" "ab" ÒÔд·½Ê½´ò¿ª£¬ÐÂÄÚÈÝ×·¼ÓÔÚÎļþβ
"r+" "rb+" "r+b" ÒÔÐ޸ķ½Ê½´ò¿ª£¬£¨¶Áд£©
"w+" "wb+" "w+b" ÒÔÐ޸ķ½Ê½´ò¿ª£¬²¢°ÑÎļþ³¤¶È½Ø¶ÌΪÁã
"a+" "ab+" "a+b" ÒÔÐ޸ķ½Ê½´ò¿ª£¬ÐÂÄÚÈÝ×·¼ÓÔÚÎļþβ

×Öĸ¡°b¡±±íʾÎļþÊÇÒ»¸ö¶þ½øÖÆÎļþ¡£UNIX°ÑËùÒÔÎļþ¶¼¿´³ÉÊǶþ½øÖÆÎļþ¡£
mode²ÎÊýÊÇÒ»¸ö×Ö·û´®£¬ËùÒÔҪʹÓá°r¡±£¬¶ø²»ÊÇ¡®r¡¯¡£

fread º¯Êý£º
#include
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
´ÓÎļþÁ÷stream¶ÁÊý¾Ýµ½ptrÖ¸¶¨µÄÊý¾Ý»º´æÇøÀï¡£ËüµÄ·µ»ØÖµÊdzɹ¦µØ¶Áµ½Êý¾Ý»º´æÇøÀïµÄ¼Ç¼¸öÊý£¬¶ø²»ÊÇ×Ö½ÚÊý¡£
fwriteº¯Êý£º
size_t fwrite (const void *ptr, size_t size, size_t nitems, FILE *stream);
´ÓÖ¸¶¨Êý¾Ý»º´æÈ¡Êý¾Ýдµ½Êä³öÁ÷È¥¡£
fcloseº¯Êý£º
int fclose (FILE *stream);
¹Ø±ÕÖ¸¶¨µÄÎļþÁ÷stream£¬Ê¹ËùÒÔÉÐδд³öµÄÊý¾Ý¶¼Ð´µ½ÎļþÀïÈ¥¡£


fflushº¯Êý£º
#include
int fflush(FILE *stream);
°ÑÎļþÁ÷ÀïµÄÏÖÓÐÊý¾ÝÁ¢¿ÌдÈëÎļþ¡£

fseekº¯Êý£º
int fseek(FILE *stream, long int offset, int whence);
ΪÏÂÒ»´Î¶Áд²Ù×÷ÉèÖÃÆ«ÒÆλÖá£

fgetc,getc,getcharº¯Êý£º
#include
int fgetc(FILE *stream); ´ÓÎļþÁ÷ÀïÈ¡³öÏÂÒ»¸ö×Ö½Ú²¢°ÑËüµ±×öÒ»¸ö×Ö·û·µ»Ø¡£µ±Ëü´ïµ½Îļþβ»ò³öÏÖ´íÎóʱ£¬·µ»ØEOF¡£
int getc (FILE *stream); ×÷ÓÃÏ൱ÓÚfgetc,µ«ÎÒÃÇ¿ÉÒÔÔÚstream²ÎÊý²»ÔÊÐíÓи±×÷ÓõÄÇé¿öÏ°ÑgetcʵÏÖΪһ¸öºê¡£
int getchar(); Ï൱getc(stdin),

fputc,putc,putcharº¯Êý£º
int fputc(int c, FILE *stream); °ÑÒ»¸ö×Ö·ûдµ½Êä³öÎļþÁ÷À·µ»ØֵΪ¸ÕдµÄÄǸöÖµ£¬Ê§°Ü·µ»ØEOF¡£
int putc(int c, FILE *stream); Ï൱ÓÚfputc£¬µ«¿ÉÒÔʵÏÖΪһ¸öºê¡£
int putchar(int c); Ï൱putc(stdout);

fgets,getsº¯Êý£º
char* fgets(char *s,int n, FILE *stream);´ÓÊäÈëÎļþÁ÷¶ÁÈ¡n-1¸ö×Ö·û¼ÓÉÏ"\0"дµ½sÖ¸ÏòµÄ×Ö·û´®È¥¡£
char* gets(char *s);¶Ô´«ÊäµÄ×Ö·û¸öÊýûÏÞÖÆ£¬±ÜÃâʹÓã¡

printf ,scanf µÈÌø¹ý¡£

chmod£¬chown ÂÔ¡£

unlink,link ,symlinkϵͳµ÷Óãº
#include

int unlink (const char *path);
int link(const char *path1, const char *path2);
int symlink(const char *path1, const char *path2);

mkdir,rmdirϵͳµ÷Óãº
#include
int mkdir (const char *path, mode_t mode);
int rmdir (const char *path);


chdir,getcwdϵͳµ÷Óãº
#include
int chdir(const char *path);¸Ä±ä¹¤×÷Ŀ¼¡£
int *getcwd(char *buf,sizo_t size);°Ñµ±Ç°×ÓĿ¼µÄÃû×Öдµ½¸ø¶¨µÄBUFÀï¡£

ɨÃè×ÓĿ¼º¯Êý£º
Óë×ÓĿ¼Ïà¹ØµÄº¯ÊýÊÇÔÚÒ»¸öÃûΪdirent.hµÄÍ·ÎļþÀï±»ÉêÃ÷µÄ¡£ËüÃÇʹÓÃÒ»¸öÃûΪDIRµÄ½á¹¹×öΪ×ÓĿ¼´¦Àí²Ù×÷µÄ»ù´¡¡£¡°×ÓĿ¼Á÷¡±(directory stream)Ö¸ÏòDIR½á¹¹µÄÖ¸Õ룬±»ÓÃÀ´Íê³É¸÷ÖÖÆÕͨµÄ×ÓĿ¼²Ù×÷¡£

opendir£¬readdirº¯Êý£º
#include
#include
DIR * opendir(const char *name); ´ò¿ªÒ»¸ö×ÓĿ¼²¢½¨Á¢Ò»¸ö×ÓĿ¼Á÷¡£
struct dirent *readdir(DIR *dirp); ·µ»ØÒ»¸ö½á¹¹ÌåÖ¸Õ룬½á¹¹ÌåÀï±£´æ×Å×ÓĿ¼Á÷dirpÖÐÏÂÒ»¸öĿ¼Êý¾ÝÏîµÄÓйØ×ÊÁÏ¡£

telldirº¯Êý£º
long int telldir (DIR *dirp); ·µ»ØÖµÀï¼Ç¼×Å×ÓĿ¼Á÷ÀïµÄµ±Ç°Î»Ö᣿ÉÔÚseekdirµ÷ÓÃÀïÀûÓÃÕâ¸öÖµ¶Ôµ±Ç°Î»ÖÃÔÙ×öÒ»´Î×ÓĿ¼ɨÃè¡£

seekdirº¯Êý£º
void seekdir(DIR *dirp, long int loc);¶ÔdirpÖ¸¶¨µÄ×ÓĿ¼Á÷ÖеÄĿ¼Êý¾ÝÏîµÄÖ¸Õë½øÐÐÉèÖã¬locµÄÖµÓÃÀ´ÉèÖÃÖ¸ÕëµÄλÖá£

closedirº¯Êý£º
int closedir(DIR *dirp);¹Ø±ÕÒ»¸ö×ÓĿ¼²¢ÊÍ·ÅÓëÖ®Ïà¹ØÁªµÄ×ÊÔ´¡£

mmapº¯Êý£º½«´´½¨Ò»¸öÖ¸ÏòÒ»¶ÎÄÚ´æµÄÖ¸Õ룬¸ÃÖ¸Õ뽫Óëͨ¹ýÒ»¸ö´ò¿ªµÄÎļþÃèÊö·ûÀ´·ÃÎʵÄÎļþµÄÄÚÈÝÏà¹ØÁª¡£
#include
void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
Óô«µÝoff²ÎÊýµÄ°ì·¨¿ÉÒԸı侭¹²ÏíÄÚ´æ¶Î·ÃÎʵÄÎļþÖÐÊý¾ÝµÄÆðʼƫÒÆÖµ¡£

msyncº¯Êý£º°Ñ¸ÃÄÚ´æ¶ÎµÄij¸ö²¿·Ö»òÕû¶ÎÖеÄÐÞ¸Äд»Øµ½±»Ó³ÉäÎļþÀ£¨»òÕß´Ó±»Ó³ÉäµÄÎļþÀï¶Á³ö£©
#include
int msync(void *addr, size_t len, int flags);

munmapº¯Êý£ºÊÍ·ÅÄÚ´æ¶Î
int munmap(void *addr, size_t len);
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ