[Autogenerated] Let's see how we can work this out.
And make this connection between a controller process,
and other worker processes.
So I'm going to create a console application here.
In fact,
I'm going to use the same executable where the first
instance will serve as the controller.
And All other instances will serve as the worker processes.
To demonstrate something else which is I think quite important.
So I'm going to create a new C++ console application.
As always,
I'm going to Keep it with a simple name called app and just
put it in a different solution called demos.
So,
that we can have all the daemons that I'm doing in this
module part of the same solutions.
Let me just remove this boiler plate,
an code provided by Visual Studio increase the font size,
a little bit, and we can be on our way.
Now, how can we do that How can we share something between these processes?
So here's a Very simple idea.
We can use a file in the file system, we can put a file in particular,
directory that is known, to all these processes,
and then Every process needs to look inside that file let's say
that If the value some kind of value inside.
These file says One, it means let's shut down.
So the controller process will put a one there at the point where shutdown,
is necessary,
and The other processes will pull will read the data inside that file from time.
To time,
to see whether shutdown is needed for their
respective processes would that work.
Yes, it would.
But obviously this is not very efficient.
Instead we want to use a different mechanism.
That mechanism would be an event object.
We discuss event objects in greater detail later on in the This course.
But for now, let's just use it.
You can click an an event object, is just like a flag.
You can raise the flag or you can lower the flag when the flag.
Is raised.
It means something interesting has happened.
That someone might be interested in and So I'm going to create an
event object here by using the Windows API function create event
Just think of that as as just a flag, a brilliant flag,
and I can provide various parameters.
In fact, I have to provide some parameters which in this case,
are not important for our purposes.
The most important is this one which states that the
event starts in a non signal state, its in the false or down state,
which means that There's no need.
To shut down at this point.
So we are starting with this non shut down state.
And the point here, is that I want to have a name for this object.
That I'm going to create now because that name will be the mechanism.
To share this object between multiple processes because my handle,
that I get back.
Is only a private handle in my process.
So let's call that Shut down, event just as a good kind of nice name.
To have and uh this is supposed to uh to succeed.
But if it fails,
let's just write some something here indicating that There's
an error and just display whatever get.
Last ______ has.
To say it should succeed in general.
However, it could fail in certain very weird cases.
For example, let's say there, is another kernel object,
that is not an event Let's say a Semaphore,
that happens.
To have the name shut, down event that would be a contradiction,
and the core will fail.
But for our purposes, it, is unlikely, to fail.
Let me just display the process here.
And the handle uh itself.
Let's just do that in order for us.
To be able to look at cross explore later.
And find, that handle.
Let's just display the value of that handle with each event So here's the idea,
I would like the first process.
To be created to function as the controller.
So how can I tell whether that event object,
is a new event object, and I have a handle,
to it or maybe I get another handle.
To an existing event object,
that has that specified name this is What Shane by name means if I
try to create or open an event with the same name,
I'm going to Get another handle.
And not really a True, truly.
A new event So fortunately, the get last error function can help us here.
So get last error is usually about giving us the last error that
happened on this thread with the last API call But in fact,
even in the case of success, it has a meaning for these kinds of cases.
And If the object already existed before the call it
means I have not the first handle.
The object wasn't just created.
In fact, it existed already.
Otherwise, if we have the value zero there,
which is error success means that this is a brand new object.
If its, if its a brand new object,
and say this is going to be my controller process,
and Here's the process I idea let me display,
that as well.
And then I'm going to write something like press enter,
to send shut, down notification, and I'm going to display the process.
ID by using get current process, ID here again, to make it easier.
To see what's going on using tools, and that point,
I'm going to wait for an Enter being used.
Just as a way, to Say, OK, we're done.
We need to shut down or send notification of shutting
down to the Other processes I'm using.
Get S here.
Just to get some kind of value.
And then I'm going to call the set,
event API which is the one that says Let's signal
the event let's raise the flag.
And This point, we know we have sent notification.
Let's just write that down cent, and We're done the only thing left,
to do is to Properly close the handle,
to the event just to be nice and We're done at least.
In the This case where we are the controller.
What happens if we get error already exists.
It means we are supposed to be a worker process.
Let's just write that down.
Like let's suppose we are worker process here.
And again.
Let me display the process.
I didn't say waiting for shut down Now, obviously,
this is a very simple example, but just know that in general,
we would have a more significant code here doing some actual work.
And then Some thread will be waiting for that notification,
which we'll do in a moment.
So here's my process.
ID here, and in order to Wait until the event is signaled.
We're going to call the wait for a single object
function we've met in previous courses.
And We will discuss in greater detail later on.
In this course,
I'm going to Wait until that event becomes signaled for as long
as it takes and Once that becomes signaled,
the thread ends its weight, and we can say shut,
down received or let's just write a shutting down or something
like that just to indicate that We have,
we have received identification, we're doing whatever needs.
To be done.
To gracefully shut down.
That particular process.
So let's try it out.
Let's build, that and in order to test,
that I'm going to run multiple instances of course.
And I expect the first instance.
To be identified as a controller, and the subsequent instances,
to be identified as workers.
So we'll do that in the Next video.