我正在尝试写入管道,并在exec到另一个文件时读取管道的内容,但是由于某种原因,我无法使其工作。
这是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写入管道,儿子将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);
}
答案 0 :(得分:2)
我认为您对自己想要的东西和如何做有一点了解,但是您并不百分百地了解那里。 如果要创建管道,请在其中写入信息,然后让另一个程序读取它:首先创建管道,然后通过fork()创建一个新进程,(有些人建议不要使用fork:{{ 3}}),并通过dup2()(和dup())调用更改文件描述符。可以通过popen()调用轻松地完成创建管道,派生并执行exec的过程,我建议您仔细研究一下。
以下示例可能会为您提供帮助:
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);
}
因此您可以确定错误的根源。