gpt4 book ai didi

linux - IPC - 如何将命令输出重定向到子进程中的共享内存段

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:54:31 24 4
gpt4 key购买 nike

我试图将 Unix 命令输出重定向(写入)到子进程中的共享内存段,然后让父进程从父进程的同一个共享内存段中读回输出。经过几次徒劳的尝试后,我并没有取得太大的成功。谁能告诉我一个方法?提前致谢。

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#define SHM_SIZE 1024

int main()
{
key_t key; int shmid; char* data;

pid_t cpid=fork();

if (cpid<0)
{
fprintf(stderr,"Fork error!\n");
exit (-1);
}
else if (cpid==0) // child process
{
if ((key = ftok("mysh.c", 'R')) == -1)
{
perror("ftok");
exit(1);
}

// Connect to shared memory
if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1)
{
perror("shmget");
exit(1);
}

// Attach to the segment
data = shmat(shmid, (void *) 0, 0);
if (data == (char *) (-1))
{
perror("shmat");
exit(1);
}

system("ls -l");
// Stuck: How to redirect the output of "ls -l"
// to a shared memmory segment "data", so that parent process
// can retrieve it later?? Tried to
// do pipe and dup2 but none worked.

// Attempt via read?, but only garbage
read(STDIN_FILENO, data, SHM_SIZE);

}
else
{ // parent process
int st;

wait(&st);
printf("Output read from the child:\n");
if ((write(STDOUT_FILENO, data, SHM_SIZE)) < 0 )
{
perror("write 2");
exit(1);
}
}
}

======================

最佳答案

    system("ls -l");
// Stuck: How to redirect the output of "ls -l"
// to a shared memmory segment "data", so that parent process
// can retrieve it later?? Tried to
// do pipe and dup2 but none worked.

出于测试目的,我建议您从 stdin 读取,然后将它们写入 data


这是一个使用 POSIX 共享内存的例子(POSIX IPC API 优于 SYSV IPC API),子进程从 stdin 读取到共享内存区域,父进程写入共享内存的内容区域到 stdout:

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include <unistd.h>
#include <fcntl.h>
#include <semaphore.h>

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
const char *shm_name = "/dummy_cat_shm";
int shm_fd;
off_t shm_length;

const char *read_sem_name = "/dummy_cat_read";
const char *write_sem_name = "/dummy_cat_write";

sem_t *read_sem, *write_sem;
pid_t pid;
int buf_length;
char *write_ptr, *read_ptr;

buf_length = 1024;
shm_length = sizeof(buf_length) + buf_length;

/* Create semaphore */
read_sem = sem_open(read_sem_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, 0);
if (read_sem == SEM_FAILED) {
perror("sem_open");
goto clean_up3;
}
write_sem = sem_open(write_sem_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, 1);
if (write_sem == SEM_FAILED) {
perror("sem_open");
goto clean_up2;
}

/* Create shared memory segment */
shm_fd = shm_open(shm_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (shm_fd < 0) {
perror("shm_open");
goto clean_up1;
}

if (ftruncate(shm_fd, shm_length) < 0) {
perror("ftruncate");
goto clean_up0;
}

if ((pid = fork()) < 0) {
perror("fork");
goto clean_up0;
}
else if (pid == 0) {
write_ptr = mmap(NULL, shm_length, PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (write_ptr == MAP_FAILED) {
perror("mmap");
goto clean_up0;
}

char *buf = write_ptr+sizeof(buf_length);

while (sem_wait(write_sem) == 0) {
if (fgets(buf, buf_length, stdin) != NULL) {
*(int *)write_ptr = 1;
sem_post(read_sem);
}
else {
*(int *)write_ptr = 0;
sem_post(read_sem);
break;
}
}

munmap(write_ptr, shm_length);
}
else {
read_ptr = mmap(NULL, shm_length, PROT_READ, MAP_SHARED, shm_fd, 0);
if (read_ptr == MAP_FAILED) {
perror("mmap");
goto clean_up0;
}

char *buf = read_ptr + sizeof(buf_length);

while (sem_wait(read_sem) == 0) {
if (*(int *)read_ptr > 0) {
printf("%s", buf);
sem_post(write_sem);
}
else {
break;
}
}

munmap(read_ptr, shm_length);
}

clean_up0:
shm_unlink(shm_name);

clean_up1:
sem_unlink(write_sem_name);

clean_up2:
sem_unlink(read_sem_name);

clean_up3:
exit(EXIT_FAILURE);
}

注意:在这种情况下,这两个 mmap() 可以放在 fork() 之前。

编译:

gcc shm_exp.c -pthread -lrt

运行:

$ ls / | ./a.out 
bin/ home/ lib32/ mnt/ run/ sys/ vmlinuz@
boot/ initrd.img@ lib64/ opt/ sbin/ tmp/ vmlinuz.old@
dev/ initrd.img.old@ lost+found/ proc/ selinux/ usr@
etc/ lib/ media/ root/ srv/ var/

关于linux - IPC - 如何将命令输出重定向到子进程中的共享内存段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22496130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com