process process

网友投稿 642 2022-09-23 18:55:00

process process

process processing

1. creat process --->  fork()

#include

#include

pid_t fork(void)

fork() creates a child process,  it is arbitrary for once calling and twice return.

in parent process:

return pid of child process

in child process:

return 0

2. communication between parent and child ---> wait() & waitpid()

#include

#include

pid_t wait(int *status);

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

3. exec() family --->execv()

#include

extern char **environ;

int execl(const char *path, const char *arg, ...);

int execlp(const char *file, const char *arg, ...);

int execle(const char *path, const char *arg, ...,

char * const envp[] );

int execv(const char *path, char *const argv[]);

int execvp(const char *file, char *const argv[]);

Note: l -> list,  v -> vector (must end with (char*)0 )

these three ones always combine together, such as followings:

refer to :服务器进程的两次fork()

0.int

1.main(void)   2.{   3.    pid_t        pid;   4.   5.    if ( (pid = fork()) < 0)   6.          err_sys("fork error");   7.    else if (pid == 0)    8.        {                /* first child */   9.           if ( (pid = fork()) < 0)   10.                        err_sys("fork error");   11.           else if (pid > 0)   12.                 exit(0);        /* parent from second fork == first child */   13.   14.                /* We're the second child; our parent becomes init as soon  15.                   as our real parent calls exit() in the statement above.  16.                   Here's where we'd continue executing, knowing that when  17.                   we're done, init will reap our status. */   18.   19.            sleep(2);   20.            printf("second child, parent pid = %d\n", getppid());   21.            exit(0);   22.        }   23.   24.    if (waitpid(pid, NULL, 0) != pid)        /* wait for first child */   25.            err_sys("waitpid error");   26.   27.        /* We're the parent (the original process); we continue executing,  28.           knowing that we're not the parent of the second child. */   29.   30.    exit(0);   31.}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:爆冷!王中王围棋争霸赛柯洁首轮遭淘汰!(围棋新闻 新浪竞技风暴 新浪网柯洁)
下一篇:中国Unix/Linux软件开发联盟门户网站 正式上线
相关文章