linux½ø³Ì¿ØÖÆ£wait£¨£©
http://blog.chinaunix.net/u1/53053/showart_425197.html
#include <sys/types.h> /* ÌṩÀàÐÍpid_tµÄ¶¨Òå */
#include <sys/wait.h>
pid_t wait(int *status)
½ø³ÌÒ»µ©µ÷ÓÃÁËwait£¬¾ÍÁ¢¼´×èÈû×Ô¼º£¬ÓÉwait×Ô¶¯·ÖÎöÊÇ·ñµ±Ç°½ø³ÌµÄij¸ö×Ó½ø³ÌÒѾÍ˳ö£¬Èç¹ûÈÃËüÕÒµ½ÁËÕâÑùÒ»¸öÒѾ±ä³É½©Ê¬µÄ×Ó½ø³Ì£¬wait¾Í»áÊÕ¼¯Õâ¸ö×Ó½ø³ÌµÄÐÅÏ¢£¬²¢°ÑËü³¹µ×Ïú»Ùºó·µ»Ø£»Èç¹ûûÓÐÕÒµ½ÕâÑùÒ»¸ö×Ó½ø³Ì£¬wait¾Í»áÒ»Ö±×èÈûÔÚÕâÀֱµ½ÓÐÒ»¸ö³öÏÖΪֹ¡£
²ÎÊýstatusÓÃÀ´±£´æ±»ÊÕ¼¯½ø³ÌÍ˳öʱµÄһЩ״̬£¬ËüÊÇÒ»¸öÖ¸ÏòintÀàÐ͵ÄÖ¸Õë¡£µ«Èç¹ûÎÒÃǶÔÕâ¸ö×Ó½ø³ÌÊÇÈçºÎËÀµôµÄºÁ²»ÔÚÒ⣬ֻÏë°ÑÕâ¸ö½©Ê¬½ø³ÌÏûÃðµô£¬£¨ÊÂʵÉϾø´ó¶àÊýÇé¿öÏ£¬ÎÒÃǶ¼»áÕâÑùÏ룩£¬ÎÒÃǾͿÉÒÔÉ趨Õâ¸ö²ÎÊýΪNULL£¬¾ÍÏóÏÂÃæÕâÑù£º
pid = wait(NULL);
Èç¹û³É¹¦£¬wait»á·µ»Ø±»ÊÕ¼¯µÄ×Ó½ø³ÌµÄ½ø³ÌID£¬Èç¹ûµ÷Óýø³ÌûÓÐ×Ó½ø³Ì£¬µ÷Óþͻáʧ°Ü£¬´Ëʱwait·µ»Ø-1£¬Í¬Ê±errno±»ÖÃΪECHILD¡£
ÏÂÃæ¾ÍÈÃÎÒÃÇÓÃÒ»¸öÀý×ÓÀ´ÊµÕ½Ó¦ÓÃÒ»ÏÂwaitµ÷Óãº
/* wait1.c */
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
main()
{
pid_t pc,pr;
pc=fork();
if(pc<0) /* Èç¹û³ö´í */
printf("error ocurred!\n");
else if(pc==0){ /* Èç¹ûÊÇ×Ó½ø³Ì */
printf("This is child process with pid of %d\n",getpid());
sleep(10); /* ˯Ãß10ÃëÖÓ */
}
else{ /* Èç¹ûÊǸ¸½ø³Ì */
pr=wait(NULL); /* ÔÚÕâÀïµÈ´ý */
printf("I catched a child process with pid of %d\n"),pr);
}
exit(0);
}
±àÒë²¢ÔËÐÐ:
$ cc wait1.c -o wait1
$ ./wait1
This is child process with pid of 1508
I catched a child process with pid of 1508
¿ÉÒÔÃ÷ÏÔ×¢Òâµ½£¬ÔÚµÚ2Ðнá¹û´òÓ¡³öÀ´Ç°ÓÐ10 ÃëÖӵĵȴýʱ¼ä£¬Õâ¾ÍÊÇÎÒÃÇÉ趨µÄÈÃ×Ó½ø³Ì˯ÃßµÄʱ¼ä£¬Ö»ÓÐ×Ó½ø³Ì´Ó˯ÃßÖÐËÕÐѹýÀ´£¬Ëü²ÅÄÜÕý³£Í˳ö£¬Ò²¾Í²ÅÄܱ»¸¸½ø³Ì²¶×½µ½¡£ÆäʵÕâÀïÎÒÃDz»¹ÜÉ趨×Ó½ø³Ì˯ÃßµÄʱ¼äÓж೤£¬¸¸½ø³Ì¶¼»áÒ»Ö±µÈ´ýÏÂÈ¥£¬¶ÁÕßÈç¹ûÓÐÐËȤµÄ»°£¬¿ÉÒÔÊÔ×Å×Ô¼ºÐÞ¸ÄÒ»ÏÂÕâ¸öÊýÖµ£¬¿´¿´»á³öÏÖÔõÑùµÄ½á¹û¡£
²ÎÊýstatus£º
Èç¹û²ÎÊýstatusµÄÖµ²»ÊÇNULL£¬wait¾Í»á°Ñ×Ó½ø³ÌÍ˳öʱµÄ״̬ȡ³ö²¢´æÈëÆäÖУ¬ÕâÊÇÒ»¸öÕûÊýÖµ£¨int£©£¬Ö¸³öÁË×Ó½ø³ÌÊÇÕý³£Í˳ö»¹ÊDZ»·ÇÕý³£½áÊøµÄ£¨Ò»¸ö½ø³ÌÒ²¿ÉÒÔ±»ÆäËû½ø³ÌÓÃÐźŽáÊø£¬ÎÒÃǽ«ÔÚÒÔºóµÄÎÄ
Ïà¹ØÎĵµ£º
LinuxÍøÂçÇý¶¯³ÌÐò±àд(Ò»)
¹¤×÷ÐèҪдÁËÎÒÃǹ«Ë¾Ò»¿éÍø¿¨µÄLinuxÇý¶¯³ÌÐò¡£¾ÀúÒ»¸ö´ÓÎÞµ½ÓеĹý³Ì£¬Éî¸Ð¼¼Êõ½»Á÷µÄÖØÒª¡£Linux×÷ΪÌôս΢Èí¢¶ÏµÄÇ¿ÓÐÁ¦ÎäÆ÷£¬ÈÕÒæÊܵ½´ó¼ÒµÄϲ°®¡£ÕæÏ£ÍûËýÄÜÔÚÖйúѸËٳɳ¤¡£°Ñ³ÌÐòÎĵµÌù³öÀ´£¬Ï£ÍûºÍ´ó¼Ò̽ÌÖLinux¼¼ÊõºÍÓ¦Ó㬴ٽøLinuxÔÚÖйúµÄÆÕ¼°¡£
¡¡¡¡±¾ÎÄ¿ÉËæÒâ×ªÔØ£¬µ«Çë²»ÒªÔ ......
¶þ.LinuxÏµÍ³ÍøÂçÉ豸Çý¶¯³ÌÐò
¡¡¡¡2.1 ÍøÂçÇý¶¯³ÌÐòµÄ½á¹¹
¡¡¡¡ËùÓеÄLinuxÍøÂçÇý¶¯³ÌÐò×ñÑͨÓõĽӿڡ£Éè¼ÆÊ±²ÉÓõÄÊÇÃæÏò¶ÔÏóµÄ·½·¨¡£Ò»¸öÉ豸¾ÍÊÇÒ»¸ö¶ÔÏó(device ½á¹¹)£¬ËüÄÚ²¿ÓÐ×Ô¼ºµÄÊý¾ÝºÍ·½·¨¡£Ã¿Ò»¸öÉ豸µÄ·½·¨±»µ÷ÓÃʱµÄµÚÒ»¸ö²ÎÊý¶¼ÊÇÕâ¸öÉ豸¶ÔÏó±¾Éí¡£ÕâÑùÕâ¸ö·½·¨¾Í¿ÉÒÔ´æÈ¡×ÔÉíµÄÊý¾Ý(ÀàËÆÃæÏ ......
topÃüÁîºÍpsÃüÁîµÄ»ù±¾×÷ÓÃÊÇÏàͬµÄ£¬ÏÔʾϵͳµ±Ç°µÄ½ø³ÌºÍÆäËü×´¿ö£»µ«ÊÇtopÊÇÒ»¸ö¶¯Ì¬ÏÔʾ¹ý³Ì£¬¼´¿ÉÒÔͨ¹ýÓû§°´¼üÀ´²»¶Ïˢе±Ç°×´Ì¬¡£Èç¹ûÔÚǰִ̨ÐиÃÃüÁËü½«¶Àռǰ̨£¬Ö±µ½Óû§ÖÕÖ¹¸Ã³ÌÐòΪֹ¡£±È½Ï׼ȷµÄ˵£¬topÃüÁîÌṩÁËʵʱµÄ¶Ôϵͳ´¦ÀíÆ÷µÄ״̬¼àÊÓ¡£Ëü½«ÏÔʾϵͳÖÐCPU×î“Ãô¸Ð”µÄÈÎÎñÁÐ±í¡£¸ÃÃü ......
DWORD GetLocalAddr(char* interface)
{
struct ifreq ifr;
int fd;
int err;
unsigned long ip;
strcpy(ifr.ifr_name, interface);
fd = socket(AF_INET, SOCK_DGRAM, 0);
err = ioctl ......
http://blog.chinaunix.net/u1/53053/showart_425191.html
#include<stdlib.h>
void exit(int status);
²»ÏñforkÄÇôÄÑÀí½â£¬´ÓexitµÄÃû×Ö¾ÍÄÜ¿´³ö£¬Õâ¸öϵͳµ÷ÓÃÊÇÓÃÀ´ÖÕÖ¹Ò»¸ö½ø³ÌµÄ¡£ÎÞÂÛÔÚ³ÌÐòÖеÄʲôλÖã¬Ö»ÒªÖ´Ðе½exitϵͳµ÷Ó㬽ø³Ì¾Í»áֹͣʣϵÄËùÓвÙ×÷£¬Çå³ý°üÀ¨PCBÔÚÄڵĸ÷ÖÖÊý¾Ý½á¹¹£¬²¢ÖÕÖ¹±¾½ø³ ......