fork

Are all reentrant functions safe to use after fork()ing in a multithreaded(with pthreads) process?

£可爱£侵袭症+ 提交于 2019-12-23 03:33:24
问题 I'm working on a C++ project which employs pthreads for multithreading, but also needs to fork now and then. I've read warnings scattered in the code that memory allocation should never be done after fork , but I didn't know exactly why before I found this article. I've summarized the points: When the main thread fork s, the other threads may be in the middle of some library function call; fork ing doesn't copy the other threads, making the calls stuck forever; malloc uses global variable,

Share variables between Child processes in perl without IPC::Shareable

浪尽此生 提交于 2019-12-23 03:09:15
问题 I need to share variables between different Children processes and my Parent process in Perl without the use of IPC::Shareable. I basically just need to have a global variable that all processes would be able to read/write to. Also, the variable only needs write access from the parent if that would make my answer simpler. The Children only need to read it. Edit: My problem could also be solved if there is a way for me to pass a message from one child process to another 回答1: From the

fork() and exec() Two Child Processes

半腔热情 提交于 2019-12-23 02:52:34
问题 I am calling fork() twice to create two child processes. I want child process A to do an exec() call and child process B to also do an exec() call. The problem I am having with the given code is that after the first exec() from child process A, the next fork() does not seem to occur and the program exits. I think that it has to do with how exec() overlays the parent process. What I want to accomplish is to call exec() from each of the child processes created by fork(). #include <sys/types.h>

Output from fork() in c

我们两清 提交于 2019-12-23 01:02:07
问题 I recently ran into this piece of code, and don't fully understand it. What circumstances would cause pid == 0? Why does wait(NULL) cause the program to go into the if(pid == 0) Basically I don't fully understand the output below. Any help would be appreciated. Thank you. Code: #include <stdio.h> #include <stdlib.h> #include <unistd.h> // standard POSIX header file #include <sys/wait.h> // POSIX header file for 'wait' function int main(void) { int i = -1; int pid; pid = getpid(); fprintf

Checking fork behaviour in python multiprocessing on Linux systems

余生长醉 提交于 2019-12-23 00:47:59
问题 I have to access a set of large and not pickable python objects from many processes. Therefore, I would like to ensure that these objects are not copied completely. According to comments in this and this post, objects are not copied (on unix systems) unless they are changed. However, referencing an object will change its reference count, which in turn will then be copied. Is this correct so far? Since my concern is due to the size of my large objects, I do not have a problem, if small parts

18 File Duplication and Pipes

别说谁变了你拦得住时间么 提交于 2019-12-22 23:58:58
1 Resource Duplication Across Forks 子进程是父进程的复制,在 fork 瞬间复制所有信息 复制父进程的代码 复制父进程的状态,包含全部的内存空间,比如变量 /*shared_variable.c*/ int main ( int argc , char * argv [ ] ) { int status ; pid_t cpid , pid ; int i = 0 ; while ( 1 ) { //loop and fork children cpid = fork ( ) ; if ( cpid == 0 ) { /* CHILD */ pid = getpid ( ) ; printf ( "Child: %d: i:%d\n" , pid , i ) ; //set i in child to something differnt i * = 3 ; printf ( "Child: %d: i:%d\n" , pid , i ) ; _exit ( 0 ) ; //NO FORK BOMB!!! } else if ( cpid > 0 ) { /* PARENT */ //wait for child wait ( & status ) ; //print i after waiting printf ( "Parent: i:%d

Linux内核分析— —创建新进程的过程

只谈情不闲聊 提交于 2019-12-22 17:56:49
分析Linux内核创建一个新进程的过程 实验过程 要求:使用gdb跟踪分析一个fork系统调用内核处理函数sys_clone ,验证对Linux系统创建一个新进程的理解,推荐在实验楼Linux虚拟机环境下完成实验。 cd LinuxKernel qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img -s -S 课程内容 阅读理解task_struct数据结构: 进程控制块PCB——task_struct 1、操作系统的三大管理功能包括:   (1)进程管理   (2)内存管理   (3)文件系统 2、PCB task_struct中包含:   (1)进程状态   (2)进程打开的文件   (3)进程优先级信息 3、通过唯一的进程标识PID来区别每个进程。 4、进程状态转化 (1)创建新进程后实际的状态是TASK_RUNNING,就绪但是没有运行,调度器选择一个task之后进入运行态,也叫TASK_RUNNING。 (2)当进程是TASK_RUNNING时,代表这个进程是可运行的,至于它有没有真的在运行,取决于它有没有获得cpu的控制权,即有没有在cpu上实际的运行。 (3)一个正在进行的进程调用do_exit(),进入TASK_ZOMBIE,进程被终止,“僵尸进程”。 (4)等待特定时间或者资源的时候

Causes for ENOMEM from ::popen()

僤鯓⒐⒋嵵緔 提交于 2019-12-22 17:40:07
问题 I have an application that mostly works, but am having one condition wherein the call to ::popen() gets an error with errno set to ENOMEM. The man page for ::popen() refers you to page for ::fork() which itself lists ENOMEM with this brief comment on Linux: The fork() function may fail if: ENOMEM Insufficient storage space is available. I am wondering if I am really running out of memory, or perhaps some other resource like file descriptors? Can fork() give ENOMEM for something other than

Why is there timing problem while to fork child processes

久未见 提交于 2019-12-22 16:48:14
问题 When I took a look at the reference of 'Launching-Jobs' in gnu.org, I didn't get this part. The shell should also call setpgid to put each of its child processes into the new process group. This is because there is a potential timing problem : each child process must be put in the process group before it begins executing a new program , and the shell depends on having all the child processes in the group before it continues executing. If both the child processes and the shell call setpgid,

C++, linux, fork, execvp, waitpid and SIGTSP

元气小坏坏 提交于 2019-12-22 14:00:54
问题 I'm implementing a Terminal for a Home Work. I almost finished, I just need to implement a bg ( Background ) and a fg ( Foreground ) commands. my code looks like this: void run(){ string command[] = parseMyInput( getInput() ); int fork_result = fork(); if( -1 == fork_result ) //handle error else if( 0 == fork_result ){ // child setpgrp(); // I don't want the children to get the signals if( -1 == execvp( command[0], makeArgs(command) ) ) //handle error } else { // parent if( command[ length -