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;
}
相关推荐
tritone1 天前
使用阿贝云免费云服务器学习Vagrant,是一次非常顺畅的体验。作为一名开发者
服务器·学习·vagrant
wangjialelele1 天前
Linux中的进程管理
java·linux·服务器·c语言·c++·个人开发
SAP工博科技1 天前
SAP 公有云 ERP 多工厂多生产线数据统一管理技术实现解析
大数据·运维·人工智能
YongCheng_Liang1 天前
从零开始学虚拟化:桌面虚拟化(VDI)入门指南(架构 + 产品 + 部署)
运维·云计算
杜子不疼.1 天前
【Linux】库制作与原理(二):动态库的制作与使用
linux·运维·服务器
森焱森1 天前
嵌入式硬件工程师应知 白银快速分析报告
linux·c语言·arm开发·嵌入式硬件·去中心化
小白电脑技术1 天前
飞牛漏洞焦虑?别瞎折腾WAF了!用Lucky五步搞定“防爬墙”
服务器·网络·安全
消失的旧时光-19431 天前
Nginx 是什么?为什么它不写在代码里?——从 0 认识 Nginx
运维·服务器·nginx
BJ_Bonree1 天前
4月17日,博睿数据受邀出席GOPS全球运维大会2026 · 深圳站!
大数据·运维·人工智能
RisunJan1 天前
Linux命令-lpq(查看打印队列状态)
linux·运维·服务器