multiple fork() question


 
Thread Tools Search this Thread
Top Forums Programming multiple fork() question
# 1  
Old 02-01-2011
multiple fork() question

I writing a program that forks three times but only on the parent process. The three children processes then produces output in order. 1, 2, 3. I am confused on how to do this. I have tried multiple if and else if statements but the output does not come out right. How should I go about doing this?
# 2  
Old 02-02-2011
When forking, a new process is created, and from that point on the kernels process scheduler decides in which order to run the processes. With enough (allocated) CPUs all may run at the same time. With lesser CPUs it's probably randomized, depending on system load.

Depending on what your application does there are different ways to "order" your output:
  • If it's mostly calculation take a look at OpenMP.
  • If you're (for example) communicating over a network, have the children, except for the first one, save the output until they receive a signal from the parent thread (using wait, signal, and kill)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

One parent, multiple children pipe with fork()

The task I have to do is something along the lines "I receive some input and based on the first character I send it through pipe to one of the children to print". The scheme it is based on is 1->2; 1->3; 1->4; 2 will print all the input that starts with a letter, 3 will print all the input that... (2 Replies)
Discussion started by: Ildiko
2 Replies

2. Programming

Linux fork, execv, wait question

Hi All, I have a program for class that needs to do the following: 1. Print the directory entries from the current directory using ncurses 2. Provide a prompt next to each directory entry and allow the user to enter commands that may or may not be about the file 3. Execute those commands in... (1 Reply)
Discussion started by: afulldevnull
1 Replies

3. Programming

question about fork

i'm experimenting fork function and i found this code #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <wait.h> #include <fcntl.h> #include <unistd.h> int main(void) { int fd; pid_t p; p = fork(); fork(); if (p>0) { fork();} fork(); fork();... (6 Replies)
Discussion started by: blob84
6 Replies

4. Programming

Beginners question about fork

Hi everyone: I'm developing a dynamic library for notifications, this library is used for a daemon that i've programmed, when something goes wrong the library should send an email to an administrator, but since sending an email is a non-vital process then it can fail (it should work as an... (4 Replies)
Discussion started by: edgarvm
4 Replies

5. Programming

Need help with fork, forking multiple childs and shared memory

Hi all, I m writing an application, where i need to fork multiple childs and those child should handle particular task given to them. More descriptive. For example, suppose i have 4 Network, each network has multiple nodes. Now on the basis of network child should be forked and these child... (8 Replies)
Discussion started by: helpmeforlinux
8 Replies

6. Programming

Question About Multi-Processed Applications... fork()

Assume we have an application built on *nix that uses fork()...then the processes procedure is going to act as follow: X is considered a parent process (first click on application) Y is considered a child process of X (second click on application) Z is considered a child process of Y (third... (6 Replies)
Discussion started by: f.ben.isaac
6 Replies

7. Programming

Multiple process using fork()

I have a code which has four different process, each printing different message. I have provided it with user input (implemented using thread), depending on which the corresponding message from that process has to be printed.The code is shown below.when I run the pgm, it takes input such as... (1 Reply)
Discussion started by: shashi
1 Replies

8. Programming

A small question about fork()

Hello experts, I am using fork() in my code but I am confused which output comes first child or parent? I did the following code .My book shows parent first but my linux shows child first.Can anyone tell me why? #include <stdio.h> int main(){ int pid; printf("I am original process with pid... (5 Replies)
Discussion started by: mlhazan
5 Replies

9. Programming

fork multiple shells

Hi, i was trying to play with fork,exec and signal for spawning multiple new shells, but it seems that i'm doing blunder somewhere. <sample code> 1 /* 2 * The idea is to fork multpile(equal to $ULIMIT) childs 3 * and replace their images with process:csh 4 * during this the parent... (3 Replies)
Discussion started by: amit4g
3 Replies

10. Programming

simple fork question

When executing this simple program: #include <unistd.h> void main() { int f; printf("\n Parent procces ID=%d\n",getpid()); f=fork(); if(f==0) { printf("\n Child process ID=%d father=%d\n",getpid(),getppid()); } ... (2 Replies)
Discussion started by: bb666
2 Replies
Login or Register to Ask a Question