我需要在1个程序中创建3个进程来模拟死锁。我怎样才能定义我自己的过程?我不想简单地打开现有的流程。我们可以在C#中创建自己的过程吗?
例如,对于一个线程:
Thread X = new Thread(){ //insert whatever code here// };
我们怎样才能做到这一点的过程中,如:
Process P = new Process(){//insert different threads here//};
我需要在1个程序中创建3个进程来模拟死锁。我怎样才能定义我自己的过程?我不想简单地打开现有的流程。我们可以在C#中创建自己的过程吗?
例如,对于一个线程:
Thread X = new Thread(){ //insert whatever code here// };
我们怎样才能做到这一点的过程中,如:
Process P = new Process(){//insert different threads here//};
进程来自可执行文件。您可以编写Process.Start(@"C:\Something.exe")
。
进程来自操作系统,有时从exe文件创建。 ; ) –
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Process process = new Process();
process.StartInfo.FileName = "notepad";
//process.StartInfo.Arguments = "filename.txt"
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
}
}
}
你可能寻找Process.Start(静态或实例方法) - 它会启动您选择的可执行文件创建新的进程。
如果你正在寻找fork from Unix的实现,你不能真正在.Net中做到这一点,据我所知。
一个进程中的代码不能在另一个进程中执行。您需要某种形式的进程间通信或远程过程调用。
你为什么要模拟使用过程?为什么不使用线程来模拟它? –
用三个线程模拟死锁是不够的吗? – Christoffer
我可以看到一个潜在的原因,为什么人们需要新的进程来模拟死锁 - 同时访问2+文件进行写入时出现死锁,但听起来这样的行为会比使用适当的模拟资源更容易/更可靠地获得几个过程。 –