execvp

Pipe function in Linux shell write in C

早过忘川 提交于 2021-01-28 04:08:08
问题 My mini-shell program accepts pipe command, for example, ls -l | wc -l and uses excevp to execute these commands. My problem is if there is no fork() for execvp, the pipe command works well but the shell terminates afterward. If there is a fork() for execvp, dead loop happens. And I cannot fix it. code: void run_pipe(char **args){ int ps[2]; pipe(ps); pid_t pid = fork(); pid_t child_pid; int child_status; if(pid == 0){ // child process close(1); close(ps[0]); dup2(ps[1], 1); //e.g. cmd[0] =

信息安全系统设计基础第十二周学习总结

牧云@^-^@ 提交于 2020-02-20 00:53:50
实践 一、实践代码总结 1. execvp()函数 函数说明: execvp()会从PATH 环境变量所指的目录中查找符合参数file 的文件名,找到后便执行该文件,然后将第二个参数argv传给该欲执行的文件。 返回值 如果执行成功则函数不会返回,执行失败则直接返回-1,失败原因存于errno中。 exec1.c: (源代码中第二个printf语句消失了,原因是调用execvp函数时,内核将新程序载入到当前进程,替代当前进程的代码和数据。) exec2.c: (exec2与exec1的区别就在于,execvp函数调用的语句变成了 execvp( arglist[0] , arglist ); 编译运行结果与exec1.c完全相同,说明arglist数组的第一项为要运行的程序的名称。) exec3.c: 2. fork()函数 函数说明: 在Unix/Linux中用fork函数创建一个新的进程。进程是由当前已有进程调用fork函数创建,分叉的进程叫子进程,创建者叫父进程。该函数的特点是调用一次,返回两次,一次是在父进程,一次是在子进程。两次返回的区别是子进程的返回值为0,父进程的返回值是新子进程的ID。子进程与父进程继续并发运行。如果父进程继续创建更多的子进程,子进程之间是兄弟关系,同样子进程也可以创建自己的子进程,这样可以建立起定义关系的进程之间的一种层次关系。

execvp/fork — how to catch unsuccessful executions?

邮差的信 提交于 2020-01-30 19:24:27
问题 Right now I'm writing a C program that must execute a child process. I'm not doing multiple child processes simultaneously or anything, so this is fairly straightforward. I am definitely executing the built-in shell programs (i.e. things like cat and echo) successfully, but I also need to be able to tell when one of these programs fails to execute successfully. I'm trying this with the following simplified code: int returnStatus; // The return status of the child process. pid_t pid = fork();

Does the argument list pass the string quotes to exec command in C?

蹲街弑〆低调 提交于 2020-01-15 06:31:20
问题 I am using execvp for execing a new process for the command grep -l night * . Here is my code: char * argument[5]; char keyword[] = "night"; argument[0] = (char *) malloc (sizeof(char)*25); argument[1] = (char *) malloc (sizeof(char)*25); argument[2] = (char *) malloc (sizeof(char)*25); argument[3] = (char *) malloc (sizeof(char)*25); argument[4] = (char *) malloc (sizeof(char)*25); argument[0] = "grep"; argument[1] = "-l"; strcpy(argument[2],keyword); argument[3] = "*"; argument[4] = NULL;

run a program in background with execvp system call in c

六月ゝ 毕业季﹏ 提交于 2020-01-07 05:45:13
问题 i'm writing a program that recieves a command name and arguments and optionally the string "bg" at the end , if the "bg" string is passed my program should execute the command with its arguments in background if not in foreground, here's my code: #include<sys/types.h> #include<sys/wait.h> #include<unistd.h> #include<stdio.h> #include<errno.h> #include <stdlib.h> #include <string.h> int main(int argc, char* argv[]) { pid_t pid; int state; if( (pid=fork())<0) { perror("\nError in fork"); exit(

Using cat and execvp

99封情书 提交于 2019-12-25 06:34:57
问题 Trying to understand why this section of code using the cat command isn't working with execvp in C. char *in[5] ={"cat", "file1.txt", ">>", "file2.txt", 0}; execvp(in[0], in); When I run it displays the contents of file1.txt but then says: cat: >> No such file or directory. Then displays the contents of file2.txt Why wouldn't it recognize the >> operator in this instance? 回答1: You can read the "man tee" command which it read from standard input and write to standard output and files. You

exevp skips over all code until wait call in c

时光总嘲笑我的痴心妄想 提交于 2019-12-25 01:55:30
问题 I am trying to execute a file using fork and execvp , however I am encountering some errors. I have not found any solutions to the problem I am having here online, since I don't get any errors from my exevp nor does it run. Here is my code: pid_t child; int status; child = fork(); char *arg[3] = {"test","/home/ameya/Documents/computer_science/cs170/project1", (char*) 0}; if(child == 0){ printf("IN CHILD BEFORE EXECVP\n"); int value = execvp(arg[0],arg); if(value < 0){ printf("ERROR\n"); }else

Waiting for execvp in main

旧街凉风 提交于 2019-12-24 18:58:42
问题 int main() { ... if(!fork()) { execvp(cmdName,cmdParam); } printf("In main()..."); return(0); } Assuming I have correctly passed the cmdName & cmdParam arguments, how do I wait for the process created by execvp to finish, before resuming the execution of main()? Does the execvp() create a process which is a child of the newly fork()ed process? 回答1: In the parent process, fork returns the PID of the child process, so you can store that in a variable, and then use waitpid to wait for the child

How to save execvp output

安稳与你 提交于 2019-12-24 12:45:30
问题 I have problem with saving the execvp output.I want to save the output of (ps -eo pid,fname,state,ppid,gid,sid) in txt file . This is my code : #include <unistd.h> int main(void) { char* args[]={"ps","-eo","pid,fname,state,ppid,gid,sid" , ">" , "t.txt"}; execvp(args[0],args); return 0; } But when i run it .It doesnt work . 回答1: Since you're wiping out your process by directly calling execvp , you could simply redirect the output to your file: int main() { char * args[] = {"ps","-eo","pid

Node.js - spawned process is generating error “execvp(): No such file or directory”

我怕爱的太早我们不能终老 提交于 2019-12-24 00:44:09
问题 I have the following code that's intended to spawn and detach a child process, which is just another node.js script in the same directory. Here's the exact code I'm running: var fs = require('fs'); var child = require('child_process'); var out = fs.openSync('/tmp/daemon.log', 'a'); var options = { cwd: process.cwd(), env: process.env, detached: true, stdio: ['ignore', out, process.stderr] }; child.spawn('/usr/local/bin/node ./daemon.js', [], options).unref(); All daemon.js does right now is