fork

Number of possible combinations of fork

怎甘沉沦 提交于 2019-12-08 08:08:05
问题 int main(void) { int id = 0; for(int i = 1; i < 4; i++) { if(fork() == 0) { id = i; } else { printf("Process %d created child %d\n", id, i); } } return 0; } In the code above, multiple ordering of the output (printf statements) can be generated based on how the operating system schedules processes for execution. How many different orderings are possible? You may assume that all fork and printf calls succeed. I'm trying to help my students understand how to approach this problem, however I got

Which one to choose waitpid/wait/waitid?

假如想象 提交于 2019-12-08 07:32:56
问题 I want to use execl in child process after doing fork. execl will execute the script which will take around 120 seconds of time. I tried almost all the combination with waitpid, wait and waitid with different argument (0, WNOHANG, etc.,) but in all case I am getting -1 return value. So I want to know in which wait function I need to use when? So I can concentrate in one wait function to make it work. One more interesting thing which I observed from my logs is that when I am doing nothing in

Segments duplicated during fork()?

Deadly 提交于 2019-12-08 06:44:56
问题 I have studied that during a fork, the data and code segment of the parent process gets duplicated into the child process. Kindly see the program below. int main() { int a = 5; pid_t pid; pid = fork(); if(pid == 0) { printf("In child a = %d",a); } else { printf("In parent a = %d",a); } return 0; } Here a is in the stack segment of parent process as it is declared inside the function, main() . The child process should only get copy of the code and data segment of the parent process and not the

How fork() work by copying parent process area structs? (Linux)

谁说胖子不能爱 提交于 2019-12-08 05:13:05
问题 I'm a beginner in Linux and Virtual Memory, still struggling to understand how fork() works: My textbook says that when the fork function is called by the current process, the kernel creates exact copies of the current process’s mm_struct, area structs, and page tables for the child process. I have questions about the page tables that being copied from parent process to child prcess. 1.If there is only one-level of page table used by parent process, my understanding is, when fork() is called,

fork with Ruby 1.8 and Windows

最后都变了- 提交于 2019-12-08 05:06:49
问题 I'm using ruby 1.8.7 patchlevel 302 and I'm working on a Windows xp system. I have to start an external process that needs to react on user input. The process doesn't react if I use threads, so I tried using fork. With fork the external process reacts on the user input but it executes more than just the fork block. For example fork do puts 'child' end puts 'parent' Process.wait puts 'done' produces the following output on my machine: parent child parent done done As you can see 'done' and

How can I kill forked processes that take too long in my perl script without timing out the forked process?

点点圈 提交于 2019-12-08 04:57:09
问题 I've been using the following template for all of my forking/processes needs when it comes to processing "things" in parallel. It basically loops through everything I need to process, X number of entries at a time, and time's out any entries that take too long: my $num_procs = 0; foreach my $entry (@entries) { $num_procs++; if($num_procs == $MAX_PROCS) { wait(); $num_procs--; } my $pid = fork(); if($pid == 0) { process($entry); } } for (; $num_procs>0; $num_procs--) { wait(); } The "process"

fork() and printf()

百般思念 提交于 2019-12-08 04:37:45
问题 As I understood fork() creates a child process by copying the image of the parent process. My question is about how do child and parent processes share the stdout stream? Can printf() function of one process be interrupted by other or not? Which may cause the mixed output. Or is the printf() function output atomic? For example: The first case: parent: printf("Hello"); child: printf("World\n"); Console has: HeWollorld The second case: parent: printf("Hello"); child: printf("World\n"); Console

Linux: fork & execv, wait for child process hangs

主宰稳场 提交于 2019-12-08 03:23:58
问题 I wrote a helper function to start a process using fork() and execv() inspired by this answer. It is used to start e.g. mysqldump to make a database backup. The code works totally fine in a couple of different locations with different programs. Now I hit one constellation where it fails: It is a call to systemctl to stop a unit. Running systemctl works, the unit is stopped. But in the intermediate process, when wait()ing for the child process, wait() hangs until the timeout process ends. If I

Nginx面试题

感情迁移 提交于 2019-12-08 03:03:12
一、什么是Nginx? Nginx是一个高性能的HTTP和反向代理服务器,及电子邮件代理服务器,同时也可以作为反向代理服务器来实现负载均衡。 二、为什么要使用Nginx? 因为Nginx具有跨平台、配置简单、非阻塞、高并发连接等特点! Nginx的优势: 内存消耗小:开启10个Nginx才占150M内存,Nginx处理静态文件好,耗费内存小; 内置的健康检查功能:如果有一个服务器宕机,Nginx会将这台宕机的服务器移出集群; 节省带宽:支持Gzip压缩,可以添加到浏览器本地进行缓存; 稳定性高:宕机的概率非常小; 接收用户请求是异步的(采用epoll模型):浏览器将请求发送到nginx服务器,它先将用户请求全部接收下来,再一次性发送给后端web服务器,极大减轻了web服务器的压力,一边接收web服务器的返回数据,一边发送给浏览器客户端, 网络依赖性比较低,只要ping通就可以负载均衡,可以有多台nginx服务器 使用dns做负载均衡,事件驱动; 三、Nginx的功能? 主要功能: 可以作为Web服务器(代替Apache,对PHP需要fastcgi处理器支持); 可以作为反向代理服务器; 可以实现负载均衡; 可以配置虚拟主机; fastcgi:Nginx本身不支持PHP等语言,但是它可以通过fastcgi来将请求转交到某些语言或框架处理; 四、Nginx是如何实现高并发的?

Perl forked socket server, stops accepting connections when a client disconnects

孤者浪人 提交于 2019-12-08 02:52:41
问题 When using the following, but also when using similar code with IO::Socket::INET, I have problems with accepting new connections, once a client has disconnected. It seems the parent stops forking new children, until all previous children have ended/disconnected. The connection is accepted though. Does anyone have an idea what I'm doing wrong. #!/usr/bin/perl -w use Socket; use POSIX qw(:sys_wait_h); sub REAPER { 1 until (-1 == waitpid(-1, WNOHANG)); $SIG{CHLD} = \&REAPER; } $SIG{CHLD} = \