<分区>
我有以下问题陈述:
Implement a function substLinesMany ... . All the specified files should be processed concurrently. If any of the files result in an error, then substLinesMany will return false otherwise return true.
你会在这里使用线程还是 fork ? (必须选一个)
<分区>
我有以下问题陈述:
Implement a function substLinesMany ... . All the specified files should be processed concurrently. If any of the files result in an error, then substLinesMany will return false otherwise return true.
你会在这里使用线程还是 fork ? (必须选一个)
最佳答案
我会使用线程而不是 fork 。创建一个新线程比创建一个新进程消耗更少的资源。线程共享相同的地址空间,而 fork 一个进程需要创建一个具有新地址空间的新进程。鉴于函数的性质(替换文件中的行),没有必要为每个文件提供单独的地址空间。
唯一的缺点是每个进程可能对同时打开的文件数量有限制,这在使用线程时可能会受到影响。不过,这是一个可控的问题。
关于c - 什么时候使用 fork 或线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26955042/