
# Week 1.5

## Assignment 5.1

> You will now create a project to run the example program main.c on the STM32F411- E-DISCO development board.  
> A) Download the project buffer.zip.  
> B) Import this project into STM32CubeIDE with the menu-option File Import. . . , General Projects from Folder or Archive , Next , Archive , select the file buffer.zip, Open and Finish . Build and debug the project. There should appear some text in the console window. Enter the following priorities: Consumer = 3, Frikandel Producer = 2, and Kroket Producer = 1. You may want to enable the Word Wrap option in the Console window, see Figure 3.  
> 
> Explain the output of the program. Be precise in your explanation. For example, explain why first all frikandellen are baked followed by all kroketten. Explain why no frikandellen are consumed after the frikandellen producer has stopped. How many snacks are stored in the buffer before the first snack is consumed?

program output:

```
Enter priority for process Consumer [1..15]: 3
Enter priority for process Frikandel Producer [1..15]: 2
Enter priority for process Kroket Producer [1..15]: 1

Output for Consumer priority = 3 frikandel Producer priority = 2 Kroket Producer priority = 1

Thread: 0x200015a0 starts

Thread: 0x20001b20 with argument: F starts
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
> FFFFFFFFFFFFFFFFFFFFFThread: 0x20001b20 stops

Thread: 0x200020a0 with argument: K starts

KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK\
> KKKKKKKKKKKKKKKKKKKKKThread: 0x200015a0 stops

Thread: 0x200020a0 stops
```

`Thread: 0x200015a0 starts` states that `0x200015a0` is the consumer, `Thread: 0x20001b20 with argument: F starts` states that `0x20001b20` is the frikandel pcroducer and `Thread: 0x200020a0 with argument: K starts` state `0x200020a0` is the kroket producer.

The consumer start first, because it has the highest priority, but the buffer is empty so it will wait until a producer puts something in it. The thread with the next priority is the frikandel producer. It does not have a delay in it, so it will continue to produce. Meanwhile every systick and when the buffer is full, the consumer takes priority and consumes the buffer. So the frikandel producer can continue to produce frikkendellen.

When the frikandellen are all produced and consumed, there is finaly time for the krokket producer and the same will happen again, but now with krokketten instead of frikandellen.

> C) The code of the consumer seems inefficient:
> 
> ```c
> char c = get();
> check_errno(sem_wait(&semPrintf));
> check_errno(printf("%c", c));
> check_errno(fflush(stdout));
> check_errno(sem_post(&semPrintf));
> ```
> 
> The use of the local variable c seems unnecessary. The following code is more compact:
> 
> ```c
> check_errno(sem_wait(&semPrintf));
> check_errno(printf("%c", get()));
> check_errno(fflush(stdout));
> check_errno(sem_post(&semPrintf));
> ```
> 
> Adjust the consumer code as discussed above. Build the program and debug it with priorities 3, 2, and 1. Explain why the program stalls. Use the FreeRTOS aware Views to help you understand what is going on.

When the consumer wait for the first producer product it is holding on the `semPrintf` semaphore. This semaphore is also used at the beginning of the producers. Because the consumer has the highest priority it start first, takes `semPrintf` and wait for the producers. when the producers start they are going to wait for `semPrintf`, but it is only given free if the procures continue. Now all task are waiting for each other.
