ÄúµÄλÖãºÍøÕ¾Ê×Ò³ > µçÆ÷άÐÞ×ÊÁÏÍø > ÕýÎÄ >
FORK£¨£©º¯ÊýµÄÀí½â
À´Ô´£º ÈÕÆÚ£º2013-11-29 9:19:25 ÈËÆø£º±êÇ©£º
·µ»ØÖµ£º
¸ºÊý£ºÈç¹û³ö´í£¬Ôòfork()·µ»Ø-1,´ËʱûÓд´½¨ÐµĽø³Ì¡£ ³õµÄ½ø³ÌÈÔÈ»ÔËÐС£
Á㣺ÔÚ×Ó½ø³ÌÖУ¬fork()·µ»Ø0
ÕýÊý£ºÔÚ¸º½ø³ÌÖУ¬fork()·µ»ØÕýµÄ×Ó½ø³ÌµÄpid
Æä´ÎÎÒÃÇÀ´¿´ÏÂÈçºÎÀûÓÃfork´´½¨×Ó½ø³Ì¡£
´´½¨×Ó½ø³ÌµÄÑù°å´úÂëÈçÏÂËùʾ£º
pid_t child;
if((child = fork())<0)
/*´íÎó´¦Àí*/
else if(child == 0)
/*ÕâÊÇнø³Ì*/
else
/*ÕâÊÇ ³õµÄ¸¸½ø³Ì*/
fockº¯Êýµ÷ÓÃÒ»´ÎÈ´·µ»ØÁ½´Î;Ïò¸¸½ø³Ì·µ»Ø×Ó½ø³ÌµÄid£¬Ïò×Ó½ø³ÌÖзµ»Ø0£¬
ÕâÊÇÒòΪ¸¸½ø³Ì¿ÉÄÜ´æÔںܶà¹ý×Ó½ø³Ì£¬ËùÒÔ±ØÐëͨ¹ýÕâ¸ö·µ»ØµÄ×Ó½ø³ÌidÀ´¸ú×Ù×Ó½ø³Ì£¬
¶ø×Ó½ø³ÌÖ»ÓÐÒ»¸ö¸¸½ø³Ì£¬ËûµÄid¿ÉÒÔͨ¹ýgetppidÈ¡µÃ¡£
ÏÂÃæÎÒÃÇÀ´¶Ô±ÈÒ»ÏÂÁ½¸öÀý×Ó£º
µÚÒ»¸ö£º
#include
#include
int main()
{
pid_t pid;
int count=0;
pid = fork();
printf( "this is first time, pid = %dn", pid );
printf( "this is second time, pid = %dn", pid );
count++;
printf( "count = %dn", count );
if ( pid>0 )
{
printf( "this is the parent process,the child has the pid:%dn", pid );
}
else if ( !pid )
{
printf( "this is the child process.n")
}
else
{
printf( "fork failed.n" );
}
printf( "this is third time, pid = %dn", pid );
printf( "this is fouth time, pid = %dn", pid );
return 0;
}
ÔËÐнá¹ûÈçÏ£º

ÎÊÌ⣺
Õâ¸ö½á¹ûºÜÆæ¹ÖÁË£¬ÎªÊ²Ã´printfµÄÓï¾äÖ´ÐÐÁ½´Î£¬¶øÄǾä“count++;”µÄÓï¾äÈ´Ö»Ö´ÐÐÁËÒ»´Î
½Ó×Å¿´£º
#include
#include
int main(void)
{
pid_t pid;
int count=0;
pid = fork();
printf( "now, the pid returned by calling fork() is %dn", pid );
if ( pid>0 )
{
printf( "this is the parent process,the child has the pid:%dn", pid );
printf( "in the parent process,count = %dn", count );
}
else if ( !pid )
{
printf( "this is the child process.n");
printf( "do your own things here.n" );
count ++;
printf( "in the child process, count = %dn", count );
}
else
{
printf( "fork failed.n" );
}
return 0;
}
ÔËÐнá¹ûÈçÏ£º
ÏÖÔÚÀ´½âÊÍÉÏÃæÌá³öµÄÎÊÌâ¡£
¿´Õâ¸ö³ÌÐòµÄʱºò£¬Í·ÄÔÖбØÐëÊ×ÏÈÁ˽âÒ»¸ö¸ÅÄÔÚÓï¾äpid=fork()֮ǰ£¬Ö»ÓÐÒ»¸ö½ø³ÌÔÚÖ´ÐÐÕâ¶Î´úÂ룬µ«ÔÚÕâÌõÓï¾äÖ®ºó£¬¾Í±ä³ÉÁ½¸ö½ø³ÌÔÚÖ´ÐÐÁË£¬ÕâÁ½¸ö½ø³ÌµÄ´úÂ벿·ÖÍêÈ«Ïàͬ£¬½«ÒªÖ´ÐеÄÏÂÒ»ÌõÓï¾ä¶¼ÊÇif ( pid>0 )……¡£
Á½¸ö½ø³ÌÖУ¬ÔÏȾʹæÔÚµÄÄǸö±»³Æ×÷“¸¸½ø³Ì”£¬Ð³öÏÖµÄÄǸö±»³Æ×÷“×Ó½ø³Ì”¡£¸¸×Ó½ø³ÌµÄÇø±ð³ýÁ˽ø³Ì±êÖ¾·û(process id)²»Í¬Í⣬±äÁ¿pidµÄÖµÒ²²»Ïàͬ£¬pid´æ·ÅµÄÊÇforkµÄ·µ»ØÖµ¡£forkµ÷ÓõÄÒ»¸öÆæÃîÖ®´¦¾ÍÊÇËü½ö½ö±»µ÷ÓÃÒ»´Î£¬È´Äܹ»·µ»ØÁ½´Î£¬Ëü¿ÉÄÜÓÐÈýÖÖ²»Í¬µÄ·µ»ØÖµ£º
1. ÔÚ¸¸½ø³ÌÖУ¬fork·µ»ØÐ´´½¨×Ó½ø³ÌµÄ½ø³Ìid;
2.ÔÚ×Ó½ø³ÌÖУ¬fork·µ»Ø0;
3.Èç¹û³öÏÖ´íÎó£¬fork·µ»ØÒ»¸ö¸ºÖµ;
fork³ö´í¿ÉÄÜÓÐÁ½ÖÖÔÒò£º(1)µ±Ç°µÄ½ø³ÌÊýÒѾ´ïµ½ÁËϵͳ¹æ¶¨µÄÉÏÏÞ£¬ÕâʱerrnoµÄÖµ±»ÉèÖÃΪeagain¡£(2)ϵͳÄÚ´æ²»×㣬ÕâʱerrnoµÄÖµ±»ÉèÖÃΪenomem¡£
½ÓÏÂÀ´ÎÒÃÇÀ´¿´¿´apue2ÖжÔforkµÄ˵Ã÷£º
the new process created by fork is called the child process. this function is called once but returns twice. the only difference in the returns is that the return value in the child is 0, whereas the return value in the parent is the process id of the new child. the reason the child's process id is returned to the parent is that a process can have more than one child, and there is no function that allows a process to o^ain the process ids of its children. the reason fork returns 0 to the child is that a process can have only a single parent, and the child can always call getppid to o^ain the process id of its parent. (process id 0 is reserved for use by the kernel, so it's not possible for 0 to be the process id of a child.)
±»fork´´½¨µÄнø³Ì½Ð×ö×Ô½ø³Ì¡£forkº¯Êý±»µ÷ÓÃÒ»´Î£¬È´Á½´Î·µ»Ø¡£·µ»ØÖµÎ¨Ò»µÄÇø±ðÊÇÔÚ×Ó½ø³ÌÖзµ»Ø0£¬¶øÔÚ¸¸½ø³ÌÖзµ»Ø×Ó½ø³ÌµÄpid¡£ÔÚ¸¸½ø³ÌÖÐÒª·µ»Ø×Ó½ø³ÌµÄpidµÄÔÒòÊǸ¸½ø³Ì¿ÉÄÜÓв»Ö¹Ò»¸ö×Ó½ø³Ì£¬¶øÒ»¸ö½ø³ÌÓÖûÓÐÈκκ¯Êý¿ÉÒԵõ½ËûµÄ×Ó½ø³ÌµÄpid¡£
both the child and the parent continue executing with the instruction that follows the call to fork. the child is a copy of the parent. for example, the child gets a copy of the parent's data space, heap, and stack. note that this is a copy for the child; the parent and the child do not share these portions of memory. the parent and the child share the text segment (section 7.6).
- 1
- 2
- ÏÂÒ»Ò³
¡¾¿´¿´ÕâÆªÎÄÕÂÔڰٶȵÄÊÕ¼Çé¿ö¡¿
Ïà¹ØÎÄÕÂ
- ÉÏһƪ£º ¹âÏ˻Á¬½ÓÆ÷µÄÖÖÀà
- ÏÂһƪ£º PADSÖÐBGA FanoutÉȳö½Ì³Ì