bunty
bunty

Reputation: 2785

How can I create a thread in unix?

How can one create a thread in unix programming?

What is difference between forking and threading?

Is threading more useful than forking?

Upvotes: 4

Views: 5001

Answers (4)

Kalanidhi
Kalanidhi

Reputation: 5092

1.In Fork kernel allocated for all resources and memory.

2.In thread split of the process and shared the memory of process

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

One usually uses POSIX threads or some other technology wrapped by its API. Forking starts new processes, threading splits an existing process into pieces. Threading results in shared global state, which may or may not be useful given the specific circumstances.

Upvotes: 5

Mentalikryst
Mentalikryst

Reputation: 760

Forking creates a copy of the current process, while threads run in the same process and are normally used to calculate something in the background so the application does not appear to be frozen.

As for the usefulness of threads vs. forking, I would go with threads unless you have a specific need for a second process.

As for how to create a thread, I would recommend using the pthreads library. It will work on any UNIX operating system (Linux, BSD, Mac OS X), but is relatively low level. If you want something higher level, check out QThread from QT.

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 753900

  1. pthread_create()

  2. Forking creates two processes, each having a separate thread of control. Creating a thread creates an extra thread of control within a single process.

  3. No - it is generally harder to get threaded applications right than it is to get separate processes right. And by quite a large margin.

Upvotes: 3

Related Questions