LinuxϵĹܵÀ±à³Ì¼¼Êõ dupº¯ÊýºÍdup2º¯Êý
dupºÍdup2Ò²ÊÇÁ½¸ö·Ç³£ÓÐÓõĵ÷Óã¬ËüÃǵÄ×÷Óö¼ÊÇÓÃÀ´¸´ÖÆÒ»¸öÎļþµÄÃèÊö·û¡£ËüÃǾ³£ÓÃÀ´Öض¨Ïò½ø³ÌµÄstdin¡¢stdoutºÍstderr¡£ÕâÁ½¸öº¯ÊýµÄ ÔÐÎÈçÏ£º
#include <unistd.h>
int dup( int oldfd );
int dup2( int oldfd, int targetfd )
ÀûÓú¯Êýdup£¬ÎÒÃÇ¿ÉÒÔ¸´ÖÆÒ»¸öÃèÊö·û¡£´«¸ø¸Ãº¯ÊýÒ»¸ö¼ÈÓеÄÃèÊö·û£¬Ëü¾Í»á·µ»ØÒ»¸öеÄÃèÊö·û£¬Õâ¸öеÄÃèÊö·ûÊÇ´«¸øËüµÄÃèÊö·ûµÄ¿½±´¡£ÕâÒâζ×Å£¬ÕâÁ½¸öÃèÊö·û¹²Ïíͬһ¸öÊý¾Ý½á¹¹
¡£ÀýÈ磬Èç¹ûÎÒÃǶÔÒ»¸öÎļþÃèÊö·ûÖ´ÐÐlseek²Ù×÷£¬µÃµ½µÄµÚÒ»¸öÎļþµÄλÖú͵ڶþ¸öÊÇÒ»ÑùµÄ¡£ÏÂÃæÊÇÓÃÀ´ËµÃ÷dupº¯ÊýʹÓ÷½·¨µÄ´úÂëƬ¶Î£º
int fd1, fd2;
...
fd2 = dup( fd1 );
ÐèҪעÒâµÄÊÇ£¬ÎÒÃÇ¿ÉÒÔÔÚµ÷ÓÃfork֮ǰ½¨Á¢Ò»¸öÃèÊö·û£¬ÕâÓëµ÷ÓÃdup½¨Á¢ÃèÊö·ûµÄЧ¹ûÊÇÒ»ÑùµÄ£¬×Ó½ø³ÌҲͬÑù»áÊÕµ½Ò»¸ö¸´ÖƳöÀ´µÄÃèÊö·û¡£
dup2º¯Êý¸údupº¯ÊýÏàËÆ£¬µ«dup2º¯ÊýÔÊÐíµ÷ÓÃÕ߹涨һ¸öÓÐЧÃèÊö·ûºÍÄ¿±êÃèÊö·ûµÄid¡£dup2º¯Êý³É¹¦·µ»Øʱ£¬Ä¿±êÃèÊö·û£¨dup2º¯ÊýµÄµÚ
¶þ¸ö²ÎÊý£©½«±ä³ÉÔ´ÃèÊö·û£¨dup2º¯ÊýµÄµÚÒ»¸ö²ÎÊý£©µÄ¸´ÖÆÆ·£¬»»¾ä»°Ëµ£¬Á½¸öÎļþÃèÊö·ûÏÖÔÚ¶¼Ö¸Ïòͬһ¸öÎļþ£¬²¢ÇÒÊǺ¯ÊýµÚÒ»¸ö²ÎÊýÖ¸ÏòµÄÎļþ¡£ÏÂÃæ
ÎÒÃÇÓÃÒ»¶Î´úÂë¼ÓÒÔ˵Ã÷£º
int oldfd;
oldfd = open("app_log", (O_RDWR | O_CREATE), 0644 );
dup2( oldfd, 1 );
close( oldfd );
±¾ÀýÖУ¬ÎÒÃÇ´ò¿ªÁËÒ»¸öÐÂÎļþ£¬³ÆΪ“app_log”£¬²¢ÊÕµ½Ò»¸öÎļþÃèÊö·û£¬¸ÃÃèÊö·û½Ð×öfd1¡£ÎÒÃǵ÷ÓÃdup2º¯Êý£¬²ÎÊýΪoldfdºÍ1£¬Õâ»á
µ¼ÖÂÓÃÎÒÃÇдò¿ªµÄÎļþÃèÊö·ûÌæ»»µôÓÉ1´ú±íµÄÎļþÃèÊö·û£¨¼´stdout£¬ÒòΪ±ê×¼Êä³öÎļþµÄidΪ1£©¡£ÈκÎдµ½stdoutµÄ¶«Î÷£¬ÏÖÔÚ¶¼½«¸ÄΪд
ÈëÃûΪ“app_log”µÄÎļþÖС£ÐèҪעÒâµÄÊÇ£¬dup2º¯ÊýÔÚ¸´ÖÆÁËoldfdÖ®ºó£¬»áÁ¢¼´½«Æä¹Ø±Õ£¬µ«²»»á¹Øµôнü´ò¿ªµÄÎļþÃèÊö·û£¬ÒòΪÎļþÃèÊö
·û1ÏÖÔÚÒ²Ö¸ÏòËü¡£
ÏÂÃæÎÒÃǽéÉÜÒ»¸ö¸ü¼ÓÉîÈëµÄʾÀý´úÂë¡£»ØÒäһϱ¾ÎÄÇ°Ãæ½²µÄÃüÁîÐйܵÀ£¬ÔÚÄÇÀÎÒÃǽ«ls –1ÃüÁîµÄ±ê×¼Êä³ö×÷Ϊ±ê×¼ÊäÈëÁ¬½Óµ½wc –lÃüÁî¡£½ÓÏÂÀ´£¬ÎÒÃǾÍÓÃÒ»¸öC³ÌÐòÀ´¼ÓÒÔ˵Ã÷Õâ¸ö¹ý³ÌµÄʵÏÖ¡£´úÂëÈçÏÂÃæµÄʾÀý´úÂë3Ëùʾ¡£
ÔÚʾÀý´úÂë3ÖУ¬Ê×ÏÈÔÚµÚ9ÐдúÂëÖн¨Á¢Ò»¸ö¹ÜµÀ£¬È»ºó½«Ó¦ÓóÌÐò·Ö³ÉÁ½¸ö½ø³Ì£ºÒ»¸ö×Ó½
Ïà¹ØÎĵµ£º
¶ÔÓÚÓ²¼þ£¬ÓеÄÐÂÊÖµÜÐÖ¿ÉÄÜ»¹ÊDz»Ì«Ã÷°×ÔÚLinuxÏÂÇý¶¯Ó²¼þ£¬ÓÈÆäÔÚ¸Õ½Ó´¥Linuxʱ£¬Çý¶¯µÄ°²×°ÍùÍùÊÇ×îÁîÈËÍ·Í´µÄÒ»¼þÊÂÁË¡£
Ò»¡¢ ¶ÔϵͳÇý¶¯°²×°µÄÒ»µã³£Ê¶¡£
1£® ¿ª·¢¹¤¾ßÊDZØÐë°²×°£º
Èç¹ûÄúÊÇÐÂÊÖ£¬ÎÒ»¹Êǽ¨ÒéÄú°ÑϵͳÍêÈ«°²×°£¬ÒòΪÔÚ°²×°ÆäËüµÄÈí¼þ»áÓõ½Ò»Ð©£¬ÎÒÃDz»Ì«Ã÷°×µÄ¿âÎļþÒÔ¼°¿ª·¢¹¤¾ß¡£±ÈÈçmake gcc au ......
ÀûÓÃsocket±àдwindowsºÍlinuxͨѶ³ÌÐò
socket±à³Ì´ó¼Ò¶¼ºÜÊìϤ£¬ÎÒÔÚÕâÀï¾Í²»ÔÙÂÞàÂÁË¡£Ö±½ÓÇÐÈëÕýÌâ¡£¸Ã³ÌÐò·ÖÁ½²¿·Ö£¬server¶ËºÍclient¶Ë£¨Ë¶¼ÖªµÀ£©¡£ÆäÖÐserver¶Ë²ÉÓÃwindowsƽ̨£¬ÄÇlinuxƽ̨µ±È»ÊÇclient¶ËÁË¡£
ÕâÀïÊÇserver¶Ë³ÌÐò£º
/**********************************/
/* it work ......
´ÓÓû§´ò¿ªµçÔ´µ½Óû§¿ÉÒԵǼµÄÕâ¶Ì¶ÌµÄÒ»¶Îʱ¼äÄÚ£¬Red Hat Enterprise Linuxµ½µ×¶¼×÷ÁËÄÄЩÊÂÇ飬ֻÓÐÖªµÀÁËÕâЩÊÂÇ飬Óû§ÔÚÒÔºóµÄʹÓùý³ÌÖУ¬Èç¹û³öÏÖÁËһЩÎÊÌ⣬ÎÒÃÇ¿ÉÒÔ½èÖúÕâЩ¹ý³ÌÀ´ÎªÎÒÃÇÅųýһЩ¹ÊÕÏ¡£
Red Hat Enterprise LinuxÔÚµçÄÔµÄÆô¶¯½×¶Î£¬Ò»¹²¾ÀúÒÔÏÂÁ½¸ö½×¶Î£¬Èçͼ1£º
BIOS×Ô¼ì
µ±µçÄÔ¿ª»úµÄʱº ......
/*
* linux/kernel/floppy.c
*
* (C) 1991 Linus Torvalds
*/
/*
* 02.12.91 - Changed to static variables to indicate need for reset
* and recalibrate. This makes some things easier (output_byte reset
* checking etc), and means less i ......