execv

Send messages from child process to parent

帅比萌擦擦* 提交于 2019-12-14 03:11:04
问题 I am executing the parent code. I then do a fork and then execvpe. The new program that i "execvpe" throws a lot of console messages and i want to hide those. Is it possible for me to redirect all my stdout and stderr messages in the child process onto a file? I tried a close(1) so that i dont dump messages on console (stdout) and that didnt help 回答1: pid_t pid = fork(); /* Child process */ if (pid == 0) { /* Open log file */ int log = creat("logfile", 0644); /* Redirect stdout to log file */

Java Program terminates after JNI method call

对着背影说爱祢 提交于 2019-12-12 04:49:12
问题 I'm using JNI to call native C methods, but my java program terminates (Exit code 0) after the first method call and doesn't reach the rest of the code. Here is my source: Exec.java: package libs; public class Exec { static { System.load(System.getProperty("user.dir")+"/bin/"+"libexec.so"); } public static native int execv(String pExecPath, String[] pArgs); } Exec.c: #include <jni.h> #include <unistd.h> #include <stdio.h> #include <errno.h> JNIEXPORT jint JNICALL Java_libs_Exec_execv(JNIEnv *

How execv gets the output from pipe?

懵懂的女人 提交于 2019-12-11 11:34:54
问题 referring to the old homework question : /* implementing "/usr/bin/ps -ef | /usr/bin/more" */ using pipes. #include <stdio.h> #include <unistd.h> #include <stdlib.h> 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 */ execv(argv[0], argv);// here how execv reads from stdin ?? exit(0); } if (fork() =

Using execv (C language) to run commands from a linux command prompt

谁说我不能喝 提交于 2019-12-11 09:37:29
问题 The only part I am confused on thus far is how to set up execv with the first parameter as the current working directory. I've tried both "." and "~", neither are executing anything to the screen; same for "/." and "/~". I'm confused on how to have execv run something like this: $ ./prog ls -t -al And have it execute the commands after the program execution (which are stored into argv) in the current directory, or the same directory as the file is in (which will vary based on who is using it.

Semaphore blocking children

霸气de小男生 提交于 2019-12-11 05:51:51
问题 I have a problem, I would like to do a fork for example a fork of 20processes, this fork created, should not do anything until the last one is not created, and I want to do it with semaphore, how can I implement it? for (i=0; i<20; i++) { switch (fork()) { case: -1: exit(EXIT_FAILURE); case 0: execve(....); exit(EXIT_FAILURE); default: printf ("Child Created!"); } } 回答1: Here's you homework. You can pay me later. #include <semaphore.h> #include <sys/mman.h> #include <stdio.h> #include <stdlib

If I fork() and then do an execv(), who owns the console?

泪湿孤枕 提交于 2019-12-06 12:19:19
问题 I am writing a Linux application. What happens if I call fork() and then run an application that takes console input? Consider the code below: int process_id = fork(); if (process_id != 0) { /* this is the parent process */ error = execv("../my_other_app", "parameter1", NULL); if (error < 0) { printf("error!"); } } else { /* this is the child process. Wait for my_other_app to set up */ sleep(3); /* now continue */ } printf("########## press ENTER to stop ##########\n"); getchar(); exit(0);

If I fork() and then do an execv(), who owns the console?

爱⌒轻易说出口 提交于 2019-12-04 18:20:30
I am writing a Linux application. What happens if I call fork() and then run an application that takes console input? Consider the code below: int process_id = fork(); if (process_id != 0) { /* this is the parent process */ error = execv("../my_other_app", "parameter1", NULL); if (error < 0) { printf("error!"); } } else { /* this is the child process. Wait for my_other_app to set up */ sleep(3); /* now continue */ } printf("########## press ENTER to stop ##########\n"); getchar(); exit(0); The thing is, my_other_app also has a press ENTER to stop message. So when I do the getchar() call, which

How can I pass the redirection operator '>' as an argument for execv?

旧城冷巷雨未停 提交于 2019-12-02 18:19:10
问题 In the linux terminal, I can type echo hello! > /path/to/file I thought I would be able to do the same thing using execv: #include <stdio.h> #include <unistd.h> #include <fcntl.h> 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; } However, this code doesn't write 'hello!' to the file, which is what I want it to do. Is there another way to do this using execv and echo? Edit: I've

How can I pass the redirection operator '>' as an argument for execv?

元气小坏坏 提交于 2019-12-02 11:22:13
In the linux terminal, I can type echo hello! > /path/to/file I thought I would be able to do the same thing using execv: #include <stdio.h> #include <unistd.h> #include <fcntl.h> 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; } However, this code doesn't write 'hello!' to the file, which is what I want it to do. Is there another way to do this using execv and echo? Edit: I've tried using dup2 as a solution as well: #include #include #include int main(void){ char *write_cmd[] =

Execv Linux printf doesn't work

喜欢而已 提交于 2019-12-02 08:18:49
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? Barmar 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 function does not flush the buffer before replacing the current process with the new program, so any