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;
}
相关推荐
liyongjun63161 小时前
CentOS 下 Zookeeper 常用命令与完整命令列表
linux·服务器·zookeeper·centos
facaixxx20242 小时前
什么是巨型帧Jumbo Frames?云服务器开启巨型帧有什么用?
运维·服务器
巨可爱熊4 小时前
高并发内存池(定长内存池基础)
linux·运维·服务器·c++·算法
zkmall4 小时前
ZKmall开源商城静态资源管理:Nginx 配置与优化
运维·nginx·开源
小小毛桃6 小时前
在Ubuntu系统中运行Windows程序
linux·windows·ubuntu
一一Null7 小时前
关于手机取证中逻辑采集与系统备份的差异
服务器·网络·智能手机
码农新猿类7 小时前
服务器本地搭建
linux·网络·c++
小度爱学习7 小时前
linux中的执行命令格式及命令帮助
linux·运维·chrome
yangshuo12817 小时前
如何在服务器上搭建mail服务器邮件服务器
运维·服务器