linux fork创建新进程

    科技2024-06-11  82

    man 2 fork

    使用fork创建和当前进程一模一样的进程,叫子进程原来的进程叫父进程

    pid_t fork(void);

    无输入参数,执行成功后返回子进程pid给父进程,返回0 给子进程,当内存不够或者pid号用尽的时候执行失败,此时返回-1

    一些提示点(来自man):

    * The child has its own unique process ID, and this PID does not match the ID of any existing process group (setpgid(2)) or session. * The child's parent process ID is the same as the parent's process ID. * The child does not inherit its parent's memory locks (mlock(2), mlockall(2)). * Process resource utilizations (getrusage(2)) and CPU time counters(times(2)) are reset to zero in the child. * The child's set of pending signals is initially empty (sigpend‐ ing(2)). * The child does not inherit semaphore adjustments from its parent(semop(2)). * The child does not inherit process-associated record locks from its parent (fcntl(2)). (On the other hand, it does inherit fcntl(2) open file description locks and flock(2) locks from its parent.) * The child does not inherit timers from its parent (setitimer(2), alarm(2), timer_create(2)). * The child does not inherit outstanding asynchronous I/O operations from its parent (aio_read(3), aio_write(3)), nor does it inherit any asynchronous I/O contexts from its parent (see io_setup(2)). The process attributes in the preceding list are all specified in POSIX.1. The parent and child also differ with respect to the follow‐ing Linux-specific process attributes: * The child does not inherit directory change notifications (dnotify) from its parent (see the description of F_NOTIFY in fcntl(2)). * The prctl(2) PR_SET_PDEATHSIG setting is reset so that the child does not receive a signal when its parent terminates.

    测试用例:

    #include<stdio.h> #include <sys/types.h> #include <unistd.h> int main() { pid_t pid; int i=100; pid =fork(); //fork error if(pid==-1) { printf("fork error \n"); return 1; } else if(pid)// this is parent process { i=200; printf ("i in parent process is %d\n",i); } else if(pid==0)//this is child process { i=300; printf ("i in child process is %d\n",i); } return 0; }

    综合测试代码:fork,exec balabala

    #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(void) { char *arg[] = {"ls","-a",NULL}; if(fork() == 0){ //in child1 printf("fork1 is OK;execl\n"); if(execl("/bin/ls","ls","-a",NULL) == -1){ perror("execl error"); exit(1); } } usleep(20000); if(fork() == 0){ //in child2 printf("fork2 is OK;execv\n"); if(execv("/bin/ls",arg) == -1){ perror("execv error"); exit(1); } } usleep(20000); if(fork() == 0){ //in child3 printf("fork3 is OK;execlp\n"); if(execlp("ls","ls","-a",NULL) == -1){ perror("execlp error"); exit(1); } } usleep(20000); if(fork() == 0){ //in child4 printf("fork4 is OK;execvp\n"); if(execvp("ls",arg) == -1){ perror("execvp error"); exit(1); } } usleep(20000); if(fork() == 0){ //in child5 printf("fork5 is OK;execle\n"); if(execle("/bin/ls","ls","-a",NULL,NULL) == -1){ perror("execle error"); exit(1); } } usleep(20000); if(fork() == 0){ //in child6 printf("fork6 is OK;execve\n"); if(execve("/bin/ls",arg,NULL) == -1){ perror("execve error"); exit(1); } } //加入小延时可以避免发生混乱的情况 usleep(20000); return 0; }
    Processed: 0.018, SQL: 9