Ïà¹Øº¯Êý signal£¬sleep
±íÍ·Îļþ #include
¶¨Ò庯Êý unsigned int alarm(unsigned int seconds);
º¯Êý˵Ã÷ alarm()ÓÃÀ´ÉèÖÃÐźÅSIGALRMÔÚ¾¹ý²ÎÊýsecondsÖ¸¶¨µÄÃëÊýºó´«Ë͸øÄ¿Ç°µÄ½ø³Ì¡£Èç¹û²ÎÊýseconds Ϊ0£¬Ôò֮ǰÉèÖõÄÄÖÖӻᱻȡÏû£¬²¢½«Ê£ÏµÄʱ¼ä·µ»Ø¡£
·µ»ØÖµ ·µ»Ø֮ǰÄÖÖÓµÄÊ£ÓàÃëÊý£¬Èç¹û֮ǰδÉèÄÖÖÓÔò·µ»Ø0¡£
·¶Àý #include
#include
void handler() {
printf(¡°hello\n¡±);
}
main()
{
int i;
signal(SIGALRM,handler);
alarm(5);
for(i=1;i<7;i++){
printf(¡°sleep %d ...\n¡±,i);
sleep(1);
}
}
Ö´ÐÐ sleep 1 ...
sleep 2 ...
sleep 3 ...
sleep 4 ...
sleep 5 ...
hello
sleep 6 ...
¡¡
kill£¨´«ËÍÐźŸøÖ¸¶¨µÄ½ø³Ì£©
Ïà¹Øº¯Êý raise£¬signal
±íÍ·Îļþ #include
#include
¶¨Ò庯Êý int kill(pid_t pid,int sig);
º¯Êý˵Ã÷ kill()¿ÉÒÔÓÃÀ´ËͲÎÊýsigÖ¸¶¨µÄÐźŸø²ÎÊýpidÖ¸¶¨µÄ½ø³Ì¡£²ÎÊýpidÓм¸ÖÖÇé¿ö:
pid>0 ½«ÐźŴ«¸ø½ø³Ìʶ±ðÂëΪpid µÄ½ø³Ì¡£
pid=0 ½«ÐźŴ«¸øºÍÄ¿Ç°½ø³ÌÏàͬ½ø³Ì×éµÄËùÓнø³Ì
pid=-1 ½«ÐźŹ㲥´«Ë͸øϵͳÄÚËùÓеĽø³Ì
pid<0 ½«ÐźŴ«¸ø½ø³Ì×éʶ±ðÂëΪpid¾ø¶ÔÖµµÄËùÓнø³Ì
²ÎÊýsig´ú±íµÄÐźűàºÅ¿É²Î¿¼¸½Â¼D
·µ»ØÖµ Ö´Ðгɹ¦Ôò·µ»Ø0£¬Èç¹ûÓдíÎóÔò·µ»Ø-1¡£
´íÎó´úÂë EINVAL ²ÎÊýsig ²»ºÏ·¨
ESRCH ²ÎÊýpid ËùÖ¸¶¨µÄ½ø³Ì»ò½ø³Ì×é²»´æÔÚ
EPERM ȨÏÞ²»¹»ÎÞ·¨´«ËÍÐźŸøÖ¸¶¨½ø³Ì
·¶Àý #include
#include
#include
#include
main()
{
pid_t pid;
int status;
if(!(pid= fork())){
printf(¡°Hi I am child process!\n¡±);
sleep(10);
return;
}
else{
printf(¡°send signal to child process (%d) \n¡±,pid);
sleep(1);
kill(pid ,SIGABRT);
wait(&status);
if(WIFSIGNALED(status))
printf(¡°chile process receive signal %d\n¡±,WTERMSIG(status));
}
}
Ö´ÐÐ sen signal to child process(3170)
Hi I am child process!
child process receive signal 6
¡¡
pause£¨Èýø³ÌÔÝÍ£Ö±µ½ÐźųöÏÖ£©
Ïà¹Øº¯Êý kill£¬signal£¬sleep
±íÍ·Îļþ #include
¶¨Ò庯Êý int pause(void);
º¯Êý˵Ã÷ pause()»áÁîÄ¿Ç°µÄ½ø³ÌÔÝÍ££¨½øÈë˯Ãß״̬£©£¬Ö±µ½±»ÐźÅ(signal)ËùÖжϡ£
·µ»ØÖµ Ö»·µ»Ø-1¡£
´íÎó´úÂë EINTR ÓÐÐźŵ½´ïÖжÏÁ˴˺¯Êý¡£
¡¡
sigaction£¨²éѯ»òÉèÖÃÐźŴ¦Àí·½Ê½£©
Ïà¹Øº¯Êý signal£¬sigprocmask£¬sigpending£¬sigsuspend
±íÍ·Îļþ #include
¶¨Ò庯Êý int sigaction(int signum,const struct sigaction *act ,struct sigaction *oldact);
º¯Êý˵Ã÷ sigaction()»áÒÀ²ÎÊýsignumÖ¸¶¨µÄÐźűàºÅÀ´ÉèÖøÃÐźŵĴ¦Àíº¯Êý¡£²ÎÊýsignum¿ÉÒÔÖ¸¶¨SIGKILLºÍSIGSTOPÒÔÍâµÄËùÓÐÐźš£
Èç²ÎÊý½á¹¹sigaction¶¨ÒåÈçÏÂ
struct sigaction
{
void (*sa_handler) (int);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer) (void);
}
sa_handler´Ë²ÎÊýºÍsignal()µÄ²ÎÊýhandlerÏàͬ£¬´ú±íеÄÐźŴ¦Àíº¯Êý£¬ÆäËûÒâÒåÇë²Î¿¼signal()¡£
sa_mask ÓÃÀ´ÉèÖÃÔÚ´¦Àí¸ÃÐźÅʱÔÝʱ½«sa_mask Ö¸¶¨µÄÐźŸéÖá£
sa_restorer ´Ë²ÎÊýûÓÐʹÓá£
sa_flags ÓÃÀ´ÉèÖÃÐźŴ¦ÀíµÄÆäËûÏà¹Ø²Ù×÷£¬ÏÂÁеÄÊýÖµ¿ÉÓá£
OR ÔËË㣨|£©×éºÏ
A_NOCLDSTOP : Èç¹û²ÎÊýsignumΪSIGCHLD£¬Ôòµ±×Ó½ø³ÌÔÝͣʱ²¢²»»á֪ͨ¸¸½ø³Ì
SA_ONESHOT/SA_RESETHAND:µ±µ÷ÓÃеÄÐźŴ¦Àíº¯ÊýÇ°£¬½«´ËÐźŴ¦Àí·½Ê½¸ÄΪϵͳԤÉèµÄ·½Ê½¡£
SA_RESTART:±»ÐźÅÖжϵÄϵͳµ÷Óûá×ÔÐÐÖØÆô
SA_NOMASK/SA_NODEFER:ÔÚ´¦Àí´ËÐźÅδ½áÊøÇ°²»Àí»á´ËÐźŵÄÔٴε½À´¡£
Èç¹û²ÎÊýoldact²»ÊÇNULLÖ¸Õ룬ÔòÔÀ´µÄÐźŴ¦Àí·½Ê½»áÓɴ˽ṹsigaction ·µ»Ø¡£
·µ»ØÖµ Ö´Ðгɹ¦Ôò·µ»Ø0£¬Èç¹ûÓдíÎóÔò·µ»Ø-1¡£
´íÎó´úÂë EINVAL ²ÎÊýsignum ²»ºÏ·¨£¬ »òÊÇÆóͼÀ¹½ØSIGKILL/SIGSTOPSIGKILLÐźÅ
EFAULT ²ÎÊýact£¬oldactÖ¸ÕëµØÖ·ÎÞ·¨´æÈ¡¡£
EINTR ´Ëµ÷Óñ»ÖжÏ
·¶Àý #include
#include
void show_handler(struct sigaction * act)
{
switch (act->sa_flags)
{
case SIG_DFL:printf(¡°Default action\n¡±);break;
case SIG_IGN:printf(¡°Ignore the signal\n¡±);break;
default: printf(¡°0x%x\n¡±,act->sa_handler);
}
}
main()
{
int i;
struct sigaction act,oldact;
act.sa_handler = show_handler;
act.sa_flags = SA_ONESHOT|SA_NOMASK;
sigaction(SIGUSR1,&act,&oldact);
for(i=5;i<15;i++)
{
printf(¡°sa_handler of signal %2d =¡±.i);
sigaction(i,NULL,&oldact);
}
}
Ö´ÐÐ sa_handler of signal 5 = Default action
sa_handler of signal 6= Default action
sa_handler of signal 7 = Default action
sa_handler of signal 8 = Default action
sa_handler of signal 9 = Default action
sa_handler of signal 10 = 0x8048400
sa_handler of signal 11 = Default action
sa_handler of signal 12 = Default action
sa_handler of signal 13 = Default action
sa_handler of signal 14 = Default action
¡¡
sigaddset£¨Ôö¼ÓÒ»¸öÐźÅÖÁÐźż¯£©
Ïà¹Øº¯Êý sigemptyset£¬sigfillset£¬sigdelset£¬sigismember
±íÍ·Îļþ #include
¶¨Ò庯Êý int sigaddset(sigset_t *set,int signum);
º¯Êý˵Ã÷ sigaddset()ÓÃÀ´½«²ÎÊýsignum ´ú±íµÄÐźżÓÈëÖÁ²ÎÊýset Ðźż¯Àï¡£
·µ»ØÖµ Ö´Ðгɹ¦Ôò·µ»Ø0£¬Èç¹ûÓдíÎóÔò·µ»Ø-1¡£
´íÎó´úÂë EFAULT ²ÎÊýsetÖ¸ÕëµØÖ·ÎÞ·¨´æÈ¡
EINVAL ²ÎÊýsignum·ÇºÏ·¨µÄÐźűàºÅ
¡¡
sigdelset£¨´ÓÐźż¯Àïɾ³ýÒ»¸öÐźţ©
Ïà¹Øº¯Êý sigemptyset£¬sigfillset£¬sigaddset£¬sigismember
±íÍ·Îļþ #include
¶¨Ò庯Êý int sigdelset(sigset_t * set,int signum);
º¯Êý˵Ã÷ sigdelset()ÓÃÀ´½«²ÎÊýsignum´ú±íµÄÐźŴӲÎÊýsetÐźż¯Àïɾ³ý¡£
·µ»ØÖµ Ö´Ðгɹ¦Ôò·µ»Ø0£¬Èç¹ûÓдíÎóÔò·µ»Ø-1¡£
´íÎó´úÂë EFAULT ²ÎÊýsetÖ¸ÕëµØÖ·ÎÞ·¨´æÈ¡
EINVAL ²ÎÊýsignum·ÇºÏ·¨µÄÐźűàºÅ
¡¡
sigemptyset£¨³õʼ»¯Ðźż¯£©
Ïà¹Øº¯Êý sigaddset£¬sigfillset£¬sigdelset£¬sigismember
±íÍ·Îļþ #include
¶¨Ò庯Êý int sigemptyset(sigset_t *set);
º¯Êý˵Ã÷ sigemptyset()ÓÃÀ´½«²ÎÊýsetÐźż¯³õʼ»¯²¢Çå¿Õ¡£
·µ»ØÖµ Ö´Ðгɹ¦Ôò·µ»Ø0£¬Èç¹ûÓдíÎóÔò·µ»Ø-1¡£
´íÎó´úÂë EFAULT ²ÎÊýsetÖ¸ÕëµØÖ·ÎÞ·¨´æÈ¡
¡¡
sigfillset£¨½«ËùÓÐÐźżÓÈëÖÁÐźż¯£©
Ïà¹Øº¯Êý sigempty£¬sigaddset£¬sigdelset£¬sigismember
±íÍ·Îļþ #include
¶¨Ò庯Êý int sigfillset(sigset_t * set);
º¯Êý˵Ã÷ sigfillset()ÓÃÀ´½«²ÎÊýsetÐźż¯³õʼ»¯£¬È»ºó°ÑËùÓеÄÐźżÓÈëµ½´ËÐźż¯Àï¡£
·µ»ØÖµ Ö´Ðгɹ¦Ôò·µ»Ø0£¬Èç¹ûÓдíÎóÔò·µ»Ø-1¡£
¸½¼Ó˵Ã÷ EFAULT ²ÎÊýsetÖ¸ÕëµØÖ·ÎÞ·¨´æÈ¡
¡¡
sigismember£¨²âÊÔij¸öÐźÅÊÇ·ñÒѼÓÈëÖÁÐźż¯À
Ïà¹Øº¯Êý sigemptyset£¬sigfillset£¬sigaddset£¬sigdelset
±íÍ·Îļþ #include
¶¨Ò庯Êý int sigismember(const sigset_t *set,int signum);
º¯Êý˵Ã÷ sigismember()ÓÃÀ´²âÊÔ²ÎÊýsignum ´ú±íµÄÐźÅÊÇ·ñÒѼÓÈëÖÁ²ÎÊýsetÐźż¯Àï¡£Èç¹ûÐźż¯ÀïÒÑÓиÃÐźÅÔò·µ»Ø1£¬·ñÔò·µ»Ø0¡£
·µ»ØÖµ Ðźż¯ÒÑÓиÃÐźÅÔò·µ»Ø1£¬Ã»ÓÐÔò·µ»Ø0¡£Èç¹ûÓдíÎóÔò·µ»Ø-1¡£
´íÎó´úÂë EFAULT ²ÎÊýsetÖ¸ÕëµØÖ·ÎÞ·¨´æÈ¡
EINVAL ²ÎÊýsignum ·ÇºÏ·¨µÄÐźűàºÅ
¡¡
signal£¨ÉèÖÃÐźŴ¦Àí·½Ê½£©
Ïà¹Øº¯Êý sigaction£¬kill£¬raise
±íÍ·Îļþ #include
¶¨Ò庯Êý void (*signal(int signum,void(* handler)(int)))(int);
º¯Êý˵Ã÷ signal()»áÒÀ²ÎÊýsignum Ö¸¶¨µÄÐźűàºÅÀ´ÉèÖøÃÐźŵĴ¦Àíº¯Êý¡£µ±Ö¸¶¨µÄÐźŵ½´ïʱ¾Í»áÌøתµ½²ÎÊýhandlerÖ¸¶¨µÄº¯ÊýÖ´ÐС£Èç¹û²ÎÊýhandler²»ÊǺ¯ÊýÖ¸Õ룬Ôò±ØÐëÊÇÏÂÁÐÁ½¸ö³£ÊýÖ®Ò»:
SIG_IGN ºöÂÔ²ÎÊýsignumÖ¸¶¨µÄÐźš£
SIG_DFL ½«²ÎÊýsignum Ö¸¶¨µÄÐźÅÖØÉèΪºËÐÄÔ¤ÉèµÄÐźŴ¦Àí·½Ê½¡£
¹ØÓÚÐźŵıàºÅºÍ˵Ã÷£¬Çë²Î¿¼¸½Â¼D
·µ»ØÖµ ·µ»ØÏÈÇ°µÄÐźŴ¦Àíº¯ÊýÖ¸Õ룬Èç¹ûÓдíÎóÔò·µ»ØSIG_ERR(-1)¡£
¸½¼Ó˵Ã÷ ÔÚÐźŷ¢ÉúÌøתµ½×Ô¶¨µÄhandler´¦Àíº¯ÊýÖ´Ðкó£¬ÏµÍ³»á×Ô¶¯½«´Ë´¦Àíº¯Êý»»»ØÔÀ´ÏµÍ³Ô¤ÉèµÄ´¦Àí·½Ê½£¬Èç¹ûÒª¸Ä±ä´Ë²Ù×÷Çë¸ÄÓÃsigaction()¡£
·¶Àý ²Î¿¼alarm()»òraise()¡£
¡¡
sigpending£¨²éѯ±»¸éÖõÄÐźţ©
Ïà¹Øº¯Êý signal£¬sigaction£¬sigprocmask£¬sigsuspend
±íÍ·Îļþ #include
¶¨Ò庯Êý int sigpending(sigset_t *set);
º¯Êý˵Ã÷ sigpending()»á½«±»¸éÖõÄÐźż¯ºÏÓɲÎÊýsetÖ¸Õë·µ»Ø¡£
·µ»ØÖµÖ´ Ðгɹ¦Ôò·µ»Ø0£¬Èç¹ûÓдíÎóÔò·µ»Ø-1¡£
´íÎó´úÂë EFAULT ²ÎÊýsetÖ¸ÕëµØÖ·ÎÞ·¨´æÈ¡
EINTR ´Ëµ÷Óñ»Öжϡ£
¡¡
sigprocmask£¨²éѯ»òÉèÖÃÐźÅÕÚÕÖ£©
Ïà¹Øº¯Êý signal£¬sigaction£¬sigpending£¬sigsuspend
±íÍ·Îļþ #include
¶¨Ò庯Êý int sigprocmask(int how,const sigset_t *set,sigset_t * oldset);
º¯Êý˵Ã÷ sigprocmask()¿ÉÒÔÓÃÀ´¸Ä±äÄ¿Ç°µÄÐźÅÕÚÕÖ£¬Æä²Ù×÷ÒÀ²ÎÊýhowÀ´¾ö¶¨
SIG_BLOCK еÄÐźÅÕÚÕÖÓÉÄ¿Ç°µÄÐźÅÕÚÕֺͲÎÊýset Ö¸¶¨µÄÐźÅÕÚÕÖ×÷Áª¼¯
SIG_UNBLOCK ½«Ä¿Ç°µÄÐźÅÕÚÕÖɾ³ýµô²ÎÊýsetÖ¸¶¨µÄÐźÅÕÚÕÖ
SIG_SETMASK ½«Ä¿Ç°µÄÐźÅÕÚÕÖÉè³É²ÎÊýsetÖ¸¶¨µÄÐźÅÕÚÕÖ¡£
Èç¹û²ÎÊýoldset²»ÊÇNULLÖ¸Õ룬ÄÇôĿǰµÄÐźÅÕÚÕÖ»áÓÉ´ËÖ¸Õë·µ»Ø¡£
·µ»ØÖµ Ö´Ðгɹ¦Ôò·µ»Ø0£¬Èç¹ûÓдíÎóÔò·µ»Ø-1¡£
´íÎó´úÂë EFAULT ²ÎÊýset£¬oldsetÖ¸ÕëµØÖ·ÎÞ·¨´æÈ¡¡£
EINTR ´Ëµ÷Óñ»ÖжÏ
¡¡
sleep£¨Èýø³ÌÔÝÍ£Ö´ÐÐÒ»¶Îʱ¼ä£©
Ïà¹Øº¯Êý signal£¬alarm
±íÍ·Îļþ #include
¶¨Ò庯Êý unsigned int sleep(unsigned int seconds);
º¯Êý˵Ã÷ sleep()»áÁîÄ¿Ç°µÄ½ø³ÌÔÝÍ££¬Ö±µ½´ïµ½²ÎÊýseconds ËùÖ¸¶¨µÄʱ¼ä£¬»òÊDZ»ÐźÅËùÖжϡ£
·µ»ØÖµ Èô½ø³ÌÔÝÍ£µ½²ÎÊýseconds ËùÖ¸¶¨µÄʱ¼äÔò·µ»Ø0£¬ÈôÓÐÐźÅÖжÏÔò·µ»ØÊ£ÓàÃëÊý¡£
¡¡
ferror£¨¼ì²éÎļþÁ÷ÊÇ·ñÓдíÎó·¢Éú£©
Ïà¹Øº¯Êý clearerr£¬perror
±íÍ·Îļþ #include
¶¨Ò庯Êý int ferror(FILE *stream);
º¯Êý˵Ã÷ ferror()ÓÃÀ´¼ì²é²ÎÊýstreamËùÖ¸¶¨µÄÎļþÁ÷ÊÇ·ñ·¢ÉúÁË´íÎóÇé¿ö£¬ÈçÓдíÎó·¢ÉúÔò·µ»Ø·Ç0Öµ¡£
·µ»ØÖµ Èç¹ûÎļþÁ÷ÓдíÎó·¢ÉúÔò·µ»Ø·Ç0Öµ¡£
¡¡
perror£¨´òÓ¡³ö´íÎóÔÒòÐÅÏ¢×Ö·û´®£©
Ïà¹Øº¯Êý strerror
±íÍ·Îļþ #include
¶¨Ò庯Êý void perror(const char *s);
º¯Êý˵Ã÷ perror()ÓÃÀ´½«ÉÏÒ»¸öº¯Êý·¢Éú´íÎóµÄÔÒòÊä³öµ½±ê×¼´íÎó(stderr)¡£²ÎÊýsËùÖ¸µÄ×Ö·û´®»áÏÈ´òÓ¡³ö£¬ºóÃæÔÙ¼ÓÉÏ´íÎóÔÒò×Ö·û´®¡£´Ë´íÎóÔÒòÒÀÕÕÈ«¾Ö±äÁ¿errnoµÄÖµÀ´¾ö¶¨ÒªÊä³öµÄ×Ö·û´®¡£
·µ»ØÖµ
·¶Àý #include
main()
{
FILE *fp;
fp = fopen(¡°/tmp/noexist¡±,¡±r+¡±);
if(fp = =NULL) perror(¡°fopen¡±);
}
Ö´ÐÐ $ ./perror
fopen : No such file or diretory
¡¡
strerror£¨·µ»Ø´íÎóÔÒòµÄÃèÊö×Ö·û´®£©
Ïà¹Øº¯Êý perror
±íÍ·Îļþ #include
¶¨Ò庯Êý char * strerror(int errnum);
º¯Êý˵Ã÷ strerror()ÓÃÀ´ÒÀ²ÎÊýerrnumµÄ´íÎó´úÂëÀ´²éѯÆä´íÎóÔÒòµÄÃèÊö×Ö·û´®£¬È»ºó½«¸Ã×Ö·û´®Ö¸Õë·µ»Ø¡£
·µ»ØÖµ ·µ»ØÃèÊö´íÎóÔÒòµÄ×Ö·û´®Ö¸Õë¡£
·¶Àý /* ÏÔʾ´íÎó´úÂë0 ÖÁ9 µÄ´íÎóÔÒòÃèÊö*/
#include
main()
{
int i;
for(i=0;i<10;i++)
printf(¡°%d : %s\n¡±,i,strerror(i));
}
Ö´ÐÐ 0 : Success
1 : Operation not permitted
2 : No such file or directory
3 : No such process
4 : Interrupted system call
5 : Input/output error
6 : Device not configured
7 : Argument list too long
8 : Exec format error
9 : Bad file descriptor
¡¡
mkfifo£¨½¨Á¢¾ßÃû¹ÜµÀ£©
Ïà¹Øº¯Êý pipe£¬popen£¬open£¬umask
±íÍ·Îļþ #include
#include
¶¨Ò庯Êý int mkfifo(const char * pathname,mode_t mode);
º¯Êý˵Ã÷ mkfifo()»áÒÀ²ÎÊýpathname½¨Á¢ÌØÊâµÄFIFOÎļþ£¬¸ÃÎļþ±ØÐë²»´æÔÚ£¬¶ø²ÎÊýmodeΪ¸ÃÎļþµÄȨÏÞ£¨mode%~umask£©£¬Òò´ËumaskÖµÒ²»áÓ°Ïìµ½FIFOÎļþµÄȨÏÞ¡£Mkfifo()½¨Á¢µÄFIFOÎļþÆäËû½ø³Ì¶¼¿ÉÒÔÓöÁдһ°ãÎļþµÄ·½Ê½´æÈ¡¡£µ±Ê¹ÓÃopen()À´´ò¿ªFIFOÎļþʱ£¬O_NONBLOCKÆì±ê»áÓÐÓ°Ïì
1¡¢µ±Ê¹ÓÃO_NONBLOCK Æì±êʱ£¬´ò¿ªFIFO ÎļþÀ´¶ÁÈ¡µÄ²Ù×÷»áÁ¢¿Ì·µ»Ø£¬µ«ÊÇÈô»¹Ã»ÓÐÆäËû½ø³Ì´ò¿ªFIFO ÎļþÀ´¶ÁÈ¡£¬ÔòдÈëµÄ²Ù×÷»á·µ»ØENXIO ´íÎó´úÂë¡£
2¡¢Ã»ÓÐʹÓÃO_NONBLOCK Æì±êʱ£¬´ò¿ªFIFO À´¶ÁÈ¡µÄ²Ù×÷»áµÈµ½ÆäËû½ø³Ì´ò¿ªFIFOÎļþÀ´Ð´Èë²ÅÕý³£·µ»Ø¡£Í¬ÑùµØ£¬´ò¿ªFIFOÎļþÀ´Ð´ÈëµÄ²Ù×÷»áµÈµ½ÆäËû½ø³Ì´ò¿ªFIFO ÎļþÀ´¶ÁÈ¡ºó²ÅÕý³£·µ»Ø¡£
·µ»ØÖµ Èô³É¹¦Ôò·µ»Ø0£¬·ñÔò·µ»Ø-1£¬´íÎóÔÒò´æÓÚerrnoÖС£
´íÎó´úÂë EACCESS ²ÎÊýpathnameËùÖ¸¶¨µÄĿ¼·¾¶ÎÞ¿ÉÖ´ÐеÄȨÏÞ
EEXIST ²ÎÊýpathnameËùÖ¸¶¨µÄÎļþÒÑ´æÔÚ¡£
ENAMETOOLONG ²ÎÊýpathnameµÄ·¾¶Ãû³ÆÌ«³¤¡£
ENOENT ²ÎÊýpathname°üº¬µÄĿ¼²»´æÔÚ
ENOSPC ÎļþϵͳµÄÊ£Óà¿Õ¼ä²»×ã
ENOTDIR ²ÎÊýpathname·¾¶ÖеÄĿ¼´æÔÚµ«È´·ÇÕæÕýµÄĿ¼¡£
EROFS ²ÎÊýpathnameÖ¸¶¨µÄÎļþ´æÔÚÓÚÖ»¶ÁÎļþϵͳÄÚ¡£
·¶Àý #include
#include
#include
main()
{
char buffer[80];
int fd;
unlink(FIFO);
mkfifo(FIFO,0666);
if(fork()>0){
char s[ ] = ¡°hello!\n¡±;
fd = open (FIFO,O_WRONLY);
write(fd,s,sizeof(s));
close(fd);
}
else{
fd= open(FIFO,O_RDONLY);
read(fd,buffer,80);
printf(¡°%s¡±,buffer);
close(fd);
}
}
Ö´ÐÐ hello!
¡¡
pclose£¨¹Ø±Õ¹ÜµÀI/O£©
Ïà¹Øº¯Êý popen
±íÍ·Îļþ #include
¶¨Ò庯Êý int pclose(FILE * stream);
º¯Êý˵Ã÷ pclose()ÓÃÀ´¹Ø±ÕÓÉpopenËù½¨Á¢µÄ¹ÜµÀ¼°ÎļþÖ¸Õë¡£²ÎÊýstreamΪÏÈÇ°ÓÉpopen()Ëù·µ»ØµÄÎļþÖ¸Õë¡£
·µ»ØÖµ ·µ»Ø×Ó½ø³ÌµÄ½áÊø״̬¡£Èç¹ûÓдíÎóÔò·µ»Ø-1£¬´íÎóÔÒò´æÓÚerrnoÖС£
´íÎó´úÂë ECHILD pclose()ÎÞ·¨È¡µÃ×Ó½ø³ÌµÄ½áÊø״̬¡£
·¶Àý ²Î¿¼popen()¡£
¡¡
pipe£¨½¨Á¢¹ÜµÀ£©
Ïà¹Øº¯Êý mkfifo£¬popen£¬read£¬write£¬fork
±íÍ·Îļþ #include
¶¨Ò庯Êý int pipe(int filedes[2]);
º¯Êý˵Ã÷ pipe()»á½¨Á¢¹ÜµÀ£¬²¢½«ÎļþÃèÊö´ÊÓɲÎÊýfiledesÊý×é·µ»Ø¡£filedes[0]Ϊ¹ÜµÀÀïµÄ¶ÁÈ¡¶Ë£¬filedes[1]ÔòΪ¹ÜµÀµÄдÈë¶Ë¡£
·µ»ØÖµ Èô³É¹¦Ôò·µ»ØÁ㣬·ñÔò·µ»Ø-1£¬´íÎóÔÒò´æÓÚerrnoÖС£
´íÎó´úÂë EMFILE ½ø³ÌÒÑÓÃÍêÎļþÃèÊö´Ê×î´óÁ¿¡£
ENFILE ϵͳÒÑÎÞÎļþÃèÊö´Ê¿ÉÓá£
EFAULT ²ÎÊýfiledesÊý×éµØÖ·²»ºÏ·¨¡£
·¶Àý /* ¸¸½ø³Ì½è¹ÜµÀ½«×Ö·û´®¡°hello!\n¡±´«¸ø×Ó½ø³Ì²¢ÏÔʾ*/
#include
main()
{
int filedes[2];
char buffer[80];
pipe(filedes);
if(fork()>0){
/* ¸¸½ø³Ì*/
char s[ ] = ¡°hello!\n¡±;
write(filedes[1],s,sizeof(s));
}
else{
/*×Ó½ø³Ì*/
read(filedes[0],buffer,80);
printf(¡°%s¡±,buffer);
}
}
Ö´ÐÐ hello!
¡¡
popen£¨½¨Á¢¹ÜµÀI/O£©
Ïà¹Øº¯Êý pipe£¬mkfifo£¬pclose£¬fork£¬system£¬fopen
±íÍ·Îļþ #include
¶¨Ò庯Êý FILE * popen( const char * command,const char * type);
º¯Êý˵Ã÷ popen()»áµ÷ÓÃfork()²úÉú×Ó½ø³Ì£¬È»ºó´Ó×Ó½ø³ÌÖе÷ÓÃ/bin/sh -cÀ´Ö´ÐвÎÊýcommandµÄÖ¸Áî¡£²ÎÊýtype¿ÉʹÓá°r¡±´ú±í¶ÁÈ¡£¬¡°w¡±´ú±íдÈë¡£ÒÀÕÕ´ËtypeÖµ£¬popen()»á½¨Á¢¹ÜµÀÁ¬µ½×Ó½ø³ÌµÄ±ê×¼Êä³öÉ豸»ò±ê×¼ÊäÈëÉ豸£¬È»ºó·µ»ØÒ»¸öÎļþÖ¸Õë¡£Ëæºó½ø³Ì±ã¿ÉÀûÓôËÎļþÖ¸ÕëÀ´¶ÁÈ¡×Ó½ø³ÌµÄÊä³öÉ豸»òÊÇдÈëµ½×Ó½ø³ÌµÄ±ê×¼ÊäÈëÉ豸ÖС£´ËÍ⣬ËùÓÐʹÓÃÎļþÖ¸Õë(FILE*)²Ù×÷µÄº¯ÊýÒ²¶¼¿ÉÒÔʹÓ㬳ýÁËfclose()ÒÔÍâ¡£
·µ»ØÖµ Èô³É¹¦Ôò·µ»ØÎļþÖ¸Õ룬·ñÔò·µ»ØNULL£¬´íÎóÔÒò´æÓÚerrnoÖС£
´íÎó´úÂë EINVAL²ÎÊýtype²»ºÏ·¨¡£
×¢ÒâÊÂÏî ÔÚ±àд¾ßSUID/SGIDȨÏ޵ijÌÐòʱÇ뾡Á¿±ÜÃâʹÓÃpopen()£¬popen()»á¼Ì³Ð»·¾³±äÁ¿£¬Í¨¹ý»·¾³±äÁ¿¿ÉÄÜ»áÔì³Éϵͳ°²È«µÄÎÊÌâ¡£
·¶Àý #include
main()
{
FILE * fp;
char buffer[80];
fp=popen(¡°cat /etc/passwd¡±,¡±r¡±);
fgets(buffer,sizeof(buffer),fp);
printf(¡°%s¡±,buffer);
pclose(fp);
}
Ö´ÐÐ root :x:0 0: root: /root: /bin/bash
220.160.165.* ÓÚ 2006-10-24 18:29:27·¢±í:
ÄãµÄÍ·ÎļþÔõô²»Ð´³öÀ´