1
00:00:00,670 --> 00:00:01,700
Hello, welcome back.

2
00:00:02,350 --> 00:00:10,960
In this lesson, we shall give an overview of semaphores so a SEMAFO is a signal or a key send between

3
00:00:10,960 --> 00:00:13,930
tasks or between tasks and interrupt.

4
00:00:14,740 --> 00:00:16,870
It does not carry any data.

5
00:00:19,830 --> 00:00:23,520
We can categorize semaphores into three groups.

6
00:00:24,420 --> 00:00:31,830
You would see that they overlap a lot, but you would understand the tiny differences, so.

7
00:00:34,390 --> 00:00:42,380
We have binary semaphores, this SEMAFO can assume two values, one or zero, and this indicates whether

8
00:00:42,380 --> 00:00:44,710
at TASC has the key or it doesn't.

9
00:00:44,980 --> 00:00:48,250
So in a binary SEMAFO, you either have the key or you don't have the key.

10
00:00:49,520 --> 00:00:56,330
We also have counting SEMAFO, this SEMAFO has a counter associated with it and the semaphore and the

11
00:00:56,330 --> 00:01:03,620
counter can be incremented or decrements at the counter indicates the number of keys available to access

12
00:01:03,620 --> 00:01:04,580
a particular resource.

13
00:01:04,790 --> 00:01:07,090
You can think of the number of keys available to.

14
00:01:08,360 --> 00:01:16,430
To enter a room with a limit, a capacity, OK, counting semaphores gives us this ability and also

15
00:01:16,430 --> 00:01:21,320
we have something known as a mutex mutex stands for mutual exclusion.

16
00:01:21,530 --> 00:01:28,630
And this is used to allow multiple task to access a single shared resource, but only one at a time.

17
00:01:29,150 --> 00:01:32,900
We see how all of these examples work in real code.

18
00:01:33,530 --> 00:01:34,430
Now, let's see.

19
00:01:36,250 --> 00:01:43,660
Let's see some of the commonly used APIs to create a binary semaphore, we simply need to use the function

20
00:01:43,660 --> 00:01:45,880
x semafo create binary.

21
00:01:46,780 --> 00:01:53,500
And then to give the key meaning, you finish using the SEMAFO or you finish using, you can think of

22
00:01:53,500 --> 00:01:57,750
this as a key to the washroom or the restroom, you finish using the restroom.

23
00:01:58,090 --> 00:02:03,460
So you want to give the key back to give the key back so that someone else can take it and go to the

24
00:02:03,460 --> 00:02:04,010
restroom.

25
00:02:04,300 --> 00:02:08,170
You use the semaphore give and this takes us argument.

26
00:02:08,380 --> 00:02:10,240
The handle of the semaphore.

27
00:02:11,330 --> 00:02:16,160
And there is a version for this when we are dealing with interrupt service routines and one thing I

28
00:02:16,160 --> 00:02:18,770
should point out in frittatas.

29
00:02:20,090 --> 00:02:26,700
All the functions that can be called in and interrupt service routine has the word hostilities, I guess

30
00:02:26,750 --> 00:02:28,150
are at the end of the name.

31
00:02:28,640 --> 00:02:34,880
We are only allowed to use APIs with the letters, I guess are at the end of their name in interrupt

32
00:02:34,880 --> 00:02:35,870
service routines.

33
00:02:36,230 --> 00:02:42,440
And we see when we stop working and interrupt service routines, we'll be using these functions so it's

34
00:02:42,440 --> 00:02:46,130
not allowed to use X semafo, give an uninterrupted service routine.

35
00:02:46,130 --> 00:02:49,730
If we are not interrupt service routine, we have to use X semaphore.

36
00:02:50,420 --> 00:02:51,680
Give from interrupt.

37
00:02:53,190 --> 00:03:00,000
Right, and this takes to document the SEMAFO, and the second argument is a higher priority task to

38
00:03:00,000 --> 00:03:05,970
be Wolken, this particular one will be to describe the second argument much more in the code when we

39
00:03:05,970 --> 00:03:06,880
are explaining it.

40
00:03:07,420 --> 00:03:17,130
OK, next, we have the API for taking the key or the SEMAFO, and this takes to document the handle

41
00:03:17,130 --> 00:03:22,460
of the SEMAFO and it takes to weight, which is very common in most of our APIs.

42
00:03:23,250 --> 00:03:25,770
And now we look at counting semaphores by default.

43
00:03:25,770 --> 00:03:29,610
Counting semaphores are not enabled to use contin semaphores.

44
00:03:29,610 --> 00:03:36,240
We have to go to our free Arktos, configured each file and set config use in semaphores to one.

45
00:03:37,060 --> 00:03:41,200
We can use counting semaphores to count events or to manage resources.

46
00:03:42,610 --> 00:03:50,260
Over here to create account in SEMAFO, we see Ximo for Crate Co. this takes to argument.

47
00:03:51,430 --> 00:03:56,260
The maximum number of count and the initial number of count, so if let's say.

48
00:03:57,960 --> 00:04:04,380
We are using counting semaphore to create the number of tickets for a nightclub and the capacity of

49
00:04:04,380 --> 00:04:05,940
the nightclub is 50.

50
00:04:07,070 --> 00:04:14,750
We can see X semafo crate counting, the maximum count is 50 if we have given five already, we can

51
00:04:14,750 --> 00:04:16,280
see the initial count is five.

52
00:04:16,790 --> 00:04:19,330
But often Woog said the initial count zero.

53
00:04:19,940 --> 00:04:20,410
Right.

54
00:04:22,140 --> 00:04:24,810
Also, take a look at mutex.

55
00:04:26,010 --> 00:04:33,030
We said we can we can categorise him by force into binary semaphores, counting, semaphores and mutex,

56
00:04:33,270 --> 00:04:41,190
and we said we have to enable counting semaphores in our free Archos config that HFA also which mutex

57
00:04:41,190 --> 00:04:47,520
we have to enable this in our free ARTUS conflict each we have to set config use new Texas to one.

58
00:04:51,720 --> 00:04:52,170
And.

59
00:04:53,380 --> 00:04:59,350
To create a mutex we simply call SEMAFO, create mutex function as simple as that.

60
00:04:59,710 --> 00:05:09,160
Now, before we go to our IDY and write the code, let's talk about some some issues that we might face

61
00:05:09,160 --> 00:05:12,580
when we don't properly use semaphores and mutex.

62
00:05:14,700 --> 00:05:21,690
We have something known as priority inversion, this occurs when a higher priority task is waiting for

63
00:05:21,690 --> 00:05:23,310
a lower priority task.

64
00:05:24,630 --> 00:05:30,660
And because it is waiting for this lower priority task, it has its inherently assumes the priority

65
00:05:30,660 --> 00:05:32,730
of the lower priority task.

66
00:05:33,180 --> 00:05:38,880
So you can think of a higher priority task waiting for a semaphore from a lower priority task.

67
00:05:39,860 --> 00:05:44,900
Ordinarily, we have to have the higher priority task running if it needs to run.

68
00:05:45,960 --> 00:05:51,690
But in this case, because it is waiting for something from the lower priority task, its priority gets

69
00:05:51,690 --> 00:05:54,200
inverted, its priority becomes law.

70
00:05:55,130 --> 00:05:57,320
This is what we call priority version.

71
00:05:58,490 --> 00:06:06,470
Also, there is something known as priority inherit inheritance, and this involves raising the priority

72
00:06:06,470 --> 00:06:12,890
of a resource holder to the priority of the higher priority task, waiting for the resource for a short

73
00:06:12,890 --> 00:06:13,790
duration of time.

74
00:06:14,210 --> 00:06:21,650
So we can if a lower priority task has the key or a resource, we can increase its priority to match

75
00:06:21,650 --> 00:06:27,150
the priority of the higher priority task, waiting for the key for a short duration of time.

76
00:06:27,740 --> 00:06:30,440
This is called priority inheritance.

77
00:06:30,740 --> 00:06:34,680
And one last one, which gets a lot of people in trouble is the deadlock.

78
00:06:35,030 --> 00:06:37,280
This is also known as the deadly Embry's.

79
00:06:37,700 --> 00:06:43,910
This occurs when two tasks cannot proceed because they are both waiting for a resource that is held

80
00:06:43,910 --> 00:06:44,930
by the other one.

81
00:06:46,480 --> 00:06:51,910
It's as if the ball has to be bounced, like in basketball, the ball will be thrown in the air and

82
00:06:51,910 --> 00:06:55,080
someone would grab it and then they start controlling the ball.

83
00:06:55,720 --> 00:07:01,060
But you can imagine throwing the ball in the air and the ball is stuck and no one grabs the ball.

84
00:07:01,940 --> 00:07:08,390
You can think of that as a deadlock, of course, would would show a practical example with with code

85
00:07:08,570 --> 00:07:11,120
when we start writing our code, right.

86
00:07:11,280 --> 00:07:15,650
So we have priority inversion, priority inheritance and deadlocks.

87
00:07:17,580 --> 00:07:22,890
Also, there is a task known as the gatekeeper task, which which will introduce, you know, practical

88
00:07:22,890 --> 00:07:28,710
lessons, a gatekeeper task is a task that has the sole ownership of a resource.

89
00:07:29,520 --> 00:07:34,290
Only the gatekeeper task is allowed to access that resource directly.

90
00:07:34,710 --> 00:07:41,970
Any other task needing to access that resource can do so only indirectly by using the services of the

91
00:07:41,970 --> 00:07:43,080
gatekeeper task.

92
00:07:43,320 --> 00:07:48,360
And we can use the gatekeeper task approach to reduce the problems we face.

93
00:07:48,360 --> 00:07:54,870
When we don't properly configure our semaphores, we shall see how to apply the gatekeeper task as well.

94
00:07:55,620 --> 00:07:58,940
So this order is for an overview on semaphores.

95
00:07:58,950 --> 00:08:03,690
If you have any questions, kindly leave them in the questions and answer section and I'll show you

96
00:08:03,690 --> 00:08:04,500
the next lesson.

97
00:08:04,500 --> 00:08:04,650
How?
