fork

printf anomaly after "fork()"

OS: Linux, Language: pure C I'm moving forward in learning C progpramming in general, and C programming under UNIX in a special case :D So, I detected a strange (as for me) behaviour of the printf() function after using a fork() call. Let's take a look at simple test program: #include <stdio.h> #include <system.h> int main() { int...

how can i sort one array with 2 child using fork() in c

example some array {2,8,9,10,21,32,1,6,3...} first child take (data size / 2) and sort second chile take (data size / 2) and sort after combine 2 child data and give us a sorted full data, is it possible with some algorithms? ...

What is the safe way to use fork with Apache::DBI under mod_perl2?

I have a problem when I use Apache::DBI in child processes. The problem is that Apache::DBI provides a single handle for all processes which use it, so I get DBD::mysql::db selectall_arrayref failed: Commands out of sync; you can't run this command now at /usr/local/www/apache22/data/test-fork.cgi line 20. Reconnection does...

Is it a bad idea to fork under mod_perl2?

Are there any counterindications to fork under mod_perl2? Should one use another way to run background process under mod_perl2? ...

How does fork() return for child process

I know that fork() returns differently for the child and parent processes, but I'm unable to find information on how this happens. How does the child process receive the return value 0 from fork? And what is the difference in regards to the call stack? As I understand it, for the parent it goes something like this: parent process--invok...

pthread and child process data sharing in C

hi everyone, my question is somewhat conceptual, how is parent process' data shared with child process created by a fork() call or with a thread created by pthread_create() for example, are global variables directly passed into child process and if so, does modification on that variable made by child process effect value of it in pare...

What's the proper way to fork() in FastCGI ?

I have an app running under Catalyst+FastCGI. And I want it to fork() to do some work in background. I used this code for plain CGI long ago (and it worked): defined(my $pid = fork) or die "Can't fork: $!"; if ($pid) { # produce some response exit 0; } die "Can't start a new session: $!" if setsid == -1; close STDIN o...

Redirecting exec output to a buffer or file

I'm writing a C program where I fork(), exec(), and wait(). I'd like to take the output of the program I exec'ed to write it to file or buffer. For example, if I exec ls I want to write file1 file2 etc to buffer/file. I don't think there is a way to read stdout, so does that mean I have to use a pipe? Is there a general procedure he...

Fork in Perl not working inside a while loop reading from file

Hi, I'm running a while loop reading each line in a file, and then fork processes with the data of the line to a child. After N lines I want to wait for the child processes to end and continue with the next N lines, etc. It looks something like this: while ($w=<INP>) { # ignore file header if ($w=~m/^\D/) { next;}...

What is the problem with this code? How to solve it? (fork)

What is the problem with this code? How to solve it? Parent processes goto in if or child process? first code produce zombie process or second code or both or non ? #include <signal.h> #include <sys/wait.h> main() { for (;;) { if (!fork()) { exit(0); } sleep(1); } } what about this code : #include <signal.h> #...

double fork using vfork

HI I am writing a part of a server which should dispatch some other child processes. Because I want to wait on some of the processes, and dispatch others without waiting for completion, I use double fork for the second kind of processes (thus avoiding the zombie processes). Problem is, my server holds a lot of memory, so forking takes a ...

How to kill all subprocesses of shell?

I'm writing bash script, which does several thing. In the beginning it starts several monitor scripts, each of them runs some other tools. At the end of my main script, I would like to kill all things that spawned from my shell. So, it might looks like this: #!/bin/bash some_monitor1.sh & some_monitor2.sh & some_monitor3.sh & do_so...

Help with output generated by this C code using fork()

I am trying to figure out the output for a block of C code using fork() and I am having some problems understanding why it comes out the way it does. I understand that when using fork() it starts another instance of the program in parallel and that the child instance will return 0. Could someone explain step by step the output to the blo...

How can I start a TCP server in the background during a Perl unit test?

I am trying to write a unit test for a client server application. To test the client, in my unit test, I want to first start my tcp server (which itself is another perl file). I tried to start the TCP server by forking: if (! fork()) { system ("$^X server.pl") == 0 or die "couldn't start server" } So when I call make test after pe...

C - fork() and sharing memory

I need my parent and child process to both be able to read and write the same variable (of type int) so it is "global" between the two processes. I'm assuming this would use some sort of cross-process communication and have one variable on one process being updated. I did a quick google and IPC and various techniques come up but I don'...

How do I determine whether ruby can fork without excessive use of regexp?

Is it possible to determine whether the implementation of ruby you're running on is capable of supporting fork, without running a regex against RUBY_PLATFORM that'll expand until it summons Cthulhu? (Related question: Ruby - How can I find out on which system my program is running?) Edit: I tried Marc-Andre's suggestion. It doesn't wor...

fork within Cocoa application

My problem is not the best scenario for fork(). However, this is the best func I can get. I am working on a Firefox plugin on Mac OSX. To make it robust, I need to create a new process to run my plugin. The problem is, when I forked a new process, much like this: if (fork() == 0) exit(other_main()); However, since the state is not cle...

c - fork() and wait()

Hi there, I need to use the fork() and wait() functions to complete an assignment. We are modelling non-deterministic behaviour and need the program to fork() if there is more than one possible transition. In order to try and work out how fork and wait work, I have just made a simple program. I think I understand now how the calls work...

can't read from stream until child exits?

OK I have a program that creates two pipes -> forks -> the child's stdin and stdout are redirected to one end of each pipe -> the parent is connected to the other ends of the pipes and tries to read the stream associated with the child's output and print it to the screen (and I will also make it write to the input of the child eventually...

Multi Forks controlled by main parent pipe

I have this code, it work fin but if i change NUM_CHILDREN = 2 or any other number "not equal 1" it bad file descriptor. why is that? #include <stdio.h> #include <unistd.h> enum { NUM_CHILDREN = 1 }; static int pipes[NUM_CHILDREN][2]; void start_encoding(void) { pid_t d, h; int pipe_master[2]; pipe(pipe_master); ...