您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > Windows相关 > windows进程的创建
实验代码Master:#includewindows.h#includeiostream.h#includestdio.h#includestring.hvoidmain(intargc,char*argv[]){charcmd[128];if(argc!=1)strcpy(cmd,argv[1]);elsestrcpy(cmd,slave.exe);intpid=GetCurrentProcessId();coutprocessID:pidendl;coutMaster准备运行:cmd\n;sprintf(cmd+strlen(cmd),%d,pid);//将pid复制到cmd后面//printf(%s\n,cmd);cout.flush();STARTUPINFOinfo;memset(&info,0,sizeof(info));info.cb=sizeof(info);PROCESS_INFORMATIONpinfo;if(!CreateProcess(NULL,cmd,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&info,&pinfo))//cmd是一个命令行{coutMaster说:从进程cmd没有找到\n;coutMaster说:重新输入从进程名\n;}coutMaster说:我睡觉去喽……………………………………………………………………\n;cout.flush();Sleep(10000);for(inti=1;i5;i++)coutMaster说:睡醒了\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\n;coutMaster说:我刷牙、洗脸、吃饭、运动\n;coutMaster说:我结束了!!!!!\n;exit(0);}Slave:#includewindows.h#includeiostream.h#includestdio.hvoidmain(intargc,char*argv[]){if(argc!=2){coutSlave说:请重新运行MASTER.EXE.\n;exit(1);}intpid=atoi(argv[1]);HANDLEprocess=OpenProcess(PROCESS_QUERY_INFORMATION|SYNCHRONIZE,FALSE,pid);if(!process)coutSlave:Erroropeningprocess\n;coutSlave说:我要等Master起床。\n;cout.flush();if(WaitForSingleObject(process,INFINITE)==WAIT_OBJECT_0)coutSlave说:Master结束了,该我玩了!!!!!!!!!!!!!!!!\n;elsecoutSlave说:出什么错了\n;for(inti=1;i5;i++)couti我唱歌看新闻\n;coutSlave说:我也该结束了,拜拜!\n;exit(0);}实验结果
本文标题:windows进程的创建
链接地址:https://www.777doc.com/doc-5750459 .html