synchronization

synchronization - Computer Science 322 Operating Systems...

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

View Full Document Right Arrow Icon
Computer Science 322 Operating Systems Mount Holyoke College Spring 2008 Topic Notes: Process Synchronization Cooperating Processes An Independent process is not affected by other running processes. Cooperating processes may affect each other, hopefully in some controlled way. Why cooperating processes? information sharing computational speedup modularity or convenience It’s hard to find a computer system where processes do not cooperate. Consider the commands you type at the Unix command line. Your shell process and the process that executes your com- mand must cooperate. If you use a pipe to hook up two commands, you have even more process cooperation (See the shell lab later this semester). For the processes to cooperate, they must have a way to communicate with each other. Two com- mon methods: shared variables – some segment of memory accessible to both processes message passing – a process sends an explicit message that is received by another For now, we will consider shared-memory communication. We saw that threads, for example, share their global context, so that is one way to get two processes (threads) to share a variable. Producer-Consumer Problem The classic example for studying cooperating processes is the Producer-Consumer problem. Buffer Producer Consumer
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 322 Operating Systems Spring 2008 One or more produces processes is “producing” data. This data is stored in a buffer to be “con- sumed” by one or more consumer processes. The buffer may be: unbounded – We assume that the producer can continue producing items and storing them in the buffer at all times. However, the consumer must wait for an item to be inserted into the buffer before it can take one out for consumption. bounded – The producer must also check to make sure there is space available in the buffer. Bounded Buffer, buffer size n For simplicity, we will assume the objects being produced and consumed are int values. This solution leaves one buffer entry empty at all times: Shared data int buffer[n]; int in=0; int out=0; Producer process while (1) { ... produce item; ... while (((in+1)%n) == out); / * busy wait * / buffer[in]=item; in=(in+1)%n; } Consumer process while (1) { while (in==out); / * busy wait * / item=buffer[out]; out=(out+1)%n; ... consume item; ... } 2
Background image of page 2
CS 322 Operating Systems Spring 2008 See Example: /cluster/examples/prodcons-shmem See Example: /cluster/examples/prodcons-pthreads Is there any danger with this solution in terms of concurrency? Remember that these processes can be interleaved in any order – the system could preempt the producer at any time and run the consumer. . Things to be careful about are shared references to variables. Note that only one of the processes can
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 25

synchronization - Computer Science 322 Operating Systems...

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

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