fork

How to wait for each process to terminate in the following example?

大兔子大兔子 提交于 2019-12-11 06:46:57
问题 The program terminates after taking 4 or 5 values from scanf . But I want it to accept 8 values (totaling 8 processes) and then terminate. void main() { fork(); fork(); fork(); scanf("%d",&j); printf("The values are %d\n",j); wait(0); } 回答1: Well, the first question you have to answer to yourself is how many processes you think you have there? As you don't use the returned value from the fork(2) system call, you don't know even if you are the parent or you are the child after execution of

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

UNIX: Waiting on process children upon exit?

一世执手 提交于 2019-12-11 04:55:32
问题 Let's say I have a C program which spawns some child processes using fork() and exec() . The parent keeps a list of the pid s of its children. Once in a while, it tries wait ing on them using WNOHANG and informs the user if they have terminated. The program then decides to exit. Must I explicitly kill and then wait on the remaining child processes so that they don't become zombies? According to Wikipedia: "Zombie processes should not be confused with orphan processes: an orphan process is a

Two processes reading the same stdin

佐手、 提交于 2019-12-11 04:47:26
问题 In a C program, I have a menu, with some options to choose from, represented by characters. There's an option that forks the process, and runs a function (we can say it runs in the background). This function that runs in the background, under some conditions, can ask the user to enter data. My problem is: when the main process is asking for data (or option) and the child process is asking for data too, I can't send the data properly. Do you have any idea on how to deal with this? I'll add a

fork + PHP CodeIgniter: 'MySQL server has gone away' - How not to lose connection when forking with CI?

社会主义新天地 提交于 2019-12-11 04:44:32
问题 I installed fork on my Ubuntu Server (using PHP-Apache-Codeigniter). I have this code: <?php public function add_keyword() { $keyword_p = $this->input->post('key_word'); $prod = $this->input->post('prod_name'); $prod = $this->kas_model->search_prod_name($prod); $prod = $prod[0]->prod_id; $country = $this->input->post('key_country'); $keyword = explode(", ", $keyword_p); var_dump($keyword); $keyword_count = count($keyword); echo "the keyword count: $keyword_count"; // Create fork $pid = pcntl

How do I merge locally a master and a fork in git?

末鹿安然 提交于 2019-12-11 04:39:50
问题 I need to use Active_admin with Formtastic 2 and the main branch doesn't support it yet. A couple of weeks ago someone made a fork to support Formtastic 2 But then other additions were added to the master branch and were not commited to the fork. And now the fork is outdated with other things, but yet it support Formtastic. How can I merge both of them locally in my computer using git? 回答1: This simplest way is to switch to your local formtastic branch, then run git merge master to merge the

clone system call OS X not linking - undefined symbols [duplicate]

妖精的绣舞 提交于 2019-12-11 04:36:43
问题 This question already has answers here : Where is clone() method in sched.h on Mac OS X (2 answers) Closed 5 years ago . I would like to use the clone system call on OS X. It's a Unix system call so it shouldn't be a problem, right? I have successfully tried using fork , vfork and other similar functions. Here is the program I'm trying: #include <sched.h> //Clone resides here #include <stdlib.h> //standard library #include <stdio.h> #include <time.h> #include <limits.h> #include <sys/shm.h>

php - WebSocket, pcntl_fork, close child process correctly

天大地大妈咪最大 提交于 2019-12-11 03:52:32
问题 I install ratchet php websocket. I do pcntl_fork in onMessage method to work with every client in new thread. public function onMessage(ConnectionInterface $from, $msg) { $pid = pcntl_fork(); if ($pid == -1) { $this->myPrint("cant create fork"); } else if ($pid) { } else { // ..... $result $from->send($result); // $pid = getmypid(); // exit($pid); } } if i try to close child process, client dont get any message. ($from->send($result) dont work). if i comment exit($pid) . Client get message.

Why does Perl's IO::Socket on Windows complain about “Resource Not Available” after 64 connections?

醉酒当歌 提交于 2019-12-11 03:42:42
问题 I created a server with Perl under Windows (ActivePerl 5.10.1 build 1006) that forks upon being connected to, accepts some JSON data, and writes it to a database. I am running into a problem after 64 clients connect to the server, the error message being "Resource is not available" when trying to fork. Running this code under Linux I found many defunct child process, which was solved by adding a wait() call on the parent. This however did not solve the problem. Running the code under Linux

Why wait() returns -1 error code?

旧街凉风 提交于 2019-12-11 03:09:49
问题 I have the following code: void fork1() { pid_t id, cpid; int status; id = fork(); if (id > 0) { // parent code cpid = wait(&status); printf("I received from my child %d this information %d\n", cpid, status); } else if (id == 0) { // child code sleep(2); exit(46); } else exit(EXIT_FAILURE); exit(EXIT_SUCCESS); } The output is: I received from my child -1 this information 0 So, why I receive the -1 error code after wait ? I was expecting to receive the value 46 as status. EDIT: I added the