为什么我的 "dup2(STDIN_FILENO, fd)" 无法向 grep 发送参数?
Why my "dup2(STDIN_FILENO, fd)" cannot send argument to grep?
日志:
@myLaptop:~/test$ gcc -o ./exe ./main.c
@myLaptop:~/test$ ./exe
Before waitpid: : Success
After dup2: : Success
.....(我的终端在这一行中等待......只能激活 Ctrl + C)
我认为 waitpid 等待的东西。 :(
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
system("rm -rf ./stdinTest");
system("ls -all / > ./stdinTest");
char **argv = (char **)malloc(sizeof(char *) * 3);
argv[0] = (char *)malloc(strlen("grep") + 1);
strcpy(argv[0], "grep");
argv[1] = (char *)malloc(strlen("bin") + 1);
strcpy(argv[1], "bin");
argv[2] = NULL;
int fd = open("./stdinTest", O_RDONLY);
pid_t cpid;
if ((cpid = fork()) == 0){
dup2(STDIN_FILENO, fd);
perror("After dup2: ");
execvp(argv[0], argv);
}
else{
perror("Before waitpid: ");
waitpid(cpid, NULL, 0);
perror("After waitpid: ");
close(fd);
}
return 0;
}
行 dup2(STDIN_FILENO, fd);
应该是 dup2(fd, STDIN_FILENO);
见http://man7.org/linux/man-pages/man2/dup.2.html
以下 code
可以工作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
system("rm -rf ./stdinTest");
system("ls -all / > ./stdinTest");
char **argv = (char **)malloc(sizeof(char *) * 3);
argv[0] = (char *)malloc(strlen("grep") + 1);
strcpy(argv[0], "grep");
argv[1] = (char *)malloc(strlen("bin") + 1);
strcpy(argv[1], "bin");
argv[2] = NULL;
int fd = open("./stdinTest", O_RDONLY);
pid_t cpid;
if ((cpid = fork()) == 0){
dup2(fd, STDIN_FILENO);
perror("After dup2: ");
execvp(argv[0], argv);
}
else{
perror("Before waitpid: ");
waitpid(cpid, NULL, 0);
perror("After waitpid: ");
close(fd);
}
return 0;
}
日志:
@myLaptop:~/test$ gcc -o ./exe ./main.c
@myLaptop:~/test$ ./exe
Before waitpid: : Success
After dup2: : Success
.....(我的终端在这一行中等待......只能激活 Ctrl + C) 我认为 waitpid 等待的东西。 :(
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
system("rm -rf ./stdinTest");
system("ls -all / > ./stdinTest");
char **argv = (char **)malloc(sizeof(char *) * 3);
argv[0] = (char *)malloc(strlen("grep") + 1);
strcpy(argv[0], "grep");
argv[1] = (char *)malloc(strlen("bin") + 1);
strcpy(argv[1], "bin");
argv[2] = NULL;
int fd = open("./stdinTest", O_RDONLY);
pid_t cpid;
if ((cpid = fork()) == 0){
dup2(STDIN_FILENO, fd);
perror("After dup2: ");
execvp(argv[0], argv);
}
else{
perror("Before waitpid: ");
waitpid(cpid, NULL, 0);
perror("After waitpid: ");
close(fd);
}
return 0;
}
行 dup2(STDIN_FILENO, fd);
应该是 dup2(fd, STDIN_FILENO);
见http://man7.org/linux/man-pages/man2/dup.2.html
以下 code
可以工作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
system("rm -rf ./stdinTest");
system("ls -all / > ./stdinTest");
char **argv = (char **)malloc(sizeof(char *) * 3);
argv[0] = (char *)malloc(strlen("grep") + 1);
strcpy(argv[0], "grep");
argv[1] = (char *)malloc(strlen("bin") + 1);
strcpy(argv[1], "bin");
argv[2] = NULL;
int fd = open("./stdinTest", O_RDONLY);
pid_t cpid;
if ((cpid = fork()) == 0){
dup2(fd, STDIN_FILENO);
perror("After dup2: ");
execvp(argv[0], argv);
}
else{
perror("Before waitpid: ");
waitpid(cpid, NULL, 0);
perror("After waitpid: ");
close(fd);
}
return 0;
}