gpt4 book ai didi

c - pipe() 数据未传输到子进程

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:09 24 4
gpt4 key购买 nike

我正在尝试写入管道并在执行到另一个文件时读取它的内容,但是由于某种原因我无法让它工作。

这是main.c文件

int main(int argc, char * argv[]) {
int pipe_descs[2];
int matrix[SIZE][SIZE];
int fdr, fdw; // file descriptors
int i;
pid_t status=0;
if (pipe(pipe_descs) == -1) {
fprintf(stderr, "cannot open");
exit(1);
}
fdr = open(argv[1], O_RDONLY); // open files
fdw = open("gg.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);

if (fdr < 0 || fdw < 0) { //validation for error
perror("failed to open input or output files");
exit(EXIT_FAILURE);
}
removeSpaces(matrix, fdr, fdw);
status=fork();
if (status < 0) {
fputs("error in fork", stderr);
exit(EXIT_FAILURE);
}
close(pipe_descs[0]);
close(STDOUT_FILENO);
dup(pipe_descs[1]);
write(pipe_descs[1],matrix,sizeof(matrix));
if(status == 0) {
execl("rowsValidation", "rowsValidation", NULL);
/*dup2(pipe_descs[IN],0);
dup2(pipe_descs[OUT],4);
close(STDOUT_FILENO);*/
}

这是另一个尝试从缓冲区读取数据但没有任何反应的文件。

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char * argv[]){
int pipe_descs[2];
int mat[9][9];


int fdr, fdw; // file descriptors
char ans='9';
int i, j;
printf("%s","got here");
close(pipe_descs[1]);
close(STDIN_FILENO);
dup(pipe_descs[0]);
read(pipe_descs[0],mat,sizeof(mat));

for (i = 0; i < 9; i++) { /* Iterate of each row */
for (j = 0; j < 9; j++) { /* In each row, go over each col element */
printf("%d ", mat[i][j]); /* Print each row element */
}
printf("\n"); /* Finish a row, start a new line */
}

exit(0);
}

在阅读了评论并在网上阅读了更多内容后,我将其更改为我试着用 char 检查它,但我仍然无法让它工作。父亲将 char 写入管道,儿子对新进程执行 exec,然后从管道中读取,但仍然无法正常工作。请帮我解决一下

int main(int argc, char * argv[]) {
int pipe_descs[2];
int matrix[SIZE][SIZE];
int fdr, fdw; // file descriptors
int i;
char a='10';
pid_t status=0;
if (pipe(pipe_descs) == -1) {
fprintf(stderr, "cannot open");
exit(1);
}
fdr = open(argv[1], O_RDONLY); // open files
fdw = open("gg.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fdr < 0 || fdw < 0) { //validation for error
perror("failed to open input or output files");
exit(EXIT_FAILURE);
}
removeSpaces(matrix, fdr, fdw);
status=fork();
if (status < 0) {
fputs("error in fork", stderr);
exit(EXIT_FAILURE);
}
if(status == 0) {

dup2(pipe_descs[0], 0); // read the matrix from pipe1 to 0
close(pipe_descs[0]);
dup2(pipe_descs[1], 4);
printf("Execl1 start\n");
// write to 4 instead of pipe1[1]
execl("rowsValidation", "rowsValidation", NULL);


}
else{

write(pipe_descs[1],&a,sizeof(char));
close(pipe_descs[1]);


/*char buffer[10];
close(pipe_descs[1]);
close(STDIN_FILENO);
dup(pipe_descs[0]);
read(pipe_descs[0],buffer,sizeof(buffer));
printf("%s",buffer);
close(pipe_descs[0]);*/
}
close(fdr); // close the files
close(fdw);
exit(EXIT_SUCCESS);
}



int main(int argc, char * argv[]){
int pipe_descs[2];
int mat[9][9];


int fdr, fdw; // file descriptors
char ans;
int i, j;

read(0,&ans,sizeof(char));
printf("%c ", ans);
for (i = 0; i < 9; i++) { /* Iterate of each row */
for (j = 0; j < 9; j++) { /* In each row, go over each col element */
printf("%d ", mat[i][j]); /* Print each row element */
}
printf("\n"); /* Finish a row, start a new line */
}
/* if (pipe(pipe_descs) == -1) {
fprintf(stderr, "cannot open");
exit(1);
}*/
//write(4,1,sizeof(char));
exit(0);
}

最佳答案

我认为您对自己想要什么以及如何去做略有了解,但您还没有 100% 做到这一点。如果你想创建一个管道,在那里写入信息并让另一个程序读取它:你首先创建管道,通过 fork() 创建一个新进程,例如,(有些人建议不要使用 fork: https://www.microsoft.com/en-us/research/publication/a-fork-in-the-road/ )并通过 dup2()(和 dup())调用更改文件描述符。创建管道、 fork 和​​执行 exec 的过程可以通过 popen() 调用轻松完成,我建议您研究一下。

下面是一个可能对您有所帮助的示例:

char* generate_hash(char* password, char* salt)
{
char password_plus_salt[MAX_PASSWORD_LEN + SALT_LEN];
strcpy(password_plus_salt, password);
strcat(password_plus_salt, salt);

int fd[2];
pipe(fd);

int stdout_save = dup(STDOUT_FILENO);
dup2(fd[WRITE], STDOUT_FILENO);

FILE* input = popen("sha256sum", "w");

fprintf(input, password_plus_salt, "%s");

FILE* output = fdopen(fd[READ], "r");

pclose(input);

char* hash = (char*)malloc(sizeof(char) * (HASH_LEN + 1));
memset(hash, '\0', (HASH_LEN + 1) * sizeof(char));

for (int i = 0; i < HASH_LEN; i++) {
hash[i] = (char)fgetc(output);
}

dup2(stdout_save, STDOUT_FILENO);

return hash;
}

我还建议您切换错误检查并更改以下内容:

if (fdr < 0 || fdw < 0) { //validation for error
perror("failed to open input or output files");
exit(EXIT_FAILURE);
}

对于:

if (fdr < 0) 
{
perror("fdr");
exit(EXIT_FAILURE);
}
if (fdw < 0)
{
perror("fdw");
exit(EXIT_FAILURE);
}

因此您可以确定错误的来源。

关于c - pipe() 数据未传输到子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56082740/

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