简体   繁体   English

不同进程的线程之间的通信是否有保护机制?

[英]Are there protection mechanisms for communication between threads from different processes?

Given that threads (of the same process) run in a shared memory space and synchronizing the access to the shared data within the process gains unprecedented importance, how does this apply to threads from different processes?鉴于(同一进程的)线程在共享的 memory 空间中运行,并且同步访问进程内的共享数据变得前所未有的重要,这如何适用于来自不同进程的线程? What are the communication methods available for them?他们可以使用哪些沟通方式?

Threads from different processes are generally treated just like independent processes themselves, because they share no state with each other and operate in separate memory spaces.来自不同进程的线程通常被视为独立进程本身,因为它们彼此不共享 state,并且在单独的 memory 空间中运行。

As a consequence, they use the standard interprocess communication mechanisms, such as Unix sockets, pipes, and other file descriptors;因此,它们使用标准的进程间通信机制,例如 Unix sockets、管道和其他文件描述符; SysV or POSIX IPC mechanisms such as shared memory and semaphores; SysV或POSIX IPC机制,如共享memory和信号量; locking;锁定; and signals.和信号。 It may be convenient to have a thread in a process that is dedicated to interacting with other processes, but IPC can be done with single-threaded processes as well or in multithreaded processes without dedicated IPC threads.在一个专门用于与其他进程交互的进程中拥有一个线程可能很方便,但是 IPC 也可以在单线程进程中完成,或者在没有专用 IPC 线程的多线程进程中完成。

Since the risk of accidentally mutating other threads' data is not possible across processes, sometimes synchronization is handled automatically by the kernel, such as with pipes, but it is possible to need to coordinate across processes.由于不可能跨进程意外改变其他线程的数据的风险,有时同步由 kernel 自动处理,例如使用管道,但可能需要跨进程进行协调。 File locks, such as with fcntl(2) and flock(2) , can prevent co-operating processes from corrupting files;文件锁,例如fcntl(2)flock(2)可以防止协作进程损坏文件; semaphores can prevent contention or limit concurrency for shared resources;信号量可以防止共享资源的争用或限制并发; and it's even possible to share a mutex for some shared memory cases.甚至可以为一些共享的 memory 案例共享一个互斥锁。 The tools you use depend on the resources you're sharing across processes.您使用的工具取决于您跨流程共享的资源。

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

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