execv

How to inherit stdin and stdout in python by using os.execv()

前提是你 提交于 2020-01-09 11:45:35
问题 First, I wrote a c++ code as follows: #include <cstdio> int main() { int a,b; while(scanf("%d %d",&a,&b) == 2) printf("%d\n",a+b); return 0; } I use g++ -o a a.cpp to complie it. Afterwards, I wrote python code as follows: import os,sys sys.stdin = open("./data.in","r") sys.stdout = open("./data.out","w") pid = os.fork() if pid == 0: cmd = ["./a","./a"] os.execv(cmd[0],cmd) However, the data.out file contains nothing. That is to say, the child process did not inherit stdin and stdout from his

Pointer losing its value + execv compilation warning

北城以北 提交于 2019-12-28 04:37:07
问题 I hope I haven't missed a similar question. I'm trying to code a mini-shell of my own, using primitive C functions. I got something that should work, but I have a pointer that makes everything bug. My adrCmd pointer should get the command-path string from the searchCmd() function and keep the same value in the main function. In fact: it points to the right value on searchCmd() , but not in the main() . Here's the code: int searchCmd(char* cmd, char* adrCmd){ char* path = getenv("PATH"); if

fork() kill() and execv()

空扰寡人 提交于 2019-12-24 09:27:04
问题 I'm trying a small unit-testing here. But the program doesn't work as I expected. 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){ perror("Error: running s with execvp has failed, Exiting\n"); } printf("IVE BEEN KILLED\n"); } //dad is here

redirecting output of execvp into a file in C

前提是你 提交于 2019-12-24 01:19:04
问题 I don't know what I am doing wrong... but here is the snippet of code that is being executed: if (fork() == 0) { // child int fd = open(fileName, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); dup2(fd, 1); // make stdout go to file execvp("ls","ls"); close(fd); exit(0); } if(wait(&status) == -1) { printf("ERROR REDIRECT\n"); } fileName gets created but there is nothing inside.What am I doing wrong? 回答1: My guess is that the execvp doesn't work but since you don't handler errors you don't see it. Try

Execv Linux printf doesn't work

随声附和 提交于 2019-12-20 06:00:28
问题 I'm trying to run executable using this c code: 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; } why printf("hello\n") doesn't work? even if i put sleep? 回答1: Your program should work when output is to a terminal, but it will not work correctly if output is redirected to a file or a pipe. When stdout is not connected to a terminal, its output is fully buffered. Calling an exec

How to use execv() without warnings?

一笑奈何 提交于 2019-12-18 17:50:23
问题 I am working on MacOS-X Lion with GCC 4.2. This code works, but I get a warning I would like fix: #include <unistd.h> main() { char *args[] = {"/bin/ls", "-r", "-t", "-l", (char *) 0 }; execv("/bin/ls", args); } warning: deprecated conversion from string constant to 'char*' I do not want the warning to be suppressed, I want not to have it at all. It is C++ code, not C. Using a char *const (so exactly the type required by execv()) still produces the warning. Thank you. 回答1: This seems to be ok

How to use execv() without warnings?

筅森魡賤 提交于 2019-12-18 17:50:04
问题 I am working on MacOS-X Lion with GCC 4.2. This code works, but I get a warning I would like fix: #include <unistd.h> main() { char *args[] = {"/bin/ls", "-r", "-t", "-l", (char *) 0 }; execv("/bin/ls", args); } warning: deprecated conversion from string constant to 'char*' I do not want the warning to be suppressed, I want not to have it at all. It is C++ code, not C. Using a char *const (so exactly the type required by execv()) still produces the warning. Thank you. 回答1: This seems to be ok

Does the C execv() function terminate the child proccess?

安稳与你 提交于 2019-12-18 09:34:04
问题 Heres a breakdown of my code. I have a program that forks a child (and registers the child's pid in a file) and then does its own thing. The child becomes any program the programmer has dignified with argv. When the child is finished executing, it sends a signal (using SIGUSR1) back to the parent processes so the parent knows to remove the child from the file. The parent should stop a second, acknowledge the deleted entry by updating its table, and continue where it left off. pid = fork();

How to make gdb follow execv? Not working despite “follow-exec-mode”

﹥>﹥吖頭↗ 提交于 2019-12-18 05:50:41
问题 i've written two simple programs: int main(int ac, char **argv ) { execv( "/home/me/Desktop/execvtest2", argv ); } and int main(int ac, char **argv ) { execv( "/home/me/Desktop/execvtest1", argv ); } I've compiled them with gcc -g to the according outputfiles. I'm running Ubuntu 10.10 using gcc (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5. When I'm debuging the first program with GNU gdb (GDB) 7.2-ubuntu, I can step until the first execv statement, but then the two files just keep running. Even if

prctl(PR_SET_PDEATHSIG, SIGNAL) is called on parent thread exit, not parent process exit

旧城冷巷雨未停 提交于 2019-12-14 04:18:17
问题 I have a process that is forking to a child process. The child process should not exist if the parent process exists. So, I call ::prctl(PR_SET_PDEATHSIG, SIGKILL) in the child process to kill it if the parent dies. What ends up happening is the parent thread calls pthread_exit, and that thread ends up being the catalyst that kills the child process. Here is my code: parent.cpp: #include <sys/prctl.h> #include <signal.h> #include <unistd.h> #include <pthread.h> #include <iostream> void* run