Starting from:

$30

CSE344-Homework 4 Solved

Objective
Let’s keep it simple and abstract. There is one supplier thread and multiple consumer threads. The supplier brings materials, one by one. And the consumers consume them, two by two. That’s it. Each actor will be modeled by its own thread.

Example: ./hw4 -C 10 -N 5 -F inputfilePath

Input File (ASCII)
It will contain in total NxC times the character ‘1’ and NxC times the character ‘2’ in an arbitrary order. This file will be provided externally as input (i.e. assume it’s there). Each character in the file will correspond to a type of material. ‘1’ will correspond to the first material, and ‘2’  will correspond to the second material.

Supplier x 1
There will be only one supplier thread. It will read the input file’s contents, one character at a time and output a message concerning its activity. For every character/material read from the file, it will augment/post a semaphore representing its amount. If it reads a ‘1’ it will post the semaphore representing the amount of ‘1’s read so far, and if it reads a ‘2’ it will post the semaphore representing the amount of ‘2’s read so far. It will terminate once it reaches the end of the file. The supplier will be a detached thread. You will have no other variables for keeping count of the delivered materials.

Output examples:

// Before delivery

Supplier: read from input a ‘1’. Current amounts: 0 x ‘1’, 0 x ‘2’.

// After delivery

Supplier: delivered a ‘1’. Post-delivery amounts: 1 x ‘1’, 0 x ‘2’.

// At EOF

The Supplier has left.

Consumers x C
Each consumer will have its own thread (not detached).  Its task is to loop N times. At each iteration it will take/remove one ‘1’ and one ‘2’ by reducing the corresponding semaphores’ values. Careful, if either is not available (e.g. if there is no ‘1’ or ‘2’) then it will not take the other. In other words, it will either take two items (one ‘1’ and one ‘2’) or wait until two (one ‘1’ and one ‘2’) are available.

This way each consumer will consume in total exactly N x ‘1’s and N x ‘2’s.

Output examples:

//Before consuming (i denotes the integer id of the consumer 0 to C-1, j is the iteration number, 0 to N-1)
Consumer-i at iteration j (waiting). Current amounts: 8 x ‘1’, 12 x ‘2’.

//After consuming

Consumer-i at iteration j (consumed). Post-consumption amounts: 7 x ‘1’, 11 x ‘2’.

// After consuming N times Consumer-i has left.

Restrictions and requirements
-  C > 4 (integer)

-  N > 1 (integer)

-  All threads must run concurrently.- inputFilePath: relative or absolute - There will be only one process.

-  Don’t use more than 2 semaphores.

-  Don’t create additional threads besides a supplier and C consumers.

-  You must use only System V semaphores for synchronization.

-  All output will be written to STDOUT without buffering (each line will begin with a timestamp). - In case of SIGINT, all processes and threads will terminate gracefully, closing all open files and freeing all allocated resources.

More products