LinuxÖض¨ÏòÏê½â
תÔØ£ºhttp://www.cnblogs.com/hexapodsoft/archive/2007/04/24/724902.html
I/OÖض¨ÏòÏê½â¼°Ó¦ÓÃʵÀý
1¡¢ »ù±¾¸ÅÄÕâÊÇÀí½âºóÃæµÄ֪ʶµÄÇ°ÌᣬÇëÎñ±ØÀí½â£©
a¡¢ I/OÖض¨Ïòͨ³£Óë FDÓйأ¬shellµÄFDͨ³£Îª10¸ö£¬¼´ 0¡«9£»
b¡¢ ³£ÓÃFDÓÐ3¸ö£¬Îª0£¨stdin£¬±ê×¼ÊäÈ룩¡¢1£¨stdout£¬±ê×¼Êä³ö£©¡¢2£¨stderr£¬±ê×¼´íÎóÊä³ö£©£¬Ä¬ÈÏÓëkeyboard¡¢monitor¡¢monitorÓйأ»
c¡¢ Óà < À´¸Ä±ä¶Á½øµÄÊý¾ÝÐŵÀ(stdin)£¬Ê¹Ö®´ÓÖ¸¶¨µÄµµ°¸¶Á½ø£»
d¡¢ Óà > À´¸Ä±äËͳöµÄÊý¾ÝÐŵÀ(stdout, stderr)£¬Ê¹Ö®Êä³öµ½Ö¸¶¨µÄµµ°¸£»
e¡¢ 0 ÊÇ < µÄĬÈÏÖµ£¬Òò´Ë < Óë 0<ÊÇÒ»ÑùµÄ£»Í¬Àí£¬> Óë 1> ÊÇÒ»ÑùµÄ£»
f¡¢ ÔÚIOÖض¨Ïò ÖУ¬stdout Óë stderr µÄ¹ÜµÀ»áÏÈ×¼±¸ºÃ£¬²Å»á´Ó stdin ¶Á½ø×ÊÁÏ£»
g¡¢ ¹ÜµÀ“|”(pipe line):ÉÏÒ»¸öÃüÁîµÄ stdout ½Óµ½ÏÂÒ»¸öÃüÁîµÄ stdin;
h¡¢ tee ÃüÁîÊÇÔÚ²»Ó°ÏìÔ±¾ I/O µÄÇé¿öÏ£¬½« stdout ¸´ÖÆÒ»·Ýµ½µµ°¸È¥;
i¡¢ bash£¨ksh£©Ö´ÐÐÃüÁîµÄ¹ý³Ì£º·ÖÎöÃüÁäÁ¿ÇóÖµ£ÃüÁîÌæ´ú£¨``ºÍ$( )£©£Öض¨Ïò£Í¨Åä·ûÕ¹¿ª£È·¶¨Â·¾¶£Ö´ÐÐÃüÁ
j¡¢
( ) ½« command group ÖÃÓÚ sub-shell È¥Ö´ÐУ¬Ò²³Æ nested
sub-shell£¬ËüÓÐÒ»µã·Ç³£ÖØÒªµÄÌØÐÔÊÇ£º¼Ì³Ð¸¸shellµÄStandard input, output, and error plus
any other open file descriptors¡£
k¡¢ exec ÃüÁ³£ÓÃÀ´Ìæ´úµ±Ç° shell ²¢ÖØÐÂÆô¶¯Ò»¸ö shell£¬»»¾ä»°Ëµ£¬²¢Ã»ÓÐÆô¶¯×Ó shell¡£Ê¹ÓÃÕâÒ»ÃüÁîʱÈκÎÏÖÓл·¾³¶¼½«»á±»Çå³ý¡£exec ÔÚ¶ÔÎļþÃèÊö·û½øÐвÙ×÷µÄʱºò£¬Ò²Ö»ÓÐÔÚÕâʱ£¬exec ²»»á¸²¸ÇÄ㵱ǰµÄ shell »·¾³¡£
2¡¢ »ù±¾IO
cmd > file °Ñ stdout Öض¨Ïòµ½ file ÎļþÖУ»
cmd >> file °Ñ stdout Öض¨Ïòµ½ file ÎļþÖÐ(×·¼Ó)£»
cmd 1> fiel °Ñ stdout Öض¨Ïòµ½ file ÎļþÖУ»
cmd > file 2>&1 °Ñ stdout ºÍ stderr Ò»ÆðÖض¨Ïòµ½ file ÎļþÖУ»
cmd 2> file °Ñ stderr Öض¨Ïòµ½ file ÎļþÖУ»
cmd 2>> file °Ñ stderr Öض¨Ïòµ½ file ÎļþÖÐ(×·¼Ó)£»
cmd >> file 2>&1 °Ñ stderr ºÍ stderr Ò»ÆðÖض¨Ïòµ½ file ÎļþÖÐ(×·¼Ó)£»
cmd < file >file2 cmd ÃüÁîÒÔ file Îļþ×÷Ϊ stdin£¬ÒÔ file2 Îļþ×÷Ϊ stdout£»
cat <>file ÒÔ¶ÁдµÄ·½Ê½´ò¿ª file£»
cmd < file cmd ÃüÁîÒÔ file Îļþ×÷Ϊ stdin£»
cmd << delimiter Here document£¬´Ó stdin ÖжÁÈ룬ֱÖÁ
Ïà¹ØÎĵµ£º
1. LinusºÍBill
ËƺõÔÚÒ»Ò¹Ö®¼ä£¬Õâ¸öÃû×ÖͻȻ±äµÃͬÏó±È¶û¡£¸Ç´ÄÒ»ÑùµÄ¶úÊìÄÜÏê¡£µ«ÊDZȶû¡£¸Ç´Ä£¬ÄÄÅÂÉí¼ÛÓÐÊý²»Ç屶µÄ°ÙÍòÃÀ½ð£¬Ò²ÓÀÔ¶²»»á±ä³ÉLinux Torvalds¡£Õâλ29ËêµÄ·ÒÀ¼ÈË£¬¼òµ¥¶øÇ¿´óµÄLinux²Ù×÷ϵͳµÄ´´ÔìÕߣ¬³¬Ô½Á˸ǴĵÄÉñ»°¡£Ò²Óд«ÎÅ˵¸Ç´ÄÊÇÒ»¸ö׿ԽµÄ³ÌÐòÔ±£¬µ«LinusÊÇ»õÕæ¼ÛʵµÄ¸ßÊ ......
stroul£¬
strdup
snprintf()
atio
CÖг£ÓÃ×Ö·û´®²Ù×÷º¯Êý
#include <string.h>
size_t strlen(const char *s) ²âÁ¿×Ö·û´®³¤¶ÈsµÄʵ¼Ê³¤¶È¡£
ÀýÈçs[20]="abc",ÄÇôstrlen(s)µÄ½á¹ûÊÇ3£¬¶ø²»ÊÇ20.Õâ¾ÍÊÇʵ¼Ê³¤¶È
char *strcat(const char *s1, const *s2) ......
Linux ÏûÏ¢Öض¨Ïò°ì·¨
command > filename
°Ñ±ê×¼Êä³öÖض¨Ïòµ½Ò»¸öÖ¸¶¨ÎļþÖÐ
command 1 > fielname
°Ñ±ê×¼Êä³öÖض¨Ïòµ½Ò»¸öÖ¸¶¨ÎļþÖÐ(ͬÉÏ)
command 2 > filename
°Ñ±ê×¼´íÎóÖض¨Ïòµ½Ò»¸öÎļþÖÐ
command >! filename
°Ñ±ê×¼Êä³öÖض¨Ïòµ½Ò»¸ö
Ö¸¶¨
ÎļþÖУ¬ÈôÎļþ´æÔÚ£¬Ôò¸²¸Ç
command >&am ......
/*
* ¸ÃÎļþʵÏÖϵͳµ÷ÓÃread£¬writeºÍlseek¡£
*/
/*
* linux/fs/read_write.c
*
* (C) 1991 Linus Torvalds
*/
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include <linux/kernel.h>
#include < ......
/*
* linux/fs/char_dev.c
*
* (C) 1991 Linus Torvalds
*/
#include <errno.h>
#include <sys/types.h> // ¶¨ÒåÁË»ù±¾µÄϵͳÊý¾ÝÀàÐÍ
#include <linux/sched.h>
#include <linux/kernel.h> // º¬ÓÐһЩÄں˳£Óú¯ÊýµÄÔÐζ¨Òå
#include < ......