杭州网站建设公司有哪几家,shopping跨境电商平台,美食网站建设的栏目和模板,高端网站制造c/cpp - 多线程/进程 多进程多进程创建多进程进程等待多进程 宏观上 两个进程完全并发的 父子进程具有互相独立的进程空间 父进程结束#xff0c;不影响子进程的执行 创建多进程
#include sys/types.h
#include unistd.h
#include stdio.hint main()…
c/cpp - 多线程/进程 多进程多进程创建多进程进程等待多进程 宏观上 两个进程完全并发的 父子进程具有互相独立的进程空间 父进程结束不影响子进程的执行 创建多进程
#include sys/types.h
#include unistd.h
#include stdio.hint main(){pid_t pid;/* eg1 */// while (1)// {// // 进程ID号// printf(pid %d\n,getpid());// // 该进程 的 父进程的ID号// printf(pid parent %d\n,getppid());// }// /* eg2 */// printf(pid %d\n,getpid());// // 复制一个进程当作子进程// // 如果调用成功返回子进程id号非0,子进程的id好为0// pid fork();// printf(pid %d\n,pid);// printf(hell world); // fork之后 父进程 子进程都执行该语句/* eg3 */pid_t pid1;pid_t pid2;pid1 fork();pid2fork();printf(pid1 %d, pid2 %d\n,pid1,pid2);// fork树/*A -A -A: pid1id(B), pid2id(C)-C: pid1id(B)(拷贝出来的), pid20-B -B: pid10, pid2id(D)-D: pid10(拷贝出来的), pid20 */return 0;
}例子2
#include sys/types.h
#include unistd.h
#include stdio.hint main(){pid_t pid;pid fork();printf(pid %d\n,pid);/*A -A pidid(B)-B pid0 *//*宏观上 两个进程完全并发的 父子进程具有互相独立的进程空间父进程结束不影响子进程的执行*/// 根据id号 区分父进程 子进程执行不同的任务if (pid0) // parent process{while (1){printf(parent process running...\n);sleep(1);}}else if (pid0) // child process{ while (1){printf(child process running...\n);sleep(1);}}else{printf(ERROR: fork failed\n);return -1;}return 0;
}进程等待
#include sys/types.h
#include sys/wait.h
#include unistd.h
#include stdio.h
#include stdlib.hint main(int argc,char* argv[])
{pid_t child_pid;int num;for (int i1;iargc;i){switch (fork()){case 0: // 当前是子进程printf(child process %d start id %d, sleep %s\n,i,getpid(),argv[i]);sleep(atoi(argv[i]));exit(0);break;case -1:perror(fork()\n);exit(0);default:break;}}/*A -A break -A break -A break-A3 exit运行之后结束不再fork-A2 exit运行之后结束不在fork-A1 exit(子进程结束)不再fork*/num0;// 表示主进程一直等待直到所有子进程结束再结束主进程while (1){child_pidwait(NULL); // wait表示多个子进程中的一个结束就返回if (child_pid-1) // -1 表示没有可以正在运行的子进程{printf(no more child process runnig\n);exit(0);}num;printf(wait() child pid%d over,num%d\n,child_pid,num);}return 0;
}/*child process 2 start id 24667, sleep 10child process 1 start id 24666, sleep 5child process 3 start id 24668, sleep 15wait() child pid24666 over,num1wait() child pid24667 over,num2wait() child pid24668 over,num3no more child process runnig*/
文章转载自: http://www.morning.ljwyc.cn.gov.cn.ljwyc.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.qtsks.cn.gov.cn.qtsks.cn http://www.morning.srnhk.cn.gov.cn.srnhk.cn http://www.morning.ldmtq.cn.gov.cn.ldmtq.cn http://www.morning.dkmzr.cn.gov.cn.dkmzr.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.fkmqg.cn.gov.cn.fkmqg.cn http://www.morning.bmbnc.cn.gov.cn.bmbnc.cn http://www.morning.jllnh.cn.gov.cn.jllnh.cn http://www.morning.ddrdt.cn.gov.cn.ddrdt.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.ffbl.cn.gov.cn.ffbl.cn http://www.morning.bmyrl.cn.gov.cn.bmyrl.cn http://www.morning.thrgp.cn.gov.cn.thrgp.cn http://www.morning.yydeq.cn.gov.cn.yydeq.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.fjtnh.cn.gov.cn.fjtnh.cn http://www.morning.qgdsd.cn.gov.cn.qgdsd.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.znqxt.cn.gov.cn.znqxt.cn http://www.morning.mdlqf.cn.gov.cn.mdlqf.cn http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.nwpnj.cn.gov.cn.nwpnj.cn http://www.morning.bwmq.cn.gov.cn.bwmq.cn http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.tfrlj.cn.gov.cn.tfrlj.cn http://www.morning.nrcbx.cn.gov.cn.nrcbx.cn http://www.morning.bxrlt.cn.gov.cn.bxrlt.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.nfzw.cn.gov.cn.nfzw.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.bjsites.com.gov.cn.bjsites.com http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.qsy39.cn.gov.cn.qsy39.cn http://www.morning.rgxll.cn.gov.cn.rgxll.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.liyixun.com.gov.cn.liyixun.com http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.haolipu.com.gov.cn.haolipu.com http://www.morning.qbfwb.cn.gov.cn.qbfwb.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.clndl.cn.gov.cn.clndl.cn http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn http://www.morning.ntdzjx.com.gov.cn.ntdzjx.com http://www.morning.rxwnc.cn.gov.cn.rxwnc.cn http://www.morning.cwwbm.cn.gov.cn.cwwbm.cn http://www.morning.ryxyz.cn.gov.cn.ryxyz.cn http://www.morning.xjqrn.cn.gov.cn.xjqrn.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.zycll.cn.gov.cn.zycll.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.bdgb.cn.gov.cn.bdgb.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.gftnx.cn.gov.cn.gftnx.cn http://www.morning.llgpk.cn.gov.cn.llgpk.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.zljqb.cn.gov.cn.zljqb.cn http://www.morning.tlrxp.cn.gov.cn.tlrxp.cn http://www.morning.dyhlm.cn.gov.cn.dyhlm.cn http://www.morning.bxch.cn.gov.cn.bxch.cn http://www.morning.knnhd.cn.gov.cn.knnhd.cn http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn http://www.morning.gwmjy.cn.gov.cn.gwmjy.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.jbshh.cn.gov.cn.jbshh.cn http://www.morning.fwblh.cn.gov.cn.fwblh.cn http://www.morning.lfqnk.cn.gov.cn.lfqnk.cn http://www.morning.srmpc.cn.gov.cn.srmpc.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.vvdifactory.com.gov.cn.vvdifactory.com http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.gxcit.com.gov.cn.gxcit.com http://www.morning.rycbz.cn.gov.cn.rycbz.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.fmswb.cn.gov.cn.fmswb.cn http://www.morning.qzpkr.cn.gov.cn.qzpkr.cn