How to resolve the algorithm Fork step by step in the BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Fork step by step in the BASIC programming language
Table of Contents
Problem Statement
Spawn a new process which can run simultaneously with, and independently of, the original parent process.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fork step by step in the BASIC programming language
Source code in the basic programming language
' Fork
pid = FORK
IF pid = 0 THEN
PRINT "I am the child, my PID is:", MYPID
ENDFORK
ELIF pid > 0 THEN
PRINT "I am the parent, pid of child:", pid
REPEAT
PRINT "Waiting for child to exit"
SLEEP 50
UNTIL REAP(pid)
ELSE
PRINT "Error in fork"
ENDIF
Function script(s As String) As String
Dim As String g = _
"Set WshShell = WScript.CreateObject(""WScript.Shell"")" + _
Chr(13,10) + "Return = WshShell.Run("""+s+" "",1,0)"
Return g
End Function
Function guardaArchivo(nombreArchivo As String, p As String) As String
Dim As Long n = Freefile
If Open (nombreArchivo For Binary Access Write As #n) = 0 Then
Put #n,,p
Close
Else
Print "No se puede guardar " + nombreArchivo : Sleep : End
End If
Return nombreArchivo
End Function
Sub ejecutaScript(nombreArchivo As String)
Shell "cscript.exe /Nologo " + nombreArchivo
End Sub
Var g = script("notepad.exe") '<< ejecuta este .exe (notepad como demo)
guardaArchivo("script.vbs",g)
ejecutaScript("script.vbs")
Dim As String s
Print "Hola"
Input "Teclee algo: ", s
Print s
Kill "script.vbs"
Sleep
run "someProgram.bas",#handle
render #handle ' this runs the program until it waits
' both the parent and child are running
' --------------------------------------------------------
' You can also call a function in the someProgram.bas program.
' For example if it had a DisplayBanner Funciton.
#handle DisplayBanner("Welcome!")
Module Module1
Sub Fork()
Console.WriteLine("Spawned Thread")
End Sub
Sub Main()
Dim t As New System.Threading.Thread(New Threading.ThreadStart(AddressOf Fork))
t.Start()
Console.WriteLine("Main Thread")
t.Join()
End Sub
End Module
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the Smart BASIC programming language
You may also check:How to resolve the algorithm Steffensen's method step by step in the Python programming language
You may also check:How to resolve the algorithm Text processing/2 step by step in the R programming language
You may also check:How to resolve the algorithm Vampire number step by step in the PARI/GP programming language