简体   繁体   English

Linux / Windows上的线程/进程比较

[英]Threads/Process comparison on Linux/Windows

I have some experience in using threads and processes in Windows. 我在Windows中使用线程和进程方面有一些经验。

May someone explain, is there any mapping possible the threads and processes in windows to the same in Linux? 有人可以解释一下,是否有可能将Windows中的线程和进程映射到Linux中的线程和进程?

That means, Threads in Windows == Threads in Linux? 这意味着Windows中的线程== Linux中的线程? -> Makes any sense? ->有道理吗? Process in Windows == Process in Linus? Windows中的进程== Linus中的进程? -> Makes any sense? ->有道理吗?

If same, I have CreateThread() and CreateProcess() calls in windows, what are the equivalent calls in linux? 如果相同,我在Windows中有CreateThread()和CreateProcess()调用,在Linux中等效的调用是什么?

I have read some posts in SO but most of them haven't cleared my doubts.So thought to start a new post. 我已经阅读了SO中的一些帖子,但大多数都没有消除我的疑虑。

It would be nice If I get some explanation with some simple examples(C programming). 如果我通过一些简单的例子(C编程)得到一些解释,那将是很好的。

Well, there are equivalent calls for your purpose in Linux, but they work a little different, at least for the process mechanism. 好吧,在Linux中有满足您目的的等效调用,但是它们至少在处理机制方面有所不同。

  1. For threads, you can use pthread_create . 对于线程,可以使用pthread_create It works in a very similar fashion to CreateThread , except some parameters are different. 除了某些参数不同外,它的工作方式与CreateThread非常相似。 Should be very easy to use. 应该很容易使用。 Here's a good tutorial: https://computing.llnl.gov/tutorials/pthreads/ 这是一个很好的教程: https : //computing.llnl.gov/tutorials/pthreads/

  2. Emulating CreateProcess in order to start an external process is not so straightforward. 为了启动外部过程而模拟CreateProcess并不是那么简单。 You will need the famous fork/exec combo. 您将需要著名的fork/exec组合。 First, you need to call fork inside the main process to spawn a child process. 首先,您需要在主流程中调用fork来生成子流程。 This child is created by duplicating the initial process. 通过复制初始过程来创建此子级。 You can then control the flow by checking the value returned by fork : 然后,您可以通过检查fork返回的值来控制流:

  int rv = fork(); // new process was spawned here. The following code is executed // by both processes. if(rv == 0) { // we are in the child process } else { // we are in the parent } 

Basically rv will be 0 for the child and the pid of the child for the parent. 基本上,对于孩子来说, rv将为0,对于父母,孩子的pid将为0。 I hope I haven't lost you so far. 希望我到目前为止还没有失去你。 :) :)

Moving on, you will need a call to one of the exec family of functions to start an external process: 继续,您将需要调用exec函数家族之一来启动外部进程:

  int rv = fork(); // new process was spawned here. The following code is executed // by both processes. if(rv == 0) { execl("/bin/ls", "ls", NULL); // start the ls process } else { // we are in the parent } 

In the above example, I am starting the /bin/ls external process, which prints the contents of the current folder. 在上面的示例中,我正在启动/bin/ls外部进程,该进程将打印当前文件夹的内容。

Here's a simple full example: http://flinflon.brandonu.ca/dueck/2001/62306/Processes/Unix%20Examples.htm 这是一个简单的完整示例: http : //flinflon.brandonu.ca/dueck/2001/62306/Processes/Unix%20Examples.htm

Now you may be wondering why you need to call fork in the first place and why execl is not enough. 现在,您可能想知道为什么首先需要调用fork以及为什么execl还不够。 This is because after the program invoked by execl terminates, the current process is also terminated, and you don't want that to happen in the main process. 这是因为execl调用的程序终止后,当前进程也终止了,您不希望在主进程中发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM