[Autogenerated] Let's look at an example for using a mut text,
to synchronize threads running in different processes.
And this is possible.
Of course, because the mut text, is a kernel object.
So here's what I want to do here.
I have some code here already where, what I'm doing,
is using a loop here of 500 times.
Opening a file,
that has the name test dot txt the CM directory If that works out,
OK.
Notice the open always constant here indicating that If the file
doesn't exist then create it if it exists just open it.
And what I'm doing here.
I'm going,
going over to the end of the file by using the site file pointer function,
and then writing,
to the end of the file a string of text which comprises this of some text.
That also has the process.
ID as part of it, and then closing, that handle,
to the file and doing that same thing 500 times.
So let's see what happens if I just run this.
I'm going to press control, a five here.
So now we have the code waiting for me.
To press enter to get things going the process, ID for this process.
Is 13,000 044 Let's press Enter here it goes.
And It's done.
We can go, to the the temp, directory here,
and Look at test dot TXT,
and you'll notice it has these 500 lines containing the text,
text from process, 3044 If you go all the way,
to the end you.
Can see In fact, we have exactly 500 lines in the file as we expect.
So let's see what happens.
once we add another process.
To the mix, and of course, we can add as many as we want.
But let's just use two that should be good enough.
To prove, a point.
So if I go ahead and press, a five Now here's one process.
Let me go back.
To visual student, press control F, five Again, we have another process here.
So now we have here two processes.
That are waiting, to execute this operation.
That We want them to execute.
So what I'm going to do here, I'm going to Press.
Enter very quickly in both of these command, Windows and See what happens.
So, unfortunately, it's just uh too fast.
That the disc, uh is too fast.
So we'll have to do some slight changes because it just wasn't um as fast,
as slow as I would have wanted.
Let's just do something very simple here.
And add, a little slip here.
Just uh one millisecond should be good enough.
Its not going to be Really one.
It's going to be more than that based on the internal clock timer,
that Windows uses which is 15.6 milliseconds by default.
But anyway, that's just to make things slightly slower.
Let's go back.
To our see temp, directory here, and remove the file to start from scratch.
Let's try it again.
I can also, of course, increase the number of iterations.
To to get to the same kind of effect,
but still let's just not bang on the disk too much.
So here's one, here's two Let's do it again.
And You notice that one of them continues to completion.
But the other one happens.
To fail with the error number, 32 and that error number,
32 If you go ahead, and look at that error using the error Look,
up simple tool, you can see it,
says The process cannot access the file because it's being used
by another process which is Exactly right.
Once the file is opened by one process.
The other one calls create file create file fails because
we have SSH sharing violation here.
And The process cannot access the file While a handle, is already open.
To the file without any ability, to share it.
So that's the problem.
We have some mis synchronization here.
So let's see if we can fix, that using a Tex.
So first let's create a TEX by calling the create Tex a P I,
the first parameter, security two attributes.
We don't really care about whether we want to be the
owner of the TEX immediately the answer,
is No, we want to Start the Tex, in the free state,
in the signal state or any thread can grab it.
And then We'll give it a name,
and We'll give it a name because otherwise we won't be able to share,
that Tex with another process.
And We know that one way of sharing is by using a name which
we've seen already in previous modules.
So now we can go ahead.
And use that mu attack So let's go ahead.
And Acquire it just before we open the file by calling
weight for single object on that mu text,
and We'll wait for as long as it takes then Once
we're done working with the file, we can go ahead.
And release the TEX.
So the point,
is to release the tex as quickly as possible once I no longer need
to access the shared data the shared data here,
is the file in this particular case, and That's basically it.
Once we're done.
We can go ahead.
And close the handle.
To the mute text properly.
Although it doesn't matter.
That much because the process will shut down anyway.
And we can test this out.
So let's press control, a five and control, a five again.
So we have these two processes waiting.
To start running.
So I'm going to press.
Enter on both console Windows very quickly.
And Notice they're running at the same time, nothing fails at this point.
Everything seems to be OK.
Now, in fact, I forgot to to raise the file content.
So let's try this again.
And also take a look at cross explore,
see What cross explorer has to show us in this respect.
So let's press control, a five twice again.
So before I run it, let me just go ahead.
And create the TEX before that So, we can have the TEX already existing.
We.
Can see that in process, explore.
Let's run this again.
So here, is our first process.
Let's run another one.
Here's our second process.
Let's go.
To cross explorer, and look for these two processes,
that have the name sync Tex.
That's the executable name, and Here's our Tex.
Here's the TEX here, a handle, in the One of these processes.
If I double-click we can see that there,
are two handles, to these mut, text, which is exactly what we expect.
We know there.
Is another one, another handle,
to the exact same Tex has the exact same address in
a different process right here.
So here goes, here's the other tex.
Well, it's actually the same tex but another handle.
And we can see Also that It's currently not being held, which means it.
Is signaled it.
Is free and If we're trying to get it.
We'll be able to get it.
So now, we can go ahead.
And Run this experiment by pressing enter quickly in both of these command,
Windows we can see the running fine running to completion.
Hopefully, here's one,
here's the second one If you go ahead and look at what happens here.
In the test T XP, file What we'll see.
Is that the first process,
that I pressed enter on that's 22392 here managed to get lots of these
outputs here but then When I press enter on the other one,
we get these text interleaved with text coming from the other
process because now it manages to get its own text.
And their interleaving here one by one in most cases, but not all cases.
Again, this is based on timing.
But eventually, at the end of the day,
we have exactly 1000 lines in the this process,
sorry, in the this file 500 lines per process added here,
the same number of lines for each process.
ID that's guaranteed because of that synchronization we
were doing without that synchronization.
Of course, we would get an error of sharing violation as we did before.
By the way, going back, to what we discussed at the beginning of this course,
if I go ahead and change the name by removing it.
What do you think we'll get,
think about that Before you continue running this video,
let me run that Here's one,
here's the second one I'm not going to Bother even rea
raising the file doesn't really matter.
That much uh as as what we do here because we'll,
we'll be able to see the problem.
Immediately.
If you go ahead and press, enter here.
And Enter here.
We'll get an error somewhere.
And and The reason for that is that We have no synchronization here anymore.
Because what happens.
Is that Every process here creates its own separate TEX.
So there's no synchronization here because each one is waiting
on its own tex which will always succeed.
Obviously.
So there's no synchronization.
We have to make sure it.
Is the same TEX because we have different processes here.
We have to share the TEX in some way,
and one of the ways that we've seen which is the simplest one.
Is using a name.
By the way,
if you want to make sure things work exactly as we plan and to make sure things.
Are robust.
We have to also use five finally here.
So in this case, it doesn't really matter.
That much because we know what this code is doing and
There's really no surprise here, but still that's the correct way of working.
To Make sure the max is released no matter what and
We've seen this termination handler, in the previous module as well.