| 
		    
                    
   clone 
  clone是Linux2.0以后才具备的新功能,它较fork更强(可认为fork是clone要实现的一部分),可以使得创建的子进程共享父进程的资源,并且要使用此函数必须在编译内核时设置clone_actually_works_ok选项。 
  clone函数的原型为: 
 
    
        
            | int clone(int (*fn)(void *), void *child_stack, int flags, void *arg); | 
         
    
 
  此函数返回创建进程的PID,函数中的flags标志用于设置创建子进程时的相关选项。 
  来看下面的例子: 
 
    
        
            int variable, fd;
  int do_something() {  variable = 42;  close(fd);  _exit(0); }
  int main(int argc, char *argv[]) {  void **child_stack;  char tempch;
   variable = 9;  fd = open("test.file", O_RDONLY);  child_stack = (void **) malloc(16384);  printf("The variable was %d\n", variable);
   clone(do_something, child_stack, CLONE_VM|CLONE_FILES, NULL);  sleep(1); /* 延时以便子进程完成关闭文件操作、修改变量 */
   printf("The variable is now %d\n", variable);  if (read(fd, &tempch, 1) < 1) {   perror("File Read Error");   exit(1);  }  printf("We could read from the file\n");  return 0; } | 
         
    
 
  运行输出: 
 
    
        
            The variable is now 42 File Read Error | 
         
    
 
  程序的输出结果告诉我们,子进程将文件关闭并将变量修改(调用clone时用到的CLONE_VM、CLONE_FILES标志将使得变量和文件描述符表被共享),父进程随即就感觉到了,这就是clone的特点。 
  sleep 
  函数调用sleep可以用来使进程挂起指定的秒数,该函数的原型为: 
 
    
        
            | unsigned int sleep(unsigned int seconds); | 
         
    
 
  该函数调用使得进程挂起一个指定的时间,如果指定挂起的时间到了,该调用返回0;如果该函数调用被信号所打断,则返回剩余挂起的时间数(指定的时间减去已经挂起的时间)。 
  exit 
  系统调用exit的功能是终止本进程,其函数原型为: 
 
  _exit会立即终止发出调用的进程,所有属于该进程的文件描述符都关闭。参数status作为退出的状态值返回父进程,在父进程中通过系统调用wait可获得此值。 
		    
                      
		      
		      
		   |