欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 明星 > linuxbash原理

linuxbash原理

2025/4/18 18:01:49 来源:https://blog.csdn.net/yangang185/article/details/147232908  浏览:    关键词: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 4096int 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 4096int 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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词