利用Linux进程通信(PIC)的共享内存函数,实践操作系统的共享内存进程间通信的相关知识。
共享内存函数由shmget、shmat、shmdt、shmctl四个函数组成。
lab3task1.c (有wait)
#include
#include
#include
#include
#include
#include
#include
int main(void)
{
int segment_id;
char *shared_memory;
const int size = 4096;
segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
shared_memory = (char *)shmat(segment_id, NULL, 0);
pid_t pid;
pid = fork();
if (pid < 0)
{
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0)
{
printf("Child Write: Hi There! \n");
sprintf(shared_memory, "Hi There! ");
}
else
{
wait(NULL);
printf("Parent Read:%s \n", shared_memory);
shmdt(shared_memory);
shmctl(segment_id,IPC_RMID,NULL);
exit(0);
}
}
lab3task2.c(无wait)
#include
#include
#include
#include
#include
#include
#include
int main(void)
{
int segment_id;
char *shared_memory;
const int size = 4096;
segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
shared_memory = (char *)shmat(segment_id, NULL, 0);
pid_t pid;
pid = fork();
if (pid < 0)
{
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0)
{
printf("Child Write: Hi There! \n");
sprintf(shared_memory, "Hi There! ");
}
else
{
wait(NULL);
printf("Parent Read:%s \n", shared_memory);
shmdt(shared_memory);
shmctl(segment_id,IPC_RMID,NULL);
exit(0);
}
}
去掉之前
This is a simple program that demonstrates how to use shared memory in a C program. The program creates a shared memory segment using the shmget
function, and then attaches the shared memory segment to the process’s address space using the shmat
function.
The program then forks a child process, and the child process writes a message to the shared memory segment using sprintf
. The parent process waits for the child to complete, and then reads the message from the shared memory segment using printf
. Finally, the parent process detaches the shared memory segment using shmdt
and removes it using shmctl
with the IPC_RMID
flag.(父进程使用shmdt'分离共享内存段,并使用
ipc_rmid“标志”使用`shmctl’将其删除。)
去掉wait之后
If you remove the wait
function, the parent process will not wait for the child process to complete before reading from the shared memory segment. This means that the parent process might try to read the shared memory before the child process has had a chance to write to it, resulting in undefined behavior.
The wait
function is used to synchronize the parent and child processes. It causes the parent process to block until the child process terminates, which ensures that the child process has completed its work before the parent process continues. This is important in this case because the parent process depends on the child process to write the message to the shared memory segment before the parent process reads it.
If you remove the wait
function, you might see different results depending on the timing of the two processes. The parent process might read the shared memory before the child process has written to it, in which case it will read an undefined value. Alternatively, the parent process might read the shared memory after the child process has written to it, in which case it will read the correct value.
To understand why this happens, it’s useful to know a little bit about how shared memory works. Shared memory is a way for different processes to access the same region of memory in a way that’s efficient and fast. When a process writes to shared memory, the changes are immediately visible to any other process that has attached the same shared memory segment. However, the changes are not immediately visible to the operating system or to other processes that have not attached the shared memory segment.
This means that the timing of the parent and child processes is important. If the parent process reads from the shared memory segment before the child process has had a chance to write to it, the parent process will read an undefined value because the shared memory segment has not yet been initialized. On the other hand, if the parent process reads from the shared memory segment after the child process has written to it, the parent process will read the correct value because the shared memory segment has been initialized by the child process.
In summary, removing the wait
function can result in undefined behavior because it can cause the parent process to read from the shared memory segment before the child process has had a chance to write to it. It’s important to use synchronization mechanisms like wait
to ensure that the processes are properly synchronized and that the shared memory is accessed correctly.
It looks like removing the wait(NULL)
function has caused the order of the output to change. With the wait(NULL)
function, the child process writes to the shared memory segment and then the parent process reads from it. Without the wait(NULL)
function, the parent process reads from the shared memory segment before the child process has had a chance to write to it, resulting in an empty string being printed.
This behavior is due to the fact that the parent process is not waiting for the child process to complete before reading from the shared memory segment. Without the wait(NULL)
function, the parent process reads from the shared memory segment as soon as it is attached, before the child process has had a chance to write to it. This is why you see an empty string being printed.
On the other hand, the child process is able to write to the shared memory segment before the parent process reads from it, because the child process starts executing as soon as it is created. This is why you see the child’s message being printed after the parent’s message.
In a C program, the wait
function is used to synchronize the parent and child processes. It causes the parent process to block until the child process terminates, which ensures that the child process has completed its work before the parent process continues. This is important in situations where the parent process depends on the child process to perform some task before the parent process can continue.
The wait
function is typically used in conjunction with the fork
function, which creates a child process that is a copy of the parent process. The child process starts executing at the same point as the parent process, but it has a separate memory space and a separate set of system resources.
One common use of fork
and wait
is to create a child process that performs some task in parallel with the parent process. For example, the parent process might create a child process to perform some time-consuming computation while the parent process continues to do other work. When the child process has completed its task, it can terminate, and the parent process can use the wait
function to block until the child process has terminated.
In the case of shared memory, the wait
function is used to ensure that the child process has had a chance to write to the shared memory segment before the parent process reads from it. Without the wait
function, the parent process might try to read from the shared memory segment before the child process has had a chance to write to it, resulting in undefined behavior.