Вы находитесь на странице: 1из 5

PRODUCER CONSUMER PROBLEM

Two processes share a common, fixed sized buffer, one of them, the producer, puts item into the buffer,
and the other one, the consumer, takes it out. To keep track of the number of items in the buffer, there is a
variable, count. The maximum number of items the buffer can hold is N.
The producers code will first test to see if count is N, if it is, the producer will go to sleep; if it is not the
producer will add an item and increment count.
The consumers code is similar; first test count to see if it is 0,if it is ,go to sleep; if it is non zero, remove
an item and decrement the counter. The code for both producer and consumer is shown below
#define n 100 // number of slots in the buffer
Int count=0;
// number of item in the buffer
Void producer()
{
Int item;
While(true)
//repeat forever
{
Item=produce_item(); //generate next item
If(count==N)
// if buffer is full goto sleep
Sleep();
Insert_item(item);
// put them in buffer
Count=count+1;
// increment count of item in buffer
If(count==1)
Wakeup(consumer);
//was buffer empty?
}
}
Void consumer()
{
Int item;
While(true)
{
If (count==0)
// if buffer is empty goto sleep
Sleep();
Item=removed_item();
//take them out of buffer.
Count=count-1;
//decrement count of item in buffer.
If(count==N-1)
Wakeup(producer);
//was buffer full?
Consume_item(item);
//print item.
}
}
The problem arises when the producer wants to put a new item in the buffer, but it is already full
The solution is for the producer to go to sleep,to be awakened when the consumer has removed
one or more items . Similarly, if the consumer wants to remove an item from the buffer and sees
that the buffer is empty, it goes to sleep until the producer puts something in the buffer and wakes
it up.
At first, the buffer is empty and the consumer reads count to be 0 and goes to sleep.similarly, the
producer also runs ; enters an item in the buffer and increments count ,since the count Is now 1,
the producer wakes up the sleeping consumer and it also starts running. When the consumer next
runs,it finds the count to be 0 goes to sleep. Sooner or later the producer fills up the buffer and
also goes to sleep. both will sleep forever.
SOLVING PRODUCER CONSUMER PROBLEM WITH SEMAPHORE
Semaphore solves the lost wakeup problem this solution uses three semaphores; one called full
for counting the number of slots that are full, one called empty for counting the number of slots
kashiram pokharel
Lecturer CITE

that are empty and one called mutes to make sure that the producer and consumer do not access
the buffer at the same time . full is initially 0 ,empty is initially equal to the number of slots in the
buffer, and mutex is initially 1.
Semaphore that are initialized to 1 and used by two or more processes to ensure that only one of
them can enter its critical region at the same time are called binary semaphores.Here,mutes is
binary semaphore . if each process does a DOWN just before entering its critical region and an
UP just after leacing it, mutual exclusionis guaranteed .it is essential that they (producer and
consumer)must be implemented in an individual ways in this case,they(full and empty
semaphore)ensure that the producer stops running when the bufferis full and the consumer stops
running when it is empty.
#include<prototype.h>
#define N 100
// number of slots in the buffer.
Typedef int semaphore;
//special type of int
Semaphore mutex= 1 ;
//control access to critical region.
Semaphore empty=N;
//control empty buffer slots
Semaphore full =0;
//counts full buffer slot.
Void producer()
{
Int item;
While(true)
//infinite loop
{
item=produce_item();
//Generate something to put in buffer.
Down(&empty);
//decrement empty count
Down(&mutex);
//enter critical region
Insert_item(item);
//put new item in buffer
Up(&mutex);
//leave critical region
Up(&full);
//increment count of full slots.
}
}
Void consumer()
{
Int item;
While(TRUE)
//infinite loop
{
Down(&full);
//decrement full count
Down(&mutex);
//enter critical regin
Item=remove_item();
//take item from buffer
Up(&mutex);
//leave critical region
Up(&empty);
//increment count of empty slot
consume_item(item);
}
}

THREAD
A thread is a flow of execution through the process code, with its own program counter, system
registers and stack. A thread is also called a light weight process. Threads provide a way to improve
application performance through parallelism. Threads represent a software approach to improving
performance of operating system by reducing the overhead thread is equivalent to a classical process.
kashiram pokharel
Lecturer CITE

Each thread belongs to exactly one process and no thread can exist outside a process. Each thread
represents a separate flow of control. Threads have been successfully used in implementing network
servers and web server. They also provide a suitable foundation for parallel execution of applications
on shared memory multiprocessors. Following figure shows the working of the single and
multithreaded processes

Difference between thread and process:


Process
Process is heavy weight or resource intensive.

Thread
Thread is light weight taking lesser resources
than a process.

Process switching needs interaction with


operating system

Thread switching does not need to interact with


operating system

In multiple processing environments each process


executes the same code but has its own memory
and file resources

All threads can share same set of open files, child


processes

If one process is blocked then no other process


can execute until the first process is unblocked

While one thread is blocked and waiting, second


thread in the same task can run.

Multiple processes without using threads use


more resources.

Multiple threaded processes use fewer resources

In multiple processes each process operates


independently of the others.

One thread can read, write or change another


thread's data.

Advantage of thread:
Thread minimizes context switching time.
Use of threads provides concurrency within a process.
Efficient communication.
Economy- It is more economical to create and context switch threads.
Utilization of multiprocessor architectures to a greater scale and efficiency.
Types of thread:
kashiram pokharel
Lecturer CITE

Threads are implemented in following two ways


User Level Threads -- User managed threads
Kernel Level Threads -- Operating System managed threads acting on kernel, an operating
system core
User Level Threads
In this case, application manages thread management kernel is not aware of the existence of
threads. The thread library contains code for creating and destroying threads, for passing message
and data between threads, for scheduling thread execution and for saving and restoring thread
contexts. The application begins with a single thread and begins running in that thread.

Advantages
Thread switching does not require Kernel mode privileges.
User level thread can run on any operating system.
Scheduling can be application specific in the user level thread.
User level threads are fast to create and manage.
Disadvantages
In a typical operating system, most system calls are blocking.
Multithreaded application cannot take advantage of multiprocessing
Kernel Level Threads
In this case, thread management done by the Kernel. There is no thread management code in the
application area. Kernel threads are supported directly by the operating system. Any application
can be programmed to be multithreaded. All of the threads within an application are supported
within a single process.
The Kernel maintains context information for the process as a whole and for individuals threads
within the process. Scheduling by the Kernel is done on a thread basis. The Kernel performs
thread creation, scheduling and management in Kernel space. Kernel threads are generally slower
to create and manage than the user threads.
Advantages
Kernel can simultaneously schedule multiple threads from the same process on multiple
processes.
If one thread in a process is blocked, the Kernel can schedule another thread of the same
process.
Kernel routines themselves can multithreaded.
Disadvantages
Kernel threads are generally slower to create and manage than the user threads.

kashiram pokharel
Lecturer CITE

Transfer of control from one thread to another within same process requires a mode
switch to the Kernel
Difference between user level and kernel level thread:
User level
Kernel level
User level threads are faster to create and manage Kernel level threads are slower to create and
manage
Implementation is by a thread library at the user
level

Operating system supports creation of Kernel


threads

User level thread is generic and can run on any


operating system

Kernel level thread is specific to the operating


system

Multi-threaded application cannot take advantage


of multiprocessing

Kernel routines themselves can be multithreaded

kashiram pokharel
Lecturer CITE

Вам также может понравиться