操作系统实验3共享内存进程间通信实验

操作系统实验3:共享内存进程间通信实验

文章目录

  • 操作系统实验3:共享内存进程间通信实验
  • 实验内容
  • 实验示例代码
  • 过程记录
    • 代码
    • 编译链接与运行结果
    • 实验现象解释

实验内容

利用Linux进程通信(PIC)的共享内存函数,实践操作系统的共享内存进程间通信的相关知识。

共享内存函数由shmget、shmat、shmdt、shmctl四个函数组成。

  • 任务一: 以实验2的进程创建为基础。创建一个子进程,在子进程中向共享内存中写入一个字符串。父进程等 待子进程的结束,当子进程结束后,父进程读出共享内存中子进程写入的字符串。
  • 任务二: 取消任务一中父进程对子进程的等待,再次编译,观察运行结果。 针对于本实验中任务一、二出现的结果的区别请在实验报告中予以解释和说明。

实验示例代码

操作系统实验3共享内存进程间通信实验_第1张图片

过程记录

代码

  • 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);
        }
    }
    

编译链接与运行结果

操作系统实验3共享内存进程间通信实验_第2张图片

实验现象解释

  • 去掉之前

    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.

你可能感兴趣的:(操作系统,linux,操作系统,共享内存)