开发网站设计微信网站开发的代码
2026/4/18 10:22:01 网站建设 项目流程
开发网站设计,微信网站开发的代码,网站付费模板,网站建设中翻译一、进程等待的必要性二、进程等待的方法和概念三、阻塞与非阻塞等待3.1进程的阻塞等待#xff1a;int main() {pid_t pid;pid fork();if(pid 0){printf(%s fork error\n,__FUNCTION__);return 1; } else if( pid 0 ){ //childprintf(child is run, …一、进程等待的必要性二、进程等待的方法和概念三、阻塞与非阻塞等待3.1进程的阻塞等待int main() { pid_t pid; pid fork(); if(pid 0){ printf(%s fork error\n,__FUNCTION__); return 1; } else if( pid 0 ){ //child printf(child is run, pid is : %d\n,getpid()); sleep(5); exit(257); } else{ int status 0; pid_t ret waitpid(-1, status, 0);//阻塞式等待等待5S printf(this is test for wait\n); if( WIFEXITED(status) ret pid ){ printf(wait child 5s success, child return code is :%d.\n,WEXITSTATUS(status)); }else{ printf(wait child failed, return.\n); return 1; } } return 0; }3.2进程的非阻塞等待#include stdio.h #include stdlib.h #include sys/wait.h #include unistd.h #include vector typedef void (*handler_t)(); // 函数指针类型 std::vectorhandler_t handlers; // 函数指针数组 void fun_one() { printf(这是⼀个临时任务1\n); } void fun_two() { printf(这是⼀个临时任务2\n); } void Load() { handlers.push_back(fun_one); handlers.push_back(fun_two); } void handler() { if (handlers.empty()) Load(); for (auto iter : handlers) iter(); } int main() { pid_t pid; pid fork(); if (pid 0) { printf(%s fork error\n, __FUNCTION__); return 1; } else if (pid 0) { // child printf(child is run, pid is : %d\n, getpid()); sleep(5); exit(1); } else { int status 0; pid_t ret 0; do { ret waitpid(-1, status, WNOHANG); // ⾮阻塞式等待 if (ret 0) { printf(child is running\n); } handler(); } while (ret 0); if (WIFEXITED(status) ret pid) { printf(wait child 5s success, child return code is :%d.\n, WEXITSTATUS(status)); } else { printf(wait child failed, return.\n); return 1; } } return 0; }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询