同时执行流程
Execute Process concurently
本文关键字:执行流 更新时间:2023-10-16
我的程序从user.then获取范围,然后创建3个进程并逐一执行但我希望所有进程同时运行。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
void function();
cin>>range;
cin>>process;
int main() {
int range = 1000;
int i;
int pid;
int pid1;
for(i = 0; i < ; i++) {
pid1 = fork();
}
if(pid1==0) {
pid = getpid();
printf("The process id is: %dn", pid);
function(range);
}
else {
wait(0);
}
return 0;
}
首先,正如其他人所提到的,您不是在分叉3个进程,而是在分叉8个进程。这样修复:
for (i = 0; i < 3; ++i) {
pid1 = fork();
if (pid1 == 0) break;
}
然后,所有4个进程都并行运行。不过,根据function
的作用,可能很难看到这一点——它可能完成得太快了。所以试试这个:
if (pid1 == 0) {
printf("pid=%dn", getpid());
sleep(5);
printf("done sleep %dn", getpid());
} else {
wait(0);
}
您应该打印出三个"pid=XX",然后延迟大约5秒,然后"完成睡眠XX"。
相关文章:
- 在程序执行期间从标准输出重定向到自定义流
- c++中的执行时间和检查流状态
- 张量流错误:执行器无法创建内核。没有注册'Snapshot' 适用于 GPU 设备的 OpKernel 运行图像标签示例
- 如何分离以C++执行的 Java 应用程序的 stdin/stout/stderr 流
- 如何在SDL2音频流数据上执行实时FFT
- 执行问题流
- Pycuda使用流执行推力
- 对理解C++代码的流执行稍有帮助
- 如何重载iostream运算符以在整个流上执行功能
- C++ 在写入流时执行函数
- C++ >>变量名称执行中的流
- 如何让输出流稍后执行某些操作
- cuda流:流执行后未调用回调
- cocos2d-X 中的执行流
- 为什么要使用fopen打开流,并使用读/写(而不是fread/fwrite)执行I/O
- python:读取可执行文件的 stdout,破碎的流
- Clang静态分析-根据执行流检查可为空的变量初始化
- 块执行流,直到脚本执行完成
- 在执行二进制序列化时,删除字符串流结果的const-ness是否安全?
- 系统()调用批处理可执行流文件使程序在windows上重置