LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2007, 02:37 AM   #1
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Rep: Reputation: 32
using fork()


Hi all,
i want to write a code to spawn 3 processess from a process which does 3 different tasks.... and to test that, i wil kill the spawned process and again it has to be spawned.. how to do this
thanks....
 
Old 01-15-2007, 09:00 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
To clarify, can you give us the overall application? Can you tell us _why_ you wish to do this?

(I'm presuming it's not just homework that you're asking others to do for you.)
 
Old 01-15-2007, 12:13 PM   #3
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
when a subprocess dies the kernel sends the SIGCHLD signal to the parent process. So, in the parent process you can catch that signal, and then act upon it. In this case, you want to catch the signal and mark some flag so that later in your main program, you can fork a new process to replace the recently died process.
Check the "signal" and "fork" man page.

I hope this is useful.
Cheers!
 
Old 01-15-2007, 10:33 PM   #4
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
Thanks wjevans_7d1,
ya this is not a homework, in my project i have to initiate 3 processess and have to keep track of those processess(i.e. whether they are dead or alive ) and i know only basics of linux...so asking help....
 
Old 01-15-2007, 10:34 PM   #5
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
Thanks demon_vox,
really useful info will try and post back...
 
Old 01-16-2007, 04:58 AM   #6
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
i know to create a child process by fork().. but how to create 3 processess ??? i am confused...
 
Old 01-16-2007, 06:14 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Making 3 children is the same as making 1 child, but that you do it three times, and NOT in the child processes.

Pseudo code:
Code:
main (...)
{
    spawn3();
    child_process_workflow();
}

spawn3()
{
    result = fork();
    if ( result == we are in the child )
        return;
 
    result = fork();
    if ( result == we are in the child )
        return;
 
    result = fork();
    if ( result == we are in the child )
        return;

    parent_process_after_fork_workflow();
}

parent_process_after_fork_workflow () 
{
    ...
}

child_process_workflow () 
{
    ...
}
 
Old 01-17-2007, 12:05 AM   #8
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
wow thanks matthewg42,
the problem with me was i had thought that after fork() the rest of the code till the end will be clonned !!... so,
Quote:
if ( result == we are in the child )
return;
after return the current forking is stopped right ????
 
Old 01-17-2007, 12:26 AM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
When you make a call to fork, Linux creates a new version of the process which is a child of the original. Both process have the same code in them. After the fork, they are both at the same point in the program - returning from the fork call. All the code is shared. Almost all state is shared. There are only a few differences, for example the process ID and parent process ID, and importantly, the return value from the fork call. In the parent process, the return value of the fork() call is the PID of the child process, but in the child process, 0 is returned.

Please read the fork manual page carefully - it seems you have some pretty big mis-conceptions about what fork is doing. You may or may not have to make sure some documentation package is installed to get these manual pages. Try this:
Code:
man 2 fork
And if it doesn't work, browse fedora's s/w installer and try to find them. You won't get far with C without a good reference handy, and the manual pages are a very convenient, searchable resource.
 
Old 01-17-2007, 02:49 AM   #10
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
Thanks for the suggestion matthewg42,
i know these details what u told about fork() but my doubt was how to spawn 3 different processess from the parent say.....
Code:
#include<unistd.h>
#include<sys/wait.h>
#include<stdlib.h>

int main()
{
        int pid;
        fork();
        write(1, "hello", 6);
        fork();
        write(1, "hello", 6);
        fork();
        write(1, "hello", 6);
        return 0;
}
in this code the output is hello is printed 14 times....because after the first fork till the end everything is clonned(including other 2 fork calls)....

my question is how to make the fork stop clonning...
is this possible ????

or else i need to call 3 functions seperately
Code:
spawn_child1()
spawn_child2()
spawn_child3()
and inside these function definitions i have to fork()
so, from the parent process can i spawn 3 different processess ??

Last edited by culin; 01-17-2007 at 02:56 AM.
 
Old 01-17-2007, 08:23 AM   #11
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
After the first fork, you have two processes, each of which will execute the next instruction (writing a message), then you ask them both to fork, making 4 etc... You must test the result of the fork call, and in the parent, not fork again.
 
Old 01-17-2007, 12:25 PM   #12
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by culin
Thanks for the suggestion matthewg42,
i know these details what u told about fork() but my doubt was how to spawn 3 different processess from the parent say.....
Code:
#include<unistd.h>
#include<sys/wait.h>
#include<stdlib.h>

int main()
{
        int pid;
        fork();
        write(1, "hello", 6);
        fork();
        write(1, "hello", 6);
        fork();
        write(1, "hello", 6);
        return 0;
}
in this code the output is hello is printed 14 times....because after the first fork till the end everything is clonned(including other 2 fork calls)....

my question is how to make the fork stop clonning...
is this possible ????
You have to check to make sure you’re not in the child process before forking again. You can easily do this either in a loop or a separate function. For a loop, e.g.,
Code:
#include<unistd.h>

#define N_FORKS 3

int main()
{
        int i;
        for(i = 0; i < N_FORKS; i++) {
                if(!fork()) // i.e., if we’re in the child
                        break;
        }
        write(1, "hello\n", 7);
        return 0;
}
should give you four “hello”s (one for the parent, and one for each of three children). The function approach was shown by matthew, and is probably more useful if you’re actually interested in doing something differently in each process.
 
Old 01-17-2007, 10:59 PM   #13
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
Thanks all for the co-operation
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fork beginner_84 Programming 2 08-20-2004 04:53 AM
fork() vibhory2j Linux - Software 1 05-24-2004 04:11 AM
about fork eshwar_ind Programming 5 02-11-2004 03:38 AM
over-fork luzi82 Linux - Newbie 2 01-02-2004 06:55 AM
fork() lowlifeish Programming 3 11-04-2002 10:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration