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;
}
相关推荐
tongsound12 小时前
记录一次崩溃问题排查过程(gtsam库相关,avx)
linux·c++
妹妹够啦12 小时前
宝塔部署-Nginx配置
运维·nginx·junit
love530love12 小时前
【笔记】解决 ComfyUI 安装节点 ComfyUI-Addoor (葵花宝典)后启动报错:No module named ‘ComfyUI-Addoor’
linux·运维·前端·人工智能·windows·笔记·python
Shelley种了一只橙子13 小时前
第一章部署 chrony服务器
运维·服务器
Full Stack Developme13 小时前
Linux 有哪些功能相似的命令
linux·运维·服务器
低音钢琴13 小时前
【从零开始构建性能测试体系-08】如何诊断性能瓶颈:从服务器到数据库的全方位分析
服务器·数据库·php
馨谙13 小时前
Linux 服务管理:重新加载 vs 重新启动的本质区别
linux·运维·服务器
小白银子13 小时前
零基础从头教学Linux(Day 55)
java·linux·服务器·python
岚天start14 小时前
Linux内核coredump分析方案
linux·运维·服务器·gdb·coredump·堆栈·内存快照
屁股割了还要学14 小时前
【Linux入门】常用工具:yum、vim
linux·运维·服务器·c语言·c++·学习·考研