開源鴻蒙Fork和Waitpid測試
前言
最近做項目需要用到多進程部分河劝,并在不同平臺進行測試渔欢,現(xiàn)將結(jié)果記錄如下:
接口介紹
-
fork
#include <unistd.h> //creates a new process by duplicating the calling process. //The new process is referred to as the child process. The calling //process is referred to as the parent process. //The child process and the parent process run in separate memory //spaces. At the time of fork() both memory spaces have the same //content. Memory writes, file mappings (mmap(2)), and unmappings //(munmap(2)) performed by one of the processes do not affect the //other. //Return //On success, the PID of the child process is returned in the //parent, and 0 is returned in the child. On failure, -1 is //returned in the parent, no child process is created, and errno is //set to indicate the error. pid_t fork(void);
-
wait
#include <sys/types.h> #include <sys/wait.h> //The wait() system call suspends execution of the calling thread //until one of its children terminates. The call wait(&wstatus) is //equivalent to:waitpid(-1, &wstatus, 0); //Return //on success, returns the process ID of the terminated //child; on failure, -1 is returned. pid_t wait(int *status);
-
waitpid
#include <sys/types.h> #include <sys/wait.h> //The waitpid() system call suspends execution of the calling //thread until a child specified by pid argument has changed state. //By default, waitpid() waits only for terminated children, but //this behavior is modifiable via the options argument, as //described below. //The value of pid can be: // < -1 meaning wait for any child process whose process group ID // is equal to the absolute value of pid. // -1 meaning wait for any child process. // 0 meaning wait for any child process whose process group ID // is equal to that of the calling process at the time of the // call to waitpid(). // > 0 meaning wait for the child whose process ID is equal to // the value of pid. //The value of options is an OR of zero or more of the following //constants: // WNOHANG // return immediately if no child has exited. // WUNTRACED // also return if a child has stopped (but not traced via // ptrace(2)). Status for traced children which have stopped // is provided even if this option is not specified. // WCONTINUED (since Linux 2.6.10) // also return if a stopped child has been resumed by // delivery of SIGCONT. //Return //on success, returns the process ID of the child whose //state has changed; if WNOHANG was specified and one or more //child(ren) specified by pid exist, but have not yet changed //state, then 0 is returned. On failure, -1 is returned. pid_t waitpid(pid_t pid, int *status, int options);
測試代碼1
以下代碼設(shè)計為啟動兩個進程雹姊,然后在主進程等待執(zhí)行完成初橘,查看返回值后執(zhí)行主進程
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main() { pid_t child1, child2; pid_t rc1, rc2; printf("parent(pid = %d, ppid = %d\n", getpid(), getppid()); if ((child1 = fork()) == 0) { for (int i = 1; i <= 10; i++) { printf("child1(process=%d%) is working!(pid=%d,ppid=%d)\n", i * 10, getpid(), getppid()); sleep(1); } printf("child1 had completed!\n"); } else if (child1 > 0) { if ((child2 = fork()) == 0) { for (int i = 1; i <= 10; i++) { printf("child2(process=%d%) is working!(pid=%d,ppid=%d)\n", i * 10, getpid(), getppid()); sleep(1); } printf("child2 had completed!\n"); } else if (child2 > 0) { //option = 0意味著阻塞父進程等子進程結(jié)束 rc1 = waitpid(child1, NULL, 0); rc2 = waitpid(child2, NULL, 0); printf("rc1=%d, rc2=%d\n", rc1, rc2); printf("child1=%d, child2=%d\n", child1, child2); for (int i = 1; i <= 3; i++) { printf("parent(process=%d%) is working!(pid=%d,ppid=%d)\n", i * 10, getpid(), getppid()); sleep(1); } printf("parent had completed!\n"); } else if (child2 == -1) { printf("Error!\n"); } } else { printf("Error!\n"); } return 0; }
測試結(jié)果1
-
Linux
Linux測試 -
OHOS(開源鴻蒙開發(fā)板,RK3568)
OHOS測試
總結(jié)1
Linux下可按照設(shè)計目標執(zhí)行幢炸,但是OHOS下waitpid返回值為-1泄隔,意味沒有找到子進程;但是實際上waitpid是找到子進程并等待子進程執(zhí)行完畢后才執(zhí)行的主進程宛徊。
測試代碼2
以下代碼設(shè)計為啟動兩個進程佛嬉,然后在主進程等待執(zhí)行完成,查看返回值后執(zhí)行主進程
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main() { pid_t child1, child2; pid_t rc1, rc2; printf("parent(pid = %d, ppid = %d\n", getpid(), getppid()); if ((child1 = fork()) == 0) { for (int i = 1; i <= 10; i++) { printf("child1(process=%d%) is working!(pid=%d,ppid=%d)\n", i * 10, getpid(), getppid()); sleep(1); } printf("child1 had completed!\n"); } else if (child1 > 0) { if ((child2 = fork()) == 0) { for (int i = 1; i <= 10; i++) { printf("child2(process=%d%) is working!(pid=%d,ppid=%d)\n", i * 10, getpid(), getppid()); sleep(1); } printf("child2 had completed!\n"); } else if (child2 > 0) { //option = WNOHANG 意味著不阻塞父進程闸天,子進程和父進程交替運行 rc1 = waitpid(child1, NULL, WNOHANG); rc2 = waitpid(child2, NULL, WNOHANG); printf("rc1=%d, rc2=%d\n", rc1, rc2); printf("child1=%d, child2=%d\n", child1, child2); for (int i = 1; i <= 3; i++) { printf("parent(process=%d%) is working!(pid=%d,ppid=%d)\n", i * 10, getpid(), getppid()); sleep(1); } printf("parent had completed!\n"); } else if (child2 == -1) { printf("Error!\n"); } } else { printf("Error!\n"); } return 0; }
測試結(jié)果2
-
Linux
Linux測試 -
OHOS
OHOS測試
-
總結(jié)2
WNOHANG:意味不阻塞進程立即返回暖呕,如果子進程存在但是沒有狀態(tài)變化則返回0;Linux執(zhí)行可看出進程是交替執(zhí)行的苞氮,但是OHOS子進程好像沒有搶到執(zhí)行權(quán)限湾揽,所以只有等父進程執(zhí)行完畢后才依次執(zhí)行子進程。