Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.


  Search this Thread
Old 09-25-2012, 08:42 AM   #1
Registered: Dec 2011
Posts: 145

Rep: Reputation: Disabled
FORK( ) ? why not threads

The FORK statement generates a father and a son, each is doing it's own things .

What is the big deal in this kind of
coding structure ?

Why not threads that do not depend on each other
and can communicate .

What is the big advantage of "Ftaher and Son "

Old 09-25-2012, 10:26 AM   #2
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
fork() is used to create processes; the process that calls fork() is called the parent and the new process is called the child; so, nope, a "father" is not created, only the "son."

Your shell program does this: you type a command and hit the return/ether key and the shell forks and executes your command in a child process, a new process. The parent waits until the child completes (and dies or you kill it with, say, Ctrl-C) returning you to your shell prompt.

In C programming you would use fork(), wait() and one of the exec() functions in a child process in lieu of using the system() function (it's more efficient to "fork and exec" than to call system()).

POSIX Threads (pthreads) can be used to implement parallelism; not the same thing. POSIX Threads are most effective on multi-processor or multi-core systems where the process flow can be scheduled to run on another processor thus gaining speed through parallel or distributed processing.

Here's a simple example of "fork and exec;" it is a simple command interpreter (the shell is a command interpreter albeit with a lot more function and capabilities) that prompts for a command the executes it:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>

void    main    (void)
        char    line [BUFSIZ];
        int     process;

        for ( ; ; ) {
                (void) fprintf (stderr, "cmd: ");
                if (gets (line) == (char *) NULL)
                        exit (EXIT_FAILURE);
                /*      create new process      */
                if ((process = fork ()) > 0)
                        (void) wait ((int *) NULL);
                else if (process == 0) {        /* child        */
                        /*      execute program                 */
                        (void) execlp (line, line, NULL);
                        /*      some problem if exec returns    */
                        (void) fprintf (stderr, "can't execute %s\n", line);
                        exit (errno);
                } else if (process == -1) {     /* can't create */
                        (void) fprintf (stderr, "can't fork\n");
                        exit (errno);
Pretty straight-forward.

Here's and example (from that demonstrates thread creation and termination:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *print_message_function( void *ptr );

     pthread_t thread1, thread2;
     char *message1 = "Thread 1";
     char *message2 = "Thread 2";
     int  iret1, iret2;

    /* Create independent threads each of which will execute function */

     iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
     iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);

     /* Wait till threads are complete before main continues. Unless we  */
     /* wait we run the risk of executing an exit which will terminate   */
     /* the process and all threads before the threads have completed.   */

     pthread_join( thread1, NULL);
     pthread_join( thread2, NULL); 

     printf("Thread 1 returns: %d\n",iret1);
     printf("Thread 2 returns: %d\n",iret2);

void *print_message_function( void *ptr )
     char *message;
     message = (char *) ptr;
     printf("%s \n", message);
Now, bear in mind that POSIX Threads are useful on multi-core or multi-procesor systems where the operating system schedules and manages the threads (Linux does).

Basically, they're not the same things and each has its uses.

Hope this helps some.

Last edited by tronayne; 09-25-2012 at 10:28 AM.
1 members found this post helpful.
Old 09-26-2012, 11:28 PM   #3
Registered: Dec 2011
Posts: 145

Original Poster
Rep: Reputation: Disabled
I will go into it.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: HTML5: To Fork or Not to Fork LXer Syndicated Linux News 1 07-25-2012 07:58 PM
some threads are become unnoticed because of large number of continious threads deepak_cucek LQ Suggestions & Feedback 9 08-20-2009 11:21 PM
kill threads on fork, no exit handlers? ta0kira Programming 3 08-16-2007 07:15 PM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer

All times are GMT -5. The time now is 10:36 AM.

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