Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Processes/Threads Homework problem(assignment #1) for ics 462 Operating Systems

ID: 3877255 • Letter: P

Question

Processes/Threads Homework problem(assignment #1) for ics 462 Operating Systems This assignment is worth 10 points This is due by 2 pm Feb. 1, 2018. ram with two processes, a producer and a consumer. If you want to use Make a C/C++ or Java prog another language, clear it with me first The producer process consists of a loop that writes the loop count (a value from 0 to 4) into a variable that it shares with the consumer process (this variable is to be initialized to 100). On each pass through the loop, before the producer writes into the shared variable, it does a random wait of from one to three seconds (compute a new random wait value on each pass through the loop). The loop is to be executed five times. The consumer process consists of a loop that reads the variable it shares with the producer five times and computes the sum of the values it has read. On each pass through the loop before it reads the shared variable, it does a random wait of from one to three seconds (compute a new random value on each pass through the loop). When the loop finishes, the program is to write the sum into a file You must run the program twice. Note that the output from the runs will almost certainly be different if you have written your program correctly. E-mail the source code, a copy of the output from each of your runs for grading (the output file must be to a data file and not a screen shot) to me. Also send me a word or text file with your observations on the assignment: What did you notice about the runs? What if anything did you learn doing this assignment? Roughly how long (in hours) did the assignment take? Please put comments at the top of your program with your name, the date, the assignment number, and a brief description of the program. I also want to see comments within the program. The first items in your data file and observations file must be your name and the assignment number (have your program output this information into your output file). Note, I do not want screen shots, your program must output to a data file which is to be sent to me. You may zip your files together, but do not send me a tar file or another type of compression file or place the files out on the Internet or the Cloud for me to pick up. If you do not know how to create a process in Java or C/C++ or do not know how to do a random wait, or how to output to a data file, it is your responsibility to learn how. This information is available on the Internet. The contents of you output file should look something like this (where ### is your computed sum). John Smith ICS 462 Assignment #1 The sum is ###

Explanation / Answer

//java code for producer and consumer as per your use

import java.util.LinkedList;

public class Threadex

{

    public static void main(String[] args)

                        throws InterruptedException

    {

        // producer() and consumer()methods are present in class

        final P p = new P();

        // Create a thread for producer

        Thread thread1 = new Thread(new Runnable()

        {

            @Override

            public void run()

            {

                try

                {

                    p.producer();

                }

                catch(InterruptedException e)

                {

                    e.printStackTrace();

                }

            }

        });

        // Create a thread for consumer

        Thread thread2 = new Thread(new Runnable()

        {

            @Override

            public void run()

            {

                try

                {

                    p.consumer();

                }

                catch(InterruptedException e)

                {

                    e.printStackTrace();

                }

            }

        });

        // Start threads thread1 and thread2

        thread1.start();

        thread2.start();

        // thread1 finishes before thread2

        thread1.join();

        thread2.join();

    }

    // This class has a linkedlist in which producer have to //adds items in the list and consumer have to removes those //items).

    public static class P

    {

        // Create a linkedlist used by producer and consumer

        // list size is 2.

        LinkedList<Integer> l = new LinkedList<>();

        int capacity = 2;

        // Producer Function

        public void producer() throws InterruptedException

        {

            int value = 0;

            while (true)

            {

                synchronized (this)

                {

                    // producer thread waits while list

                    // is full

                    while (l.size()==capacity)

                        wait();

                    System.out.println("Producer produced-"

                                                  + value);

                    // to put the jobs in the linkedlist

                    l.add(value++);

                    // to notify the consumer thread

              

                    notify();

                    // and sleep

                    Thread.sleep(1000);

                }

            }

        }

        // Consumer Function

        public void consumer() throws InterruptedException

        {

            while (true)

            {

                synchronized (this)

                {

                    // consumer thread waits if linkedlist

                    // is empty

                    while (l.size()==0)

                        wait();

                    //to used the initial job in the linkedlist

                    int val = l.removeFirst();

                    System.out.println("Consumer consumed-"

                                                    + val);

                    // notify producer thread

                    notify();

                    // and sleep

                    Thread.sleep(1000);

                }

            }

        }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote