Tag: execv

execv如何从管道获取输出?

参考旧的作业问题: /* implementing “/usr/bin/ps -ef | /usr/bin/more” */使用管道/* implementing “/usr/bin/ps -ef | /usr/bin/more” */ #include #include #include int main() { int fds[2]; int child[2]; char *argv[3]; pipe(fds); if (fork()== 0) { close(fds[1]); close(STDIN_FILENO); dup(fds[0]); /* redirect standard input to fds[1] */ argv[0] = “/bin/more”; argv[1] = NULL; /* check how the argv array is set […]

fork()kill()和execv()

我在这里尝试一个小unit testing。 但该计划并不像我预期的那样有效。 char *args[2]; args[0] = (char*)”/usr/bin/firefox”; args[1] = NULL; pid = fork(); printf(“forked and my pid is %d\n”,pid); //check for errors if (pid<0){ printf("Error: invoking fork to start ss has failed, Exiting\n "); exit(1); } //the child process runs firefox if (pid==0){ if (execv(args[0],args)0){ sleep(3); kill(get_pidof(string(“firefox”)),SIGUSR1); } 现在,我希望程序运行如下:1。孩子运行firefox(它确实如此)2。爸爸打印DAD即将杀死U(到目前为止一样好)3。firefox会打开3秒钟然后关闭(也是这样)4。子进程将完成运行execv并打印“IVE BEEN KILLED”。 这不会发生。 我的目的是要知道execv运行的程序已经通过在execv继续运行之后立即引发一些标志来完成(其中我已被杀死了printf)。 除非必要,否则我有几千行的代码并且不想使用其他方法。 […]

Execv Linux printf不起作用

我正在尝试使用此c代码运行可执行文件: int main(int argc, char *argv[]) { printf(“hello.\n”); sleep(2); if (execlp(“ls”,”ls”,”-l”,NULL) == -1) printf(“Error occured during execute ls.\n”); return 0; } 为什么printf(“你好\ n”)不起作用? 即使我睡觉了?

如何将重定向运算符’>’作为execv的参数传递?

在linux终端中,我可以输入 echo hello! > /path/to/file 我以为我可以用execv做同样的事情: #include #include #include int main(void){ char *write_cmd[] = { “echo”, “hello!”, “>”, “/path/to/file”, NULL}; if (fork() == 0){ execv(“/bin/echo”, write_cmd); } else{ sleep(1); } return 0; } 但是,这段代码不写’你好!’ 到文件,这是我想要它做的。 有没有其他方法使用execv和echo来做到这一点? 编辑:我也尝试过使用dup2作为解决方案:#include #include #include int main(void){ char *write_cmd[] = { “echo”, “hello!”, NULL }; if (fork() == 0){ int tmpFd […]

C execv()函数是否终止子进程?

下面是我的代码细分。 我有一个程序,它会分叉一个孩子(并将孩子的pid注册到一个文件中),然后做自己的事情。 孩子成为程序员用argv尊严的任何程序。 当子进程执行完毕后,它会向父进程发送一个信号(使用SIGUSR1),以便父进程知道从文件中删除子进程。 父节点应该停止一秒,通过更新其表来确认已删除的条目,并从中断处继续。 pid = fork(); switch(pid){ case -1:{ exit(1); } case 0 :{ (*table[numP-1]).pid = getpid(); //Global that stores pids add(); //saves table into a text file freeT(table); //Frees table execv(argv[3], &argv[4]); //Executes new program with argv printf(“finished execution\n”); del(getpid()); //Erases pid from file refreshReq(); //Sends SIGUSR1 to parent return 0; } […]

编写解释器或更像是命令提示程序

我应该编写一个更像是命令提示符的解释程序。 这是一些背景信息: General flow of basic interpreter 1. Prompt for user request. 2. Carry out the user request. 3. Unless user terminates the program, go to step 1. Run a command. Format: R command_path [arg1 to arg4] //a single character ‘R’ which stands for ‘Run’, followed by the path of the command //the user can […]

如何让gdb跟随execv? 尽管“跟随执行模式”不工作

我写了两个简单的程序: int main(int ac, char **argv ) { execv( “/home/me/Desktop/execvtest2”, argv ); } 和 int main(int ac, char **argv ) { execv( “/home/me/Desktop/execvtest1”, argv ); } 我用gcc -g将它们编译成相应的输出文件。 我使用gcc(Ubuntu / Linaro 4.4.4-14ubuntu5.1)4.4.5运行Ubuntu 10.10。 当我使用GNU gdb(GDB)7.2-ubuntu调试第一个程序时,我可以直到第一个execv语句,但随后两个文件继续运行。 即使我将follow-exec-mode设置为new,我也无法进入第二个程序。 当我设置catch exec时,gdb会在每次调用execv时停止(某些如何没有第二个程序的链接源,我无法退出gdb,因为它有点挂起!?),但我无法跳过调用“新”(作为exec替换过程)的劣质程序。 那怎么办呢? 必须有一种方法可以进入新的流程吗? 难道我做错了什么? 干杯

指针失去其值+ execv编译警告

我希望我没有错过类似的问题。 我正在尝试使用原始C函数编写自己的迷你shell。 我得到了一些应该可以工作的东西,但是我有一个指针可以解决所有问题。 我的adrCmd指针应该从searchCmd()函数获取命令路径字符串,并在main函数中保持相同的值。 实际上:它指向searchCmd()上的正确值,但不searchCmd() main() 。 这是代码: int searchCmd(char* cmd, char* adrCmd){ char* path = getenv(“PATH”); if(debug)printf(“PATH : %s\n”, path); int nbPath = (compteLettre(path, ‘:’)+1); char** pathTab = malloc(nbPath*sizeof(char*)); decompose(path, pathTab, 2048, ‘:’); int i; char* adr = malloc(sizeof(char*)); for(i=0; i<nbPath; i++){ sprintf(adr, "%s/%s", pathTab[i], cmd); if(debug)printf(" source : %s \n", adr); int fs […]