Docsity

Exploring Threads, Atomicity, and Sync: Producer-Consumer Architecture Demystified, Study notes of Operating Systems

Toggle sidebar
bg1
Memory map
PC and register values.
A process includes an execution context containing
○
Switching between memory maps can take time.
○
Do maintain separate PC, registers.
Do maintain separate stacks.
All "lightweight processes" share most of memory.
In a multi-core environment, threads can execute simultaneously.
(Threads are the most used mechanism for parallel computing.)
Idea: share the memory map between a group of
cooperating "lightweight processes".
○
Voila: we have the concept of a "thread".
○
Reading: Stallings Chapter 4.
○
Threads
Threads
Tuesday, September 28, 2004
10:37 AM
Threads Page 1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download Exploring Threads, Atomicity, and Sync: Producer-Consumer Architecture Demystified and more Study notes Operating Systems in PDF only on Docsity!

 Memory map  PC and register values. ○ A process includes an execution context containing ○ Switching between memory maps can take time.  Do maintain separate PC, registers.  Do maintain separate stacks.  Don't maintain separate segments for other memory.  All "lightweight processes" share most of memory.  In a multi-core environment, threads can execute simultaneously.( Threads are the most used mechanism for parallel computing.) Idea: share the memory map between a group of cooperating "lightweight processes". ○ ○ Voila: we have the concept of a "thread". ○ Reading: Stallings Chapter 4. Threads Threads Tuesday, September 28, 2004 10:37 AM Docsity.com

Atomicity: property of code that nothing is going to happen during it, other than it. A code segment is "atomic" if it cannot be interrupted, nor can anything concurrent interfere with it. Processes (without threads and signals) are in essence atomic: the memory for a process is not changed by anything but the process. System calls are atomic; the process can't interfere while they're going on. Atomicity Tuesday, October 12, 2010 4:35 PM Docsity.com

Review of the memory map for a process Memory maps Tuesday, September 28, 2004 10:37 AM Docsity.com

Shares a memory map with other threads in the process.

 Has a separate PC and register values.  Has its own execution stack. A thread (of execution) within a process is an execution subcontext that Inside a thread Tuesday, September 28, 2004 10:37 AM Docsity.com

optimizes runtime. Hiding latency for multiple I/O requests. simplify coding. Enabling Producer/Consumer program architecture. Common thread uses Thread uses Tuesday, September 28, 2004 12:15 PM Docsity.com

Make multiple requests to servers. Get answers asychronously, one per thread. Combine at end. "fork-join parallelism" Used in web browsers. Latency hiding Latency hiding Tuesday, September 28, 2004 12:15 PM Docsity.com

Must wait for each response Must maintain each connection Without threads Without threads Tuesday, September 28, 2004 12:15 PM Docsity.com

With threads: With threads Tuesday, September 28, 2004 12:15 PM Docsity.com

a messy program that reads blocks and then does its own buffering.

two programs, one that reads blocks and writes chars, another that reads chars

little difference in performance , but a lot of difference in readability.

Is it better to have one complicated program, or two simple ones?

Two alternatives Two alternatives Wednesday, September 27, 2006 2:43 PM Docsity.com

Producer : an entity that provides data  Consumer : the entity that uses data. Pipeline : a chain of producer-consumer relationships

Producer-consumer concepts P/C concepts Wednesday, September 27, 2006 2:43 PM Docsity.com

Example of a pipeline: disk I/O architecture Every component knows what it needs to know, and no more! Pipelines Wednesday, September 27, 2006 2:43 PM Docsity.com

End of Lecture on 10/4/ Thursday, October 04, 2012 5:28 PM Docsity.com

#include <pthread.h> char name[20]; struct inputdata { // input to thread } ; int idno; struct outputdata { // output from thread } x ; // must have lifetime greater than thread struct inputdata *d = (struct inputdata *)v; printf ("hello from the thread!\n"); printf ("my name is %s\n",d->name); sleep(5); printf ("setting idno\n"); x.idno = 42; printf ("bye from the thread!\n"); return (void *) &x; void *threaded_routine (void * v) { } main() pthread_t thread; printf("hello from the parent... creating thread\n"); strcpy(input.name,"George"); struct outputdata *retptr;

Thread example Tuesday, September 28, 2004 3:08 PM Docsity.com

strcpy(input.name,"George"); struct outputdata *retptr; if (pthread_create( &thread, NULL, threaded_routine, (void *)&input)==0) { pthread_join(thread,(void **)&retptr); printf("got id number %d\n",retptr->idno); printf("pretend to do useful work….\n"); printf("could not create thread!\n"); } else { } printf("bye from the parent\n"); } See http://www.cs.tufts.edu/comp/111/examples/Threads/pthreads1.c Docsity.com