fork

C++ pipe and fork

旧巷老猫 提交于 2021-01-29 03:03:00
问题 I'm working on a sample program to learn how pipes and forking works. In my very basic implementation, in my child process, i closed 0 and duplicated the read end of the pipe so that file descriptor 0 is now the read end of my pipe. From my parent process, I write out a string, and in my child process, I read the string using cin as cin essentially is my read end of the pipe and what I observe is the complete string does not print out and I can't seem to understand why! #include <iostream>

Temporary disabling child processes from parent terminal at runtime

徘徊边缘 提交于 2021-01-28 22:13:16
问题 Brief: there is huge Linux application with many child processes, and I need something like that: /* parent process, far from fork*/ suppress_child_output_to_parent_tty(); printf("important message"); /* huge piece of code */ printf("important message"); restore_child_output_to_parent_tty(); I know that exist a standard way to do this by make pipe, fork, redirect STDOUT to that pipe, select&read pipe /write to parent stdout at the some loop at different thread, manually pause this loop when

How to fork a Linux process in a Stopped state?

拈花ヽ惹草 提交于 2021-01-28 14:41:37
问题 I am starting a new task using a clone(2) call. There used to be CLONE_STOPPED flag, but it is no longer present in current kernel. Is there any trick to start a task in a Stopped state (waiting for SIGCONT to actually run)? 回答1: You can't, there's no way to do that in recent kernels, not unless you write a kernel module to do that. You can see how kernel v2.6.32 used to do it in kernel/fork.c (L1449): if (unlikely(clone_flags & CLONE_STOPPED)) { /* * We'll start up with an immediate SIGSTOP.

Fork How to Kill A process with PID

大兔子大兔子 提交于 2021-01-28 12:11:02
问题 Hi all I have to run a binary file using c++ and kill it. My code look like static int PROCESS_PID=0; void startService(bool startservice){ if(startservice==true){ pid_t PID = fork(); if(PID == 0) { PROCESS_PID = getpid(); printf("the child's pid is: %d\n", PROCESS_PID); system("./process"); } } else{ kill(PROCESS_PID, SIGUSR1); //kill process inside child process } } But when I kill the process the entire program get exited. Any Idea ? Is there anything wrong with my code ? Thanks.... 回答1:

Is there a reason for the child to not to print until the parent process finishes printing?

大兔子大兔子 提交于 2021-01-28 10:48:00
问题 #include <stdio.h> #include <unistd.h> int do_fork(int depth) { if (!depth) { return fork(); } int val = do_fork(depth - 1); printf("%d\n", val); return val; } int main() { int val; scanf("%d", &val); do_fork(val); } I tried this code several times, with several different values on several different machines. It doesn't matter how big the number is, I always see the pid of the child printed one next to the other with no zeros in between. Only when the pids are all printed, the child starts

Bi-directional inter-process communication using two pipes

为君一笑 提交于 2021-01-28 08:44:22
问题 I am trying to write code that forks a subprocess and communicates with it using pipes. I am using two pipes - one for writing to, and the other for reading from the standard streams of the subprocess. Here's what I have so far: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <string.h> void read_move(int fd) { FILE *stream = fdopen(fd, "r"); char c; setvbuf(stream, NULL, _IONBF, BUFSIZ); while ((c = fgetc(stream)) != EOF) { putchar(c); } fclose(stream)

Global variable value in case of fork() [duplicate]

人走茶凉 提交于 2021-01-28 07:08:38
问题 This question already has answers here : After forking, are global variables shared? (4 answers) Closed 4 years ago . Lately I've encountered an interesting situation: I've defined a global static variable in a dynamically linked library (.so). This library always being called under fork(). What I've noticed is that the global variable always being called with init values, and doesn't change them between the calls. I have a few questions about that: Why does being 'forked' change the basic

Zombie process and fork

天大地大妈咪最大 提交于 2021-01-28 04:47:45
问题 i have a code like this... c = fork(); if(c==0) { close(fd[READ]); if (dup2(fd[WRITE],STDOUT_FILENO) != -1) execlp("ssh", "ssh", host, "ls" , NULL); _exit(1); } close(fd[WRITE]); fd[READ] and fd[WRITE] are pipe file descriptors. when i run it continuously, there are a lot of zombie processes when i use ps ax. How to rectify this? Is this because i am not using the parent to wait for the exit status of the child process... 回答1: If you have no intention to wait for your child processes, set the

Multiple fork calls cause BlockingIOError

断了今生、忘了曾经 提交于 2021-01-28 02:25:15
问题 I'm searching for an explanation about the error I get with the following snippet : #!/usr/bin/env python3 import os, sys if __name__ == '__main__': while True: pid = os.fork() if pid == 0: sys.exit() elif pid > 0: pass # os.waitpid(pid, 0) else: sys.exit() This will spawn many processes (processes that exit as they are spawned). This will eventually cause a BlockingIOError showing like this : Traceback (most recent call last): File "./asd.py", line 7, in <module> pid = os.fork()

Multiple fork calls cause BlockingIOError

余生颓废 提交于 2021-01-27 20:29:00
问题 I'm searching for an explanation about the error I get with the following snippet : #!/usr/bin/env python3 import os, sys if __name__ == '__main__': while True: pid = os.fork() if pid == 0: sys.exit() elif pid > 0: pass # os.waitpid(pid, 0) else: sys.exit() This will spawn many processes (processes that exit as they are spawned). This will eventually cause a BlockingIOError showing like this : Traceback (most recent call last): File "./asd.py", line 7, in <module> pid = os.fork()