问题描述
我添加错误处理我小的C程序,我已经得到了它与叉子和execvp而不是dup2工作。
INT spawn_proc(INT中,诠释出,结构命令* CMD){
将为pid_t PID;
如果((PID =叉())== 0){
如果(在!= 0){
dup2(在0);
逼近);
}
如果(满分!= 1){
dup2(出,1);
关闭(出);
}
如果(execvp(CMD->的argv [0],(char * const的*)CMD->的argv)小于0){
PERROR(execvp失败);
出口(1);
}
}否则如果(PID℃,){
PERROR(叉失败);
出口(1);
}
返回PID;
}
应该如何与PERROR dup2使用?我问过这件事,但是我不认为这对dup2答案是正确的,因为该计划为,如果不带参数运行,如果我用code从这里的答案dup2错误退出。
我的C程序是这样的。
的#include< SYS / types.h中>
#包括LT&;&errno.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&string.h中GT;
结构命令
{
为const char ** argv的;
};
/ *辅助函数,产生进程* /
INT spawn_proc(int类型,int的列,结构命令* CMD){
将为pid_t PID;
如果((PID =叉())== 0){
如果(在!= 0){
dup2(在0);
逼近);
}
如果(满分!= 1){
dup2(出,1);
关闭(出);
}
如果(execvp(CMD->的argv [0],(char * const的*)CMD->的argv)小于0){
PERROR(execvp失败);
出口(1);
}
}否则如果(PID℃,){
PERROR(叉失败);
出口(1);
}
返回PID;
}
/ *辅助函数叉管* /
INT fork_pipes(INT N,结构命令* CMD){
INT I;
int类型,FD [2];
对于(i = 0; I< N - 1 ++ I){
管(FD);
spawn_proc(中,FD [1],CMD + I);
关闭(FD [1]);
在= FD [0];
}
dup2(在0);
返回execvp(CMD [I] .argv [0],(char * const的*)CMD [I] .argv);
}INT主(INT ARGC,字符** argv的){
INT I;
如果(ARGC == 1){/ *有没有参数* /
为const char * printenv [] = {printenv,0};
为const char *排序[] = {排序,0};
为const char *更少[] = {少,0};
结构命令CMD [] = {{printenv},{排序},{少}};
返回fork_pipes(3,CMD);
}
如果(ARGC大于1){/ *我想一个参数* / 如果(STRNCMP(的argv [1],CD,2)及和放大器; STRNCMP(的argv [1],退出,2)){
字符* tmp目录;
INT LEN = 1;
对于(i = 1; I< ARGC,我++)
{
LEN + = strlen的(的argv [I])+ 2;
}
TMP =(字符*)malloc的(LEN);
TMP [0] ='\\ 0';
INT POS = 0;
对于(i = 1; I< ARGC,我++)
{
POS + = sprintf的(TMP + POS,%s%S(我== 1:?|)的argv [I]);
}
为const char * printenv [] = {printenv,0};
为const char *的grep [] = {grep的,-E,TMP,NULL};
为const char *排序[] = {排序,0};
为const char *更少[] = {少,0};
结构命令CMD [] = {{printenv},{grep的},{排序},{少}};
返回fork_pipes(4,CMD);
免费(TMP);
}否则如果(!STRNCMP(的argv [1],CD,2)){/ *更改目录* /
的printf(更改目录到%s \\ n,argv的[2]);
CHDIR(的argv [2]);
}否则如果(!STRNCMP(的argv [1],退出,2)){/ *更改目录* /
的printf(退出\\ n);
出口(0);
}
}
出口(0);
}
更新
当我运行这个code,当不带参数运行程序diesn't显示任何输出。什么是错的?
INT spawn_proc(INT中,诠释出,结构命令* CMD){
将为pid_t PID;
如果((PID =叉())== 0){
如果(在!= 0){
如果(dup2(在0)== - 1){
PERROR(dup2失败);
出口(1);
}
dup2(在0);
逼近);
}
如果(满分!= 1){
dup2(出,1);
关闭(出);
}
如果(execvp(CMD->的argv [0],(char * const的*)CMD->的argv)小于0){
PERROR(execvp失败);
出口(1);
}
}否则如果(PID℃,){
PERROR(叉失败);
出口(1);
}
返回PID;
}
您使用它同大多数其他系统调用。您检查错误的返回值( 1
),然后调用 PERROR
来显示错误。
如果(dup2(在0)== -1){
PERROR(dup2失败);
出口(1);
}
I'm adding error handling to my small C program and I've gotten it to work with fork and for execvp but not for dup2.
int spawn_proc (int in, int out, struct command *cmd) {
pid_t pid;
if ((pid = fork ()) == 0) {
if (in != 0) {
dup2 (in, 0);
close (in);
}
if (out != 1) {
dup2 (out, 1);
close (out);
}
if (execvp(cmd->argv [0], (char * const *)cmd->argv) < 0) {
perror("execvp failed");
exit(1);
}
} else if (pid < 0) {
perror("fork failed");
exit(1);
}
return pid;
}
How should perror be used with dup2 ? I asked about it before but I don't think that the answer for dup2 was correct since the program exits with an error for dup2 if run with no arguments if I use the code from the answer here.
My C program is this.
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
struct command
{
const char **argv;
};
/* Helper function that spawns processes */
int spawn_proc (int in, int out, struct command *cmd) {
pid_t pid;
if ((pid = fork ()) == 0) {
if (in != 0) {
dup2 (in, 0);
close (in);
}
if (out != 1) {
dup2 (out, 1);
close (out);
}
if (execvp(cmd->argv [0], (char * const *)cmd->argv) < 0) {
perror("execvp failed");
exit(1);
}
} else if (pid < 0) {
perror("fork failed");
exit(1);
}
return pid;
}
/* Helper function that forks pipes */
int fork_pipes (int n, struct command *cmd) {
int i;
int in, fd [2];
for (i = 0; i < n - 1; ++i) {
pipe (fd);
spawn_proc (in, fd [1], cmd + i);
close (fd [1]);
in = fd [0];
}
dup2 (in, 0);
return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}
int main (int argc, char ** argv) {
int i;
if (argc == 1) { /* There were no arguments */
const char *printenv[] = { "printenv", 0};
const char *sort[] = { "sort", 0 };
const char *less[] = { "less", 0 };
struct command cmd [] = { {printenv}, {sort}, {less} };
return fork_pipes (3, cmd);
}
if (argc > 1) { /* I'd like an argument */
if (strncmp(argv[1], "cd", 2) && strncmp(argv[1], "exit", 2)) {
char *tmp;
int len = 1;
for( i=1; i<argc; i++)
{
len += strlen(argv[i]) + 2;
}
tmp = (char*) malloc(len);
tmp[0] = '\0';
int pos = 0;
for( i=1; i<argc; i++)
{
pos += sprintf(tmp+pos, "%s%s", (i==1?"":"|"), argv[i]);
}
const char *printenv[] = { "printenv", 0};
const char *grep[] = { "grep", "-E", tmp, NULL};
const char *sort[] = { "sort", 0 };
const char *less[] = { "less", 0 };
struct command cmd [] = { {printenv}, {grep}, {sort}, {less} };
return fork_pipes (4, cmd);
free(tmp);
} else if (! strncmp(argv[1], "cd", 2)) { /* change directory */
printf("change directory to %s\n" , argv[2]);
chdir(argv[2]);
} else if (! strncmp(argv[1], "exit", 2)) { /* change directory */
printf("exit\n");
exit(0);
}
}
exit(0);
}
Update
When I run this code, the program diesn't display any output when run with no arguments. What is wrong?
int spawn_proc (int in, int out, struct command *cmd) {
pid_t pid;
if ((pid = fork ()) == 0) {
if (in != 0) {
if (dup2(in, 0) == -1) {
perror("dup2 failed");
exit(1);
}
dup2 (in, 0);
close (in);
}
if (out != 1) {
dup2 (out, 1);
close (out);
}
if (execvp(cmd->argv [0], (char * const *)cmd->argv) < 0) {
perror("execvp failed");
exit(1);
}
} else if (pid < 0) {
perror("fork failed");
exit(1);
}
return pid;
}
You use it the same as most other system calls. You check for an error return value (-1
), and then call perror
to display the error.
if (dup2(in, 0) == -1) {
perror("dup2 failed");
exit(1);
}
这篇关于如何使用PERROR与dup2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!