fork and exec

来源:互联网 发布:上海市行知小学照片 编辑:程序博客网 时间:2024/06/07 18:48

程序分歧fork() 
 
   
  fork()
会产生一个与父程序相同的子程序,唯一不同之处在於其
process 
   
  id(pid)
  
 
   
  
如果我们要撰写守护神程序,或是例如网路伺服器,需要多个行程来同时提供
 
   
  
多个连线,可以利用fork()来产生多个相同的行程。
  
 
   
  
函数宣告
 
 
   
  pid_t fork(void);  
   
  pid_t vfork(void);  
 
   
  
返回值

 
   
  -1 : 
失败。
  
   
    0 : 
子程序。
  
   
  >0 : 
将子程序的process id传回给父程序。
  
 
   
  
Linuxfork()vfork()是相同的东西。
  
 
   
  
范例一
: fork.c 
 
   
  
在这个范例中,我们示范fork()的标准用法。
  
 
   
  #include <stdio.h>  
   
  #include <stdlib.h>  
   
  #include <unistd.h>  
 
   
  void main(void)  
   
  {  
   
    pid_t pid;  
 
   
    printf("hello/n");  
   
    pid = fork();  
 
   
    switch (pid) {  
   
      case -1: printf("failure!/n"); break;  
   
      case  0: printf("I am child!/n"); break;  
   
      default: printf("my child is %d/n",pid); break;  
   
    }  
   
    for (;;) { /* do something here */ }  
   
  }  
 
   
  
编译

 
   
  gcc -o ex1 fork.c  
 
   
  
执行结果

 
   
  ./ex1 &  
 
   
  hello  
   
  my child is 8650  
   
  I am child!  
 
   
  
我们可以见到,使用fork(),可将一个程序分岐成两个。在分歧之前的程序码
 
   
  
只执行一次。
  
 
   
  
检验行程

 
   
  ps | grep ex1  
 
   
   8649  p0 R    0:40 ./ex1  
   
   8650  p0 R    0:40 ./ex1  
 
   
  8649
是父程序的pid8650则为子程序的pid
  
   
  
您会需要用到"killall ex1"来杀掉两个行程。
  
 
   
  
范例二
: daemon.c 
 
   
  
UNIX中,我们一般都利用fork(),来实作所谓的"守护神程序",也就是DOS
 
   
  
所谓的"常驻程序"。一般的技巧是将父程序结束,而子程序便成为"守护神"
  
 
   
  
这个范例中,示范一般标准的daemon写法。
  
 
   
  #include <stdio.h>  
   
  #include <stdlib.h>  
   
  #include <unistd.h>  
 
   
  void main(void)  
   
  {  
   
    pid_t pid;  
 
   
    pid = fork();  
 
   
    if (pid>0) {  
   
      printf("daemon on duty!/n");  
   
      exit(0);  
   
    } else  
   
    if (pid<0) {  
   
      printf("Can't fork!/n");  
   
      exit(-1);  
   
    }  
 
   
    for (;;) {  
   
      printf("I am the daemon!/n");  
   
      sleep(3);  
   
      /* do something your own here */  
   
    }  
 
   
  }  
 
   
  
编译

 
   
  gcc -o ex2 daemon.c  
 
   
  
执行结果

 
   
  ./ex2  
 
   
  daemon on duty!  
   
  I am the daemon!  
   
  
接下来每三秒钟,都会出现一个"I am the daemon!"的讯息,这表示您的程序
 
   
  
已经"长驻"在系统中了。
  
 
   
  
检验行程

 
   
  ps | grep ex2  
 
   
  8753  p0 S    0:00 ./ex2  
 
   
  
注意到在范例一中,我们下的指令为"./ex1 &",而在范例二中为"./ex2",没
 
   
  
"&"符号。
  
   

 

  范例三:Perl Script

 my @subpid;
 my $rtpid = $$;
 foreach my $file (@file){
  chomp $file;
  my($file, $dir, $ext) = fileparse($file);
  if (1 == 1) {
   my $exec_cmd = "$cmp_cmd  ${rsyncTgtDir}${file} ${rsyncTmpDir}${file}";
   my $pid = fork();
   if ($pid == 0){
    setpgrp(0, $rtpid);
    exec($exec_cmd);
    print "fail to execute $exec_cmd/n";
    exit(1);
   }
   else{
    push(@subpid, $pid);
   }
  }
 }
 if($$ == $rtpid ){
  foreach my $pid (@subpid){
   if(getpgrp($pid) == $rtpid ){
    waitpid($pid, 0);
   }
  }
 }

 

原创粉丝点击