linuxbash原理

3417 1647 0 04:17 ? 00:00:21 /usr/libexec/gnome-terminal-server

yangang 3425 3417 0 04:17 pts/0 00:00:00 bash

yangang 4524 3417 0 04:26 pts/1 00:00:00 bash

控制台创建是通过/usr/libexec/gnome-terminal-server 进行创建

read :

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>

#define SHM_NAME "/my_shared_memory"
#define SHM_SIZE 4096

int main() {
    int shm_fd;
    //char *ptr;

#if 0 
    // 打开共享内存对象
    shm_fd = shm_open(SHM_NAME, O_RDONLY, 0666);
    if (shm_fd == -1) {
        perror("shm_open");
        exit(EXIT_FAILURE);
    }

    // 映射共享内存
    ptr = mmap(0, SHM_SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
    if (ptr == MAP_FAILED) {
        perror("mmap");
        exit(EXIT_FAILURE);
    }

    // 从共享内存读取数据
    printf("Reading from shared memory:\n");
    printf("%s\n", ptr);

    // 清理
    if (munmap(ptr, SHM_SIZE) == -1) {
        perror("munmap");
    }

    close(shm_fd);
#endif 
    

    char *p = NULL;

    shm_fd = shm_open (SHM_NAME, O_RDONLY, 0666);
    if (shm_fd == -1) 
    {
        printf("error, shm_fd:%d\r\n", shm_fd);
    } 
    else {
    printf("shm_fd:%d\r\n", shm_fd);
    }
 
    p = mmap (0, SHM_SIZE,PROT_READ,MAP_SHARED, shm_fd, 0);             
    if (p == NULL)
    {
         printf("error, p = %d\r\n", p);
    } 
    else 
    {     

    printf("p:%d\r\n", p);
    }   
    
   printf("read value:%s", p);


    if ( munmap(p, SHM_SIZE) < 0)
    {
         perror("munmap failed\r\n");
    } else {
    printf("munmap ok\r\n");
    }
     close (shm_fd);
     

    return EXIT_SUCCESS;
}

write

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>

#define SHM_NAME "/my_shared_memory"
#define SHM_SIZE 4096

int main() {
    int shm_fd;
    char *ptr;
#if 0 
    // 创建共享内存对象
    shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0666);
    if (shm_fd == -1) {
        perror("shm_open");
        exit(EXIT_FAILURE);
    }

    // 设置共享内存大小
    if (ftruncate(shm_fd, SHM_SIZE) == -1) {
        perror("ftruncate");
        exit(EXIT_FAILURE);
    }

    // 映射共享内存
    ptr = mmap(0, SHM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
    if (ptr == MAP_FAILED) {
        perror("mmap");
        exit(EXIT_FAILURE);
    }

    // 写入数据到共享内存
    printf("Writing to shared memory...\n");
    sprintf(ptr, "Hello from the writer process! PID: %d", getpid());

    // 等待用户输入,保持共享内存映射
    printf("Press Enter to unlink and exit...\n");
    getchar();

    // 清理
    if (munmap(ptr, SHM_SIZE) == -1) {
        perror("munmap");
    }

    if (shm_unlink(SHM_NAME) == -1) {
        perror("shm_unlink");
    }
#endif 


    char *p = NULL;
    shm_fd = shm_open (SHM_NAME, O_RDWR, 0666);
   if (shm_fd < 0 ) 
    {
        printf("error, shm_fd:%d\r\n", shm_fd);
    } 
    else {
    printf("shm_fd:%d\r\n", shm_fd);
    }

    if (ftruncate(shm_fd, 4096) < 0 )
    {
        printf("ftruncate error\r\n");
    } 
    else {
       printf("ftruncate ok \r\n", shm_fd);
    }

    p = mmap (0, SHM_SIZE,PROT_WRITE,MAP_SHARED, shm_fd, 0);             
    if (p == NULL)
    {
         printf("error, p = %d\r\n", p);
    } 
    else 
    {     

    printf("p:%d\r\n", p);
    }   

    sprintf(p, "this is mmap test \r\n");

    getchar();

    if ( munmap(p, SHM_SIZE) < 0)
    {
         perror("munmap failed\r\n");
    } else {
    printf("munmap ok\r\n");
    }
     close (shm_fd);
   
    return EXIT_SUCCESS;
}
相关推荐
热爱编程的小白白4 小时前
【Playwright自动化】录制生成脚本
运维·自动化
ROCKY_8174 小时前
计算机网络考试考点——应用层
服务器·网络·计算机网络
e***74954 小时前
Modbus报文详解
服务器·开发语言·php
java_logo4 小时前
MySQL Server Docker 容器化部署指南
linux·运维·数据库·docker·容器
运维管理4 小时前
anolis openeuler 文件复制上用命令-学习篇
服务器
I***t7164 小时前
自己编译RustDesk,并将自建ID服务器和key信息写入客户端
运维·服务器
誰能久伴不乏4 小时前
Linux文件套接字AF_UNIX
linux·服务器·c语言·c++·unix
BJ_Bonree4 小时前
数智先锋 | 核心应用响应时常<1s、多终端崩溃率低至 0.1%!Bonree ONE 赋能蓝月亮应用性能与终端体验双重升级!
运维
张鱼小丸子4 小时前
电脑刷机教程:轻松重装系统指南
运维
a41324474 小时前
如何解决centos上oracle连接问题
linux·oracle·centos