I am struggling with semaphores. Can someone explain what is happening here? please comment the…

I am struggling with semaphores. Can someone explain what is happening here? please comment the code as well.

#include // pthread
#include // prinftf
#include // random
#include // sleep
#include // semaphores
// you might need more header files
void *doctor_actions();
void *car_actions(void *car_id);
int NUM_CARS = 5;
//To set a limit for the line
int line = 0;
int maxLength = 3;
//Id to the first car in the line
int first;
//Define semaphores
sem_t queue;
sem_t lock;
sem_t docWait;
sem_t carWait;
void vaccine(int id) {
 first = id;
 if (line == 1)
 printf("Car %d honks to wake the doctor. n", id);
 //Wakes up the doctor
 //Wait for the doctor
void entering(int id) {
 line = (line + 1);
 printf("Car %d enters the line. Car waiting = %d. n", id, line);
void exiting() {
 line = (line - 1);
int main(int argc, char **argv) {
 int car_ids[NUM_CARS];
 pthread_t cars[NUM_CARS];
 pthread_t doctor;
 //init. semaphores
 sem_init(&queue, 0, 1);
 sem_init(&lock, 0, 1);
 sem_init(&docWait, 0, 0);
 sem_init(&carWait, 0, 0);
 // create threads
 pthread_create(&doctor, NULL, doctor_actions, NULL);
 for (int i = 0; i < NUM_CARS; i++) {
 car_ids[i] = i;
 pthread_create(&cars[i], NULL, car_actions, (void *)&car_ids[i]);
 // join threads
 pthread_join(doctor, NULL);
 for (int i = 0; i < NUM_CARS; i++) {
 pthread_join(cars[i], NULL);
 return 0;
//Doctor thread
void *doctor_actions() {
 while (1) {
 //Sleeps utill another car wakes it up
 //Taking the vaccine
 int vacTime = rand() % 10;
 printf("Patient in car %d is getting tested. Testing for %d seconds. n", first, vacTime);
 printf("Patient in car %d finished testing. n", first);
 if(line == 0)
 printf("No cars waiting. Sleeping");
//Car thread
void *car_actions(void *car_id) {
 // get the id of this car
 int id_car = *(int *)car_id;
 while (1) {
 //Taking a sunday drive around the block
 int time = rand() % 10;
 printf("Car %d is driving for %d seconds. n",id_car,time);
 //Critical! If the line is full, start over
 if (line >= maxLength){
 printf("Car %d will try again later. n", id_car);
 //Critical! Entring the queue
 //Critical! Waiting in queue, to take the vaccine
 //Critical! Exiting the queue

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
The price is based on these factors:
Academic level
Number of pages
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more
error: Content is protected !!