-   Linux - Embedded & Single-board computer (
-   -   FORK( ) ? why not threads (

elico 09-25-2012 09:42 AM

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 "


tronayne 09-25-2012 11:26 AM

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.

elico 09-27-2012 12:28 AM

I will go into it.


All times are GMT -5. The time now is 08:37 PM.