Creating Multiple Processes


 
Thread Tools Search this Thread
Top Forums Programming Creating Multiple Processes
# 1  
Old 02-04-2010
Creating Multiple Processes

I am having problems creating multiple forks. I want create a certain number of forks, each call a program and each wait for a different value. How is this accomplished my loop is not doing the trick.

Code:
for (i = 0; i < 5; i++) {

        if (fork() < 0) {
            //print error
        }

        else if (fork() == 0) {
            //run a program (exec(...)
        }

        else {
            p_id = wait(&result);
        }
    }

I know how to use the wait() and exec() call but can't figure out how to do this for multiple forks I need each fork to return a differnt value.

Any help much appreciated.
# 2  
Old 02-04-2010
fork() returns the PID of the child process to the parent, just store its value. Or if you need the value in the child, get it with getpid().

Is the fork at the top of the loop just to run two processes? If so, keep in mind you'll end up with two waiters, too! I usually encapsulate each fork in code like:

Code:
pid_t pid=fork();
if(pid < 0)
{
  // error
}
else if(pid == 0)
{
  // child code
  exit(0); // to prevent it executing code below.  after all what if exec fails?
}
else if(pid > 0)
{
  // parent code.
}

# 3  
Old 02-04-2010
My goal is to simply use system calls to add a group of numbers. I retrieve the result through wait(&value) aka the suicide note. So if I had 8 numbers, I first need to create 4 forks, call on a program which returns the sum. I am having problems with the loop the manages this task.
# 4  
Old 02-05-2010
You can only communicate numbers 0 to 255 through a return value, you should know.

Code:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
{
  int total=0;
  int n;

  for(n=0; n<4; n++)
  {
      pid_t pid=fork();
      if(pid == 0)  // This code is executed in the child
        return(42); // the child returns from main, stopping execution

    // The parent continues looping.
  }

  for(n=0; n<4; n++)
  {
    int status;
    wait(&status);
    total += WEXITSTATUS(status);
  }

  printf("The total is %d\n", total);
  return(0);
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SPAWN Multiple Processes in Unix

Hi, I have three files in my IN directory.Each file should be copied 25 times using for loop.Each file processing should run in parallel?How to spawn multiple processes in unix?Any help would be appreciated. Thanks, Liyakath (7 Replies)
Discussion started by: liyakathali
7 Replies

2. Shell Programming and Scripting

need help ps -e on multiple processes

:)Hi there, I am new to scripting and wanted to see if someone can show me how to grep on multiple processes and send the output to a file in /home/mydir/output. I am aware of ps -ef | grep on 1 process but need help looking up multiple processes, can you use this command ps -elf | grep |pid1... (4 Replies)
Discussion started by: abbya
4 Replies

3. Programming

Creating more processes with fork()

Hello people I need help How to make ONE process to create MORE (not one) processes with fork(). I tried several codes but do not work. Thanks (8 Replies)
Discussion started by: nekoj
8 Replies

4. Solaris

Multiple hanged FTP processes

Hello people, I got one problem with a script. I have a script which runs every five mins and in the script an ftp process is invoked which sends files to a particular location. The problem is that the ftp process hangs every now and then which causes the whole script to hang. As the... (4 Replies)
Discussion started by: m_usmanayub
4 Replies

5. Shell Programming and Scripting

kill multiple processes by name

Want to kill multiple processes by name. for the example below, I want to kill all 'proxy-stagerd_copy' processes. I tried this but didn't work: >> ps -ef|grep proxy_copy root 991 986 0 14:45:34 ? 0:04 proxy-stagerd root 1003 991 0 14:45:49 ? 0:01... (2 Replies)
Discussion started by: catalinawinemxr
2 Replies

6. Shell Programming and Scripting

Creating a pipe using parent and child processes

Hello, I am trying to create a pipe that will direct stdout to in side of the pipe, and stdin to the out side of the pipe - I created two child processes to handle this. However, my pipe doesn't seem to be working correctly. Did I use execv() correctly? Command1 and command2 represent the two... (3 Replies)
Discussion started by: jre247
3 Replies

7. UNIX for Dummies Questions & Answers

Running multiple processes in Linux

Hi guys, I want to run the multiple scripts at the same time using a ksh script. For example, I have three scripts to run: a.ksh, b.ksh and c.ksh How to start the above 3 scripts simultaneously and then on the completion of the above scripts I have other tasks to schedule. Thanks Gary (6 Replies)
Discussion started by: abcabc1103
6 Replies

8. Shell Programming and Scripting

multiple processes overlap

Hello I've got a script that creates multiple processes, in ksh, to bcp out 6 tables at a time. In the script, we write messages to the log to show our progress; most of the time, the log messages are nice and neat with one per line, like they should be. But every once in awhile, at random, the... (2 Replies)
Discussion started by: stonemonolith
2 Replies

9. Shell Programming and Scripting

Doubt about multiple processes

Suppose that I am performing some operation on an sql database. Lets say process of Searching and then if a value is found, updating it... Now, when I have some millions of records on which the operation has to be performed... Does it help to spawn multiple processes each executing the same... (9 Replies)
Discussion started by: Legend986
9 Replies

10. Shell Programming and Scripting

Shell script creating too many processes.

I have a shell script that I am running every 60 seconds, but it is creating this process to the point that it is causing the server to perfrom poorly. Below is my script, what can I change to prevent this? while true do java -classpath .....( all my classes here) >/dev/null 2>&1 ... (3 Replies)
Discussion started by: Miller_K
3 Replies
Login or Register to Ask a Question