Operating System Development: Stage One

Project Download

In this stage we will look at threads and the stack in Visual Basic .Net 2005.

Firstly, included on this page is a download to 'Stage One.zip' which includes the source code for this stage.

In an operating system, you require a stack for general data operations and also threads for process accounting.

The source code provided in this section regarding threading uses a restricted threading model. We will come to a more advanced threading architecture later on, using the Thread Class.

The stack program example is a simple example that pushes up to 10 integers onto the stack and pops them off again at the push of a button. The threading example, runs two threads concurrently for thirty seconds and records the amount of time it takes for both threads to complete.

An understanding of how the threading operation works and also the stack is vital to operating system development.

The source code for the threading program, and the stack program, follows;

Stage Two

Public Class Form1
REM Stack Program
'Declare the stack directive
Dim Stack As New Collections.Stack()
Dim counter As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Make sure the stack is not overflowed
If Stack.Count < 10 Then
'Increment the counter and push it on the stack
counter += 1
Stack.Push(counter)
'Declare the push in the textbox
TextBox1.Text &= "Pushed " & counter.ToString & " on stack." & vbCrLf
'scroll the text box down
TextBox1.SelectionStart = TextBox1.TextLength
TextBox1.ScrollToCaret()
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'Make sure the stack is not empty...
If Stack.Count > 0 Then
'Decrement the counter and pop it off the stack
counter -= 1
'Declare the stack pop in textbox
TextBox1.Text &= "Popped " & CType(Stack.Pop, String) & " off the stack." & vbCrLf
'scroll the textbox down
TextBox1.SelectionStart = TextBox1.TextLength
TextBox1.ScrollToCaret()
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Initialise the textbox and counter
TextBox1.Text = ""
counter = 0
End Sub
End Class


Public Class Form1
REM Thread Program
Dim StartTime As DateTime = DateTime.Now
Dim ResultA, ResultB As Integer

Private Delegate Function TimeConsumingTask(ByVal seconds As Integer) As Integer

'Start the first task on a new thread
' Specify a delay of 30 seconds
Dim AsyncInvoker As New TimeConsumingTask(AddressOf TimeConsumingTaskA)
Dim AsyncResult As IAsyncResult

Private Function TimeConsumingTaskA(ByVal seconds As Integer) As Integer
Return Delay(seconds)
End Function
Private Function TimeConsumingTaskB(ByVal seconds As Integer) As Integer
Return Delay(seconds)
End Function

Private Function Delay(ByVal seconds As Integer) As Integer
Dim StartTime As DateTime = DateTime.Now
Do
'do nothing

Loop While DateTime.Now.Subtract(StartTime).TotalSeconds < seconds
Return seconds
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TextBox1.Text = "Thread A Running for 30 seconds"
Application.DoEvents()

AsyncResult = AsyncInvoker.BeginInvoke(30, Nothing, Nothing)

'Start the second task on the main thread
' Specify a delay of 30 seconds
' This blocks until complete
TextBox1.Text &= vbCrLf & "Thread B Running for 30 seconds"
TextBox1.Text &= vbCrLf & "Please wait 30 seconds"
Application.DoEvents()

ResultB = TimeConsumingTaskB(30)

'Retreive the result of the asyncronous tasks
'if it is not already complete, this blocks until complete
ResultA = AsyncInvoker.EndInvoke(AsyncResult)

TextBox1.Text &= vbCrLf & "Method A delayed for: " & ResultA.ToString()
TextBox1.Text &= vbCrLf & "Method B delayed for " & ResultB.ToString()
TextBox1.Text &= vbCrLf & "Total seconds taken to execute: " & DateTime.Now.Subtract(StartTime).TotalSeconds.ToString()

End Sub
End Class