![]() |
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 " Thanks Elico |
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: Code:
#include <stdio.h>Here's and example (from http://www.yolinux.com/TUTORIALS/Lin...ixThreads.html) that demonstrates thread creation and termination: Code:
#include <stdio.h>Basically, they're not the same things and each has its uses. Hope this helps some. |
Thnaks
I will go into it. Elico |
| All times are GMT -5. The time now is 02:44 AM. |