linux c 编程--进程控制

来源:互联网 发布:matlab数组累加 编辑:程序博客网 时间:2024/06/01 08:56

1、进程控制相关函数:

     fork、execl、execlp、execle、execv、execve 、execvp和clone;

     int clone(int (*fn)(void *), void *child_stack, int flags, void *arg);

     int sleep(unsigned int seconds);

     void _exit(int status);

     pid_t wait(int *status);

     pid_t waitpid(pid_t pid, int *status, int options);

2、实例代码:

    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);
    }

  运行输出:
  The variable is now 42
  File Read Error

原创粉丝点击