Skip to content

ggrig/C-coding-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

C-coding-sample

An interview task passed back in 2013

Description

Write a console application which has N producers (N=1…10), M consumers (M=1…10) and one data queue. Each producer and consumer is a separate thread and all threads are working concurrently. Producer thread sleeps 0…100 milliseconds randomly then it wakes up and generates a random number between 1 and 100 and then puts this number to data queue. Consumer thread sleeps 0…100 milliseconds randomly and then wakes up and takes the number from the queue and saves it to the output ‘data.txt’ file. All numbers are appended in the file and all they are comma delimited (for example 4,67,99,23,…). When producer thread puts the next number to data queue it checks the size of data queue, and if it is >=100 the producer thread is blocked until the number of elements gets <= 80. When consumer thread wants to take the next number from data queue and no elements in it, consumer thread is blocked until new element is added to data queue by a producer.

When we start application we need to insert the N (number of producers) and the M (number of consumers) after which program starts all threads. It should print current number of elements of data queue in each second. When we stop program it should interrupt all producers and wait for all consumers to save all queued data then program exits.

Classes:

  • The queue: Sets events for queue-full
  • File: sinchronized for writing
  • Producer: sleeps 0…100 milliseconds randomly; when wakes up generates a random number between 1 and 100 and then puts this number to the queue
  • Consumer: sleeps 0…100 milliseconds randomly; when wakes up takes a number from the queue and saves it to the output ‘data.txt’ file
  • Timer: generates a time event every second
  • WatchDog: on the time event outputs the queue size to the console

The app:

  • gets inputs (number of producers, consumers)
  • validates inptuts
  • starts the treads
  • waits until CTRL-C
  • stops the producers
  • generates a stop event for consumers
  • waits till consumers stop
  • exits

About

An interview task passed back in 2013

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published