Tag: exec

execl的返回值

我正在运行execl函数来通过我的代码编译程序。 我想检查编译是否正确。 所以我写了b = execl(“usr/bin/gcc”,”cc”,path,NULL); 然后检查if( b==-1) 。 但即使汇编中存在错误,它也没有进入if语句。 有什么想法吗? 谢谢! int b=0; if ((pid1 = fork())<0) perror("Error forking"); else { if(pid1==0) b= execl("/usr/bin/gcc","cc",path,NULL); else wait(&status); } if(b==-1) printf("\n——–\n");

通过C中的argv使用管道发送和接收字符数组

所以,我正在尝试创建一个管道,通过argv []连接的管道来回发送char数组。 现在,我一直坚持在接口中接收数组(从父节点发送到子节点的c_param的参数)。在db.c中接收字符3和5。 我知道3和5是我的管道所在的argv []的索引,但我不知道如何接受它并在db.c中打印出我的消息。 interface.c创建管道,分叉到父进程和子进程。 char数组param被转移到子进程到char数组c_param。 使用snprintf,我将我的管道变成了一个char,使用execl和我的char数组c_param发送。 interface.c: int main (int argc, char *argv[]) { int to_Child[2]; int to_Parent[2]; int id, toChildPipe, toParentPipe, err; char param[100] = “This is the parameter!”; char sendPipe[100]; char recPipe[100]; /*CREATING PIPE*/ toChildPipe = pipe(to_Child); toParentPipe = pipe(to_Parent); if(toChildPipe == -1 || toParentPipe == -1) { printf (“Error on […]

execvpe函数的编译器警告

我有一个用c编写的程序,它使用execvpe(3)函数,我有一个行设置包含必需的头文件: #include 我使用以下命令编译此文件… gcc foo.c -o foo …只是为了得到以下警告: warning: implicit declaration of function ‘execvpe’ [-Wimplicit-function-declaration] 我遇到了与引用pthread_create(3)函数的文件类似的行为。 显然不同之处在于,pthread_create(3)手册页明确指出应该“编译并链接-pthread”,exec(3)函数族的手册页没有任何这样的指令。 此外,我在手册或在线中找不到exec(3)系列的类似编译器标志的任何参考。 我很感激您对此事的任何信息。 如果有一些标志我应该在编译时使用,或者如果我在寻找完全错误的解决方案,请告诉我。

C – 检索大于8位的子退出状态

注意:为简单起见,我没有包含太多错误检查,我的示例代码实际上没有任何实际用途。 我想要的是: 我想要一个fork()是一个子进程的程序,并让它使用execl()调用一个进程。 然后我的父母检索该进程的退出代码。 这是相当微不足道的。 我尝试了什么: int main(int argc, char** argv) { int ch = fork(); if(ch == -1) { perror(NULL); }else if(ch == 0) { // In child, invoke process execl(“/path/process”, “process”, 0); }else { // In parent, retrieve child exit code int status = 0; wait(&status); // print exit status if(WIFEXITED(status)) printf(“%d\n”, WEXITSTATUS(status)); […]

使用execvp在输出重定向期间接收错误代码

我试图将输出从ls重定向到我在C中创建的shell中的文件。我输入: ls > junk 我得到的是: ls: cannot access >: No such file or directory 然后,如果我使用CTRL-D退出shell,它会在退出之前将ls命令的结果打印到屏幕。 我尝试使用print语句来确定它发生的位置,并且在以下情况之后不打印任何打印语句: dup2(f, STDOUT_FILENO); Also tried dup2(f, 1); 码: pid = fork(); if(pid == 0) { // Get the arguments for execvp into a null terminated array for(i = 0; i “) == 0) //numargs is the number of arguments typed […]

命令行参数未在C中执行

我试图将参数传递给ls命令的execl()函数。 但是当我通过时 /bin/ls -l -a 作为我的程序的参数,execl()函数不识别最后两个参数。 为什么?这是代码: #include #include int main(int argc, char *argv[]) { int i,childpid; if(argc!=4) { printf(“you didn’t provide any commandline arguments!\n”); return 0; } childpid=fork(); if(childpid0); printf(“My ID %d, Parent ID %d, CHild ID %d\n”, getpid(),getppid(),childpid); return 0; } 我在Ubuntu上。 问候

如何处理execvp中的错误?

我写了一个小程序(带有来自SO的代码),方便了printenv | sort | less printenv | sort | less printenv | sort | less 。 现在我想实现error handling,我从execvp开始。 它只是检查返回值还有什么? AFAIK我只检查返回值,如果它在此函数中为0则return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv); 。 那是对的吗? #include #include #include #include #include #include struct command { const char **argv; }; /* Helper function that spawns processes */ int spawn_proc (int in, […]

execl之前的打印在输出中不可见

#include #include #include #include #include int main(void) { pid_t Checksum_pid = fork(); if (Checksum_pid 0) { if (WIFEXITED(childStatus)) printf(“\nChild Exit Code: %d\n”, WEXITSTATUS(childStatus)); else printf(“\nChild Exit Status: 0x%.4X\n”, childStatus); } else if (returnValue == 0) printf(“\nChild process still running\n”); else { if (errno == ECHILD) printf(“\nError ECHILD!!\n”); else if (errno == EINTR) printf(“\nError EINTR!!\n”); else […]

execvp实际上做了什么?

可能重复: 编写shell – 如何执行命令 我的任务是在C中编写一个shell。到目前为止,我理解execvp将尝试在arg1中运行arg2作为参数的程序。 现在看来这样做了 execvp (“ls”, args); //assume args is {“ls”, “>”, “awd.txt”} 不等于在控制台中键入它 ls > awd.txt 我意识到我需要使用freopen来实现相同的结果,但我很好奇execvp正在做什么。

使用execve时传递整数参数

我是使用C语言系统调用编程的初学者。我试图在我的一个程序中使用execve调用。 我必须将一个整数作为参数传递给通过execve调用的程序。 但是,在互联网上阅读并查看示例代码,我可以看到我们只能将字符串作为参数传递。 所以,我尝试使用’sprintf’将整数转换为字符串,然后使用’strcpy’将该字符串复制到必须通过execve传递的字符串数组的一个元素中。 但是使用strcpy会导致分段错误。 我通过调试检查了这一点,如果我不使用strcpy但只是写一些像 – myargv [1] =“123”; 然后整个程序工作正常。 但由于我必须传递一个变量整数作为参数而不是常量,我不能使用上面的方法。 这个问题一直困扰着我。 请帮助我解决我应该做的事情。