Implement a simple program to illustrate one producer and on
Implement a simple program to illustrate one producer and one consumer problem. The producer will
 produce 100 messages for the consumer to consume. Use an array of 10 slots to hold the contents of
 the buffer.
 The producer should send a sequence of integers to the consumer. Specifically, it should send the
 sequence 1, 2, 3, ..., 100, in that order. The consumer should receive them and check that it has indeed
 received exactly that sequence, in that order.
 Implement the program in two ways.
 1. not thread-safe, not protecting the buffer;
 2. using mechanism to protect the critical sections while they are updated.
 Figure 2-32 (page 138) of the textbook gives a solution for a single buffer producer-consumer problem
 using semaphores and conditional variable. Figure 2-35 gives a Java solution using monitors. You
 may construct your program based on these examples.
 Assume that it takes a random time between (0, 1) second to produce an item, and (0, 1) second to
 consumer an item.
 Notes:
 1. You may use any programming language, as long as there is a way to to turn off protection on
 synchronization;
 2. You may increase the number of producers and consumers for additional bonuses;
i NEED THE BONUS AS WELL.tHANKYOU
Solution
//1ST WAY IN C
 #include <stdio.h>
 
 int main () {
int n[ 100 ]; /* n is an array of 10 integers */
 int i,j;
 
 /* initialize elements of array n to 0 */   
 for ( i = 0; i < 100; i++ ) {
 n[ i ] = i + 1; /* set element at location i to i + 100 */
 }
 
 /* output each array element\'s value */
 for (j = 0; j < 100; j++ ) {
 printf(\"Producer to Consumer = %d\ \", j, n[j] );
 }
 
 return 0;
 }
//2ND WAY IN JAVA
 import java.util.*;
 import java.lang.*;
 
 class Main
 {
    public static void main (String[] args) throws java.lang.Exception
    //In the loop condtion it loops less than value less than 10
    {
        for (int i = 0; i < 10; i++) {
 new Thread1().start();
 }
    }
 
 //static method to get the Integer
 public static int getNum(int i) {
    return i + 1;
    }
 
   
 static class Thread1 extends Thread {
 static Integer value = 0;
 @Override
 public void run() {
 while (value < 100) {
 synchronized(Thread1.class) { //synchronized is the way to prevent deadlocks in threads
 value = getNum(value);
 System.out.println(\"Prouducer to Consumer\" + \"=\" + + value);
 }
 try {Thread.sleep(100);} catch (Exception ignored) {}
 }
 }
    }
 }


