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;
}
相关推荐
求知若渴,虚心若愚。1 小时前
Error reading config file (/home/ansible.cfg): ‘ACTION_WARNINGS(default) = True
linux·前端·ansible
π大星星️2 小时前
Nginx 四层(stream)反向代理 + DNS 负载均衡
运维·nginx·负载均衡
beyoundout3 小时前
HAproxy
linux·运维·服务器
qq_218753315 小时前
服务器查日志太慢,试试grep组合拳
运维·服务器
Jie_176 小时前
【linux】高可用集群Keepalived
linux·运维·服务器
思绪漂移6 小时前
阿里云【免费试用】Elasticsearch 智能运维 AI 助手
运维·elasticsearch·阿里云
aiprtem6 小时前
LVGL + ESP-Brookesia 嵌入式模拟桌面应用开发
linux·c语言·物联网
21号 17 小时前
4.应用层自定义协议与序列化
运维·服务器·网络
xx.ii8 小时前
4.Linux 应用程序的安装和管理
linux·服务器·网络
奋斗的蛋黄9 小时前
解析分区、挂载与块设备:Linux 存储管理核心命令详解
linux·服务器·网络