I am trying to write a program in C based on the concept of shared memory. I am creating two shared memory and creating two processes using the fork() function. I want one of the process to write into one of the shared memory and the other to read from it and vice versa with the other shared memory. I don't want to use pipes. How can i context switch between the two processes continuosly so that I can read from one and write from the other?
我正在尝试基于共享内存的概念在C中编写程序。我正在创建两个共享内存并使用fork()函数创建两个进程。我希望其中一个进程写入其中一个共享内存,另一个进程从其中读取,反之亦然,使用其他共享内存。我不想用管道。我如何连续地在两个进程之间切换上下文,以便我可以从一个进程读取并从另一个进行写入?
1 个解决方案
#1
1
I am trying to write a program in C based on the concept of shared memory. I am creating two shared memory and creating two processes using the fork() function. I want one of the process to write into one of the shared memory and the other to read from it and vice versa with the other shared memory. I don't want to use pipes. How can i context switch between the two processes continuosly so that I can read from one and write from the other?
我正在尝试基于共享内存的概念在C中编写程序。我正在创建两个共享内存并使用fork()函数创建两个进程。我希望其中一个进程写入其中一个共享内存,另一个进程从其中读取,反之亦然,使用其他共享内存。我不想用管道。我如何连续地在两个进程之间切换上下文,以便我可以从一个进程读取并从另一个进行写入?
Context switching is not a C concept. The OS manages it, outside the control of userspace programs. But that might not even be relevant to your problem, because multicore and multiprocessor computers are the norm these days, and they can provide true concurrency, without context switching.
上下文切换不是C概念。操作系统在用户空间程序的控制之外管理它。但这可能与您的问题无关,因为多核和多处理器计算机现在已成为常态,它们可以提供真正的并发性,而无需上下文切换。
It is entirely possible for two processes to communicate via shared memory. That's what it's for, after all. But it is not possible for them to do so without a mechanism for synchronizing their actions. For example, how is process B supposed to know when process A has finished updating the shared memory, so that B can read what A wrote? And how can A know that B has finished reading so that no data will be lost if it updates the shared memory again?
两个进程完全可以通过共享内存进行通信。毕竟,这就是它的用途。但如果没有同步行动的机制,他们就不可能这样做。例如,进程B如何知道进程A何时完成更新共享内存,以便B可以读取A写的内容?如何知道B已经完成读取,以便在再次更新共享内存时不会丢失任何数据?
These kinds of synchronization problems are handled via for-purpose synchronization objects such as semaphores, mutexes, and condition variables, and functions that operate on them. How to use these properly is much too broad a topic to cover in one SO answer, but you will find many examples in various SO questions about the details.
这些类型的同步问题通过信号量,互斥量和条件变量等专用同步对象以及对它们进行操作的函数来处理。如何正确使用这些是一个太广泛的主题,无法涵盖在一个SO答案中,但你会在各种SO问题中找到许多关于细节的例子。
Or you can just use pipes, which are a lot easier for communications that are fundamentally serial in nature.
或者你可以使用管道,这对于基本上是连续的通信来说要容易得多。
#1
1
I am trying to write a program in C based on the concept of shared memory. I am creating two shared memory and creating two processes using the fork() function. I want one of the process to write into one of the shared memory and the other to read from it and vice versa with the other shared memory. I don't want to use pipes. How can i context switch between the two processes continuosly so that I can read from one and write from the other?
我正在尝试基于共享内存的概念在C中编写程序。我正在创建两个共享内存并使用fork()函数创建两个进程。我希望其中一个进程写入其中一个共享内存,另一个进程从其中读取,反之亦然,使用其他共享内存。我不想用管道。我如何连续地在两个进程之间切换上下文,以便我可以从一个进程读取并从另一个进行写入?
Context switching is not a C concept. The OS manages it, outside the control of userspace programs. But that might not even be relevant to your problem, because multicore and multiprocessor computers are the norm these days, and they can provide true concurrency, without context switching.
上下文切换不是C概念。操作系统在用户空间程序的控制之外管理它。但这可能与您的问题无关,因为多核和多处理器计算机现在已成为常态,它们可以提供真正的并发性,而无需上下文切换。
It is entirely possible for two processes to communicate via shared memory. That's what it's for, after all. But it is not possible for them to do so without a mechanism for synchronizing their actions. For example, how is process B supposed to know when process A has finished updating the shared memory, so that B can read what A wrote? And how can A know that B has finished reading so that no data will be lost if it updates the shared memory again?
两个进程完全可以通过共享内存进行通信。毕竟,这就是它的用途。但如果没有同步行动的机制,他们就不可能这样做。例如,进程B如何知道进程A何时完成更新共享内存,以便B可以读取A写的内容?如何知道B已经完成读取,以便在再次更新共享内存时不会丢失任何数据?
These kinds of synchronization problems are handled via for-purpose synchronization objects such as semaphores, mutexes, and condition variables, and functions that operate on them. How to use these properly is much too broad a topic to cover in one SO answer, but you will find many examples in various SO questions about the details.
这些类型的同步问题通过信号量,互斥量和条件变量等专用同步对象以及对它们进行操作的函数来处理。如何正确使用这些是一个太广泛的主题,无法涵盖在一个SO答案中,但你会在各种SO问题中找到许多关于细节的例子。
Or you can just use pipes, which are a lot easier for communications that are fundamentally serial in nature.
或者你可以使用管道,这对于基本上是连续的通信来说要容易得多。