fork

Run process with string input and output

扶醉桌前 提交于 2019-12-10 16:54:54
问题 There are plenty of questions on here related to fork() and exec(). I have not found one that really makes the process of using them simple though, and making programmer's lives simple is the goal. I need a C++, linux-friendly function that does the following: string RunCommand(string command, string input){} This function should be able to run a shell command, like grep, and "pipe" the content of input into it and read the ouptut and return it. So if I would do the following at the command

Is there a way to automatically close certain handles on a fork()?

懵懂的女人 提交于 2019-12-10 16:29:59
问题 Background: I've got a large existing process (it happens to be on AIX, so basically POSIX semantics) that's part of an even larger system. The existing processes are designed to run continuously. A new requirement for this process is to handle a new kind of complex input stream. In order to reduce risk, I've decided to fork/exec a child process to do the actual input processing, which isolates the existing main process from problems such as crashes or hangs on malformed input data. The child

understanding fork(), sleep() and processes flux

爱⌒轻易说出口 提交于 2019-12-10 16:09:29
问题 Been practicing with those system calls, but I stucked into this code: #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> main() { pid_t pid; switch(pid = fork()) { case -1: printf("fork failed"); break; case 0: //first child printf("\ni'm the first child, my pid is %d", getpid()); fflush(stdout); break; default: //parent sleep(5); /** sleep is generating problems **/ printf("\ni'm the parent process, my pid is %d", getpid()); printf("\ngenerating a new child"

exit from a child process (c)

陌路散爱 提交于 2019-12-10 15:58:03
问题 Consider this snippet of code: void do_child(void); int main(void){ int n; pid_t child; printf("Write a number: "); scanf("%d", &n); if(n != 1){ exit(1); } child = fork(); if(child >= 0){ /* fork ok */ if(child == 0){ printf("Child pid: %d\n", getpid()); do_child(); _exit(0); } else{ /* parent */ printf("Child parent: %d\n", getpid()); _exit(0); } } else{ /* fallito */ perror("No fork"); return 1; } return EXIT_SUCCESS; } void do_child(void){ /* some code here */ if(1 != 1){ /* what to write

Is there a way to prevent only a specific child from triggering a SIGCHLD?

北城以北 提交于 2019-12-10 15:35:39
问题 I'm writing a debugging utility, and I want to fork a child while preventing that child's termination from triggering a SIGCHLD to its parent. I still want other children to normally cause a SIGCHLD upon termination. I want to do this because I don't want the fork to trigger an existing $SIG{CHLD} handler, but I still want other children to trigger it. That is, I want to isolate my new child and I don't want it to interfere with management of existing children. I'm wary of locally installing

Why am I forking more than 5 times here?

时光怂恿深爱的人放手 提交于 2019-12-10 15:27:16
问题 So I have code here, and I expected it to strictly run ls -l 5 times, but it seems to run far more times. What am I doing wrong here? I want to run ls 5 times, so I fork 5 times. Perhaps I don't understand the concept of wait properly? I went over a ton of tutorials, and none seem to tackle multiple processes using fork thoroughly. #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() { pid_t pidChilds[5]; int i =0; for(i = 0; i<5;

How to fork a process of another module

☆樱花仙子☆ 提交于 2019-12-10 15:19:53
问题 TL;DR : How does one fork a process that is located outside of the current running process? I'm trying to use child_process of Nodejs in order to start another nodejs process on the parent's process exit. I successfully executed the process with the exec but I need the child process be independent of the parent, so the parent can exit without waiting for the child, hence I tried using spawn with the detached: true, stdio: 'ignore' option and unref() ing the process: setting options.detached

In C, are file descriptors that the child closes also closed in the parent?

≡放荡痞女 提交于 2019-12-10 15:02:38
问题 As far I understand it, Fds are integers that are used to look up the open files in the kernel's file description table. So therefore if you have a code segment like this: int fd[2], temp1, temp2; pipe(fd); temp1 = fd[0]; temp2 = fd[1]; close(temp1); close(temp2); All the file descriptors to the pipe are closed and thus the pipe would no longer exist. Since FDs are simply ints, saying close(temp1) is identical to saying close(fd[0]) . In light of all this (and please let me know if I am

How to Spawn Child Processes that Don't Die with Parent?

落花浮王杯 提交于 2019-12-10 14:14:59
问题 I have a C++ program that acts as a watchdog over others. If it detects that a process is no longer running, it restarts it via system . The problem is, if I kill the watchdog process, any processes it has started up also die. void* ProcessWatchdog::worker(void* arg) { //Check if process is running if( !processRunning ) system("processName /path/to/processConfig.xml &"); } The new child process gets started correctly, and runs without any problems. But when the parent (now this

using fork() to make 3 children out of 1 parent in C (not C++)

ⅰ亾dé卋堺 提交于 2019-12-10 14:12:05
问题 Hi there I've been working on a program that forks children and later will fork more children from each child but that's not what I need help with. When I run my program (in here it is a function but works the same) I am supposed to have one parent(PPID) spawn 3 children (PIDS= 1,2,3) but what I get is either the same PID and PPID 3 times (my current code) or before I was getting 3 parents with each parent having one child and the PPIDS were different as well as the PIDS, but the PPIDs were