2018年4月17日 星期二

[C][Concept] some differences between fork() and pthread()

In C there are some differences however:

**fork()**

-   Purpose is to create a new process, which becomes the child process of the caller
    
-   Both processes will execute the next instruction following the fork() system call
    
-   Two identical copies of the computer's address space,code, and stack are created one for parent and child.
    

Thinking of the fork as it was a person; Forking causes a clone of your program (process), that is running the code it copied.

----------

**pthread_create()**

-   Purpose is to create a new thread in the program which is given the same process of the caller
    
-   Threads within the same process can communicate using shared memory. (Be careful!)
    
-   The second thread will share data,open files, signal handlers and signal dispositions, current working directory, user and group ID's. The new thread will get its own stack, thread ID, and registers though.
    

Continuing the analogy; your program (process) grows a second arm when it creates a new thread, connected to the same brain.

Ref:

0 意見:

張貼留言