在C / C ++中同时运行两个进程 [英] Run two process at the same time in C/C++

查看:115
本文介绍了在C / C ++中同时运行两个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C / C ++中创建一个将运行2个进程的程序。这是我的示例代码



i want to create one program in C/C++ that will run 2 process. This is my sample code

#include <stdio.h>
#include <stdlib.h>

main()
{
  system("Run process 1");
  system("Run process 2")
  return 0;
} 
;





但问题是我必须先等待进程1才能完成它可以继续第二个过程(过程2)。



什么是最佳解决方案,因此它可以运行进程2而无需等待process1先完成。



谢谢



But the problem is I have to wait the "process 1" to finish first before it can proceed with the second process ("process 2").

What is the best solution so it can run the "process 2" without waiting the "process1" to finish first.

Thanks

推荐答案

使用 fork [ ^ ]创建两个线程并通过 system exec [ ^ ]。
Use fork[^] to create two threads and start each process via system or exec[^].


如果使用Visual Studio 2012(在Windows上),它包含C ++的最新语言功能最近才加入。查找''async''关键字。



我看到你有''linux''标记。我不知道任何linux编译器是否可用,但async仍然是你的答案。
If you use Visual Studio 2012 (on Windows), it contains the latest language features of C++ which were just added recently. Look up the ''async'' keyword.

I see you have ''linux'' flagged. I don''t know if this is available yet with any linux compiler, but ''async'' is still your answer.


线程本身称为轻量级进程。

我认为,无论操作系统是什么,解决方案都是创建新线程并让各个线程执行不同的进程。
Threads are themselves called light weight processes.
I think, whichever the OS be, solution is to create new threads and let the individual threads execute the different processes.


这篇关于在C / C ++中同时运行两个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆