LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-17-2005, 05:23 AM   #1
iclinux
Member
 
Registered: Dec 2004
Posts: 69

Rep: Reputation: 15
Question why there're lots of child processes when fork?


When I create a child process that consists of multiple threads, I'll find lots of child processes in the system.

Can you tell me why, or how to solve this problem?

Regards.

Code:
pid_t spawn(const char *program, char *const arg_list[])
{   
    assert(program && arg_list);
    
    // Duplicate this process.
    pid_t children_pid = fork();
    if (children_pid != 0) { // This is the parent process.
        return children_pid;
    } else {                 // This is the child process
        // Now execute PROGRAM
        execv(program, arg_list);
        
        // The execvp function returns only if an error occurs
        fprintf(stderr, "Error occurred in spawn()'s execvp. "
                        "[Bad parameter, or Has no root privilege]\n");
        abort(); // Abort the child process, no surprise here!
    }
}

Last edited by iclinux; 01-17-2005 at 05:35 AM.
 
Old 01-17-2005, 11:12 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
In Linux, threads are implemented as a special form of processes (lightweight and sharing data).

I think what you see as processes are actually the threads.

May I recommend
Code:
perror("spawn(), execv()");
instead of:
Code:
fprintf(stderr, "Error occurred in spawn()'s execvp. "
                        "[Bad parameter, or Has no root privilege]\n");
Using perror(), you'll get more specific information in case of an error.
 
Old 01-17-2005, 06:53 PM   #3
iclinux
Member
 
Registered: Dec 2004
Posts: 69

Original Poster
Rep: Reputation: 15
hello Hko,
I'll accept your advice.
and thank you very much

But my child process has about 100 threads, it's boring to see about 100 child process name in the system after forked.
There's no method to solve this?

Quote:
Originally posted by Hko
In Linux, threads are implemented as a special form of processes (lightweight and sharing data).

I think what you see as processes are actually the threads.

May I recommend
Code:
perror("spawn(), execv()");
instead of:
Code:
fprintf(stderr, "Error occurred in spawn()'s execvp. "
                        "[Bad parameter, or Has no root privilege]\n");
Using perror(), you'll get more specific information in case of an error.
 
Old 01-18-2005, 07:09 AM   #4
iclinux
Member
 
Registered: Dec 2004
Posts: 69

Original Poster
Rep: Reputation: 15
can't be solved?
 
  


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
change child name (c fork) alaios Programming 15 09-11-2005 04:51 PM
GCC fork() - Can a child change a parent variable? johnhardey Programming 4 03-10-2005 10:49 AM
forking 7 child processes ianomc Programming 5 11-07-2004 12:33 PM
Getting a parent to communicate with its child -- fork() kamel Programming 3 06-02-2004 03:04 AM
child-parent-&-fork mukul Programming 2 03-24-2001 01:12 PM

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

All times are GMT -5. The time now is 06:38 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