杂谈Linux编程(二)

1. Linux中的记时
----获得当前时间
 =>    time 精度为秒
 =>    ftime精度毫秒
 =>    gettimeofday精度微秒
 =>    时间格式间相互转化的函数gmtime, ctime, asctime, mktime等。函数

----sleep
 =>    sleep 精度为秒
 =>    usleep精度为微秒
 =>    nanosleep精度为纳秒this

'man 7 time' for more infospa

2. 执行新程序,对fork-exec的封装(尽可能避免使用system())进程

int spawn (char* program, char** arg_list)
{
  pid_t child_pid;get

  /* Duplicate this process.  */
  child_pid = fork ();
  if (child_pid != 0)
    /* This is the parent process.  */
    return child_pid;
  else {
    /* Now execute PROGRAM, searching for it in the path.  */
    execvp (program, arg_list);
    /* The execvp function returns only if an error occurs.  */
    fprintf (stderr, "an error occurred in execvp\n");
    abort ();
  }
}it


3. 清除子进程
方法1:循环调用wait3和wait4,使用WNOBLOCK标志。
方法2:处理SIGCHLD信号,在此信号的handler中使用wait,从而获得子进程的状态,而且清除。
方法2比较好。io

相关文章
相关标签/搜索