дÁËÁ½¸ö³ÌÐò1.cºÍ2.c
1.cÖÐvforkÁËÒ»¸ö×Ó½ø³Ì£¬ÔÚ×Ó½ø³ÌÖÐʹÓÃexeclpÈ¥Ö´ÐÐ2.c
Ï£ÍûʵÏÖµÄÖ´ÐÐ˳ÐòÊǸ¸½ø³Ì-->×Ó½ø³Ì-->2.c-->¸¸½ø³Ì
½á¹ûÈ´ÊÇ·¢ÏÖʵ¼ÊµÄÖ´ÐÐ˳ÐòÊǸ¸½ø³Ì-->×Ó½ø³Ì-->¸¸½ø³Ì£»×îºó²ÅÊÇ2.cÖ´ÐÐÍê³É (Õû¸ö1.cÖ´ÐÐÍê³Éºó£¬execlp²ÅÖ´ÐÐÍê³É¡£)
Çë¸ßÊÖÃÇ°ïæ¿´¿´£¬Ó¦¸ÃÔõÑùÐ޸ģ¿Ð»Ð»£¡
ÒÔÏÂÊÇÔ´´úÂ룺
[2.c]
#include
main()
{
printf("2.c %ld\n",getpid());
}
[1.c]
#include
#include
int main(int argc,char ** argv)
{
int child;
printf("ID PID F/S PID\n"); // (F/S:Father/Son)
printf("--------------------------------------\n");
printf("father %ld\n",getpid());
if(!(child=vfork()))
{
printf("son %ld %ld\n",getpid(),getppid());
execlp("./2","./2",NULL);
//exit(1);
}
else
printf("father %ld\n",getpid());
}
ʵ¼ÊÖ´ÐеĽá¹ûÊÇ:
[abc@linux:/home/abc]./1
ID PID F/S PID
---------------------------------------
father 727
son 728 727
father 727 728
[abc@linux:/home/abc]2.c 728
ÆÚÍûµÄÖ´Ðнá¹ûÊÇ
[abc@linux:/home/abc]./1
ID PID P/S PID
---------------------------------------
father 727
son 728 727
2.c 728
father 727 728
[abc@linux:/home/abc]
(ºÇºÇ£¬Êµ¼ÊÉÏÎÒÏëҪʵÏֵŦÄܾÍÊÇsystemº¯Êý£¬µ«±Èsystemº¯ÊýÒª¶àÒ»µã¹¦ÄÜ£¬¾ÍÊÇÒªÖªµÀsystemº¯ÊýÔÚforkʱ´´½¨µÄ×Ó½ø³ÌPID,ÒÔÏò×Ó³ÌÐò´«µÝµ÷ÓòÎÊý)
stop ÓÚ 2009-09-11 02:12:45·¢±í:
лл£¡
ubuntulover ÓÚ 2009-09-10 21:48:44·¢±í:
ÎÒÊÔÁË¡£°´ÎÒÉÏÃæ˵µÄ¾ÍÐС£
ubuntulover ÓÚ 2009-09-10 21:44:20·¢±í:
ÓÃfork£¬È»ºó¸¸½ø³ÌÖе÷ÓÃwaitµÈ´ý×Ô½ø³ÌÏÈÍê³É¾ÍÐÐÁË£¨ÎҲµģ¬Äã¿ÉÊÔÊÔ£©