notes10 - Threads A thread is an execution path inside a...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Threads A thread is an execution path inside a process. By default, a program has 1 thread that executes the main() procedure. A thread can be created using the unix call : pthread_create(proc, args, …) Windows call : CreateThread(proc, args); proc is pointer to function. Proc will run in a separate path of execution. Main() { pthread_create( foo, arg) } Threads are useful to separate tasks in the same process where each task wait for a separate event. Threads are useful in servers. A main thread waits for requests and when a request arrives, it creates a different thread to handle the request and goes back to wait for more requests. We could accomplish concurrency in the server by creating a separate process (fork()). Creating a new process is slower than creating a thread ( new process means copying parent too and more memory requirement). Threads v/s processes Advantage of thread. - creating a thread is faster than creating a process. Fork() creates a copy in memory of the parent and that is slow. - Communication among threads is faster since they can communicate using memory ( share the same process address space). Processes can communicate with each other using pipes and files and that is slower. Disadvantage of thread - Since threads can access the data structures in memory, the data can be corrupted if 2 or more threads try to modify it at the same time. Mutex locks, semaphores and condition variables are needed to control/synchronize the access to shared data structures. - Processes are more robust. If 1 thread crashes, the entire process crashes. If a process crashes in a system of multiple processes, the other processes will continue running.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Eg of multithread program void printstr( char *s) {
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/30/2012 for the course CS 354 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 7

notes10 - Threads A thread is an execution path inside a...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online