This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
PrinzJuliano's profile picture

Multitasking problem

Started by PrinzJuliano, 29 May 2014 - 07:32 PM
PrinzJuliano #1
Posted 29 May 2014 - 09:32 PM
Hello,

I encoured a problem and I tried out a lot but i'm new to multitasking so.

I want to create a "silent" Task which is running separate to the other multitasking stuff.
I looked up in the code of multishell and salvedged some code to create a task called tHack and one for the normal shell:

local parent = term.native()
local w,h = parent.getSize()
local tHack = {}
local running = true;
local tProcess = {}
local function launch()
  --local tProcess = {}
  tProcess.window = window.create(parent, 1, 1, w, h, true)
  tProcess.terminal = tProcess.window
  tProcess.co = coroutine.create( function()
  os.run({["shell"] = shell, ["multishell"] = multishell},
   "/rom/programs/shell")
  --running = false;
  end)
  tHack.window = window.create(parent, 1, 1, w, h, false)
  tHack.co = coroutine.create(function()
	while running do
	end
  end)
end
local function resume(eventD)
  coroutine.resume(tHack.co, unpack(eventD))
  local ok, result = coroutine.resume(tProcess.co, unpack(eventD))
  if ok then
	term.redirect(tProcess.terminal)
	tProcess.terminal = term.current();
  else
	launch()
  end
end
--events
launch()
term.clear()
term.setCursorPos(1,1)
resume({})
while running do
  local eventData = {os.pullEventRaw()}
  local event = eventData[1]
   resume(eventData)
end

I am stuck. The code should hide the tHack coroutine and show the tProcess coroutine. Both should get the event Data from the mail loop.
Thanks for you help.
P.S. yes this may be end up in a hacking software for educational purposes
PrinzJuliano #2
Posted 29 May 2014 - 10:12 PM
Fixed it my self. But i want you to look if i can do something differntly:




--check for rednet
local function modem()
for n,sModem in ipairs(peripheral.getNames()) do
  if peripheral.getType(sModem) == "modem" then
    if not rednet.isOpen(sModem) then
      rednet.open(sModem)
      return true
    end
  end
end
return false
end

local parent = term.native()
local w,h = parent.getSize()
local tHack = {}
local running = true;
local tProcess = {}
local function launch()
  --local tProcess = {}
  tProcess.window = window.create(parent, 1, 1, w, h, true)
  tProcess.terminal = tProcess.window
  tProcess.co = coroutine.create( function()
  os.run({["shell"] = shell, ["multishell"] = multishell},
   "/rom/programs/advanced/multishell")
  running = false;
  end)
  tHack.window = window.create(parent, 1, 1, w, h, false)
  tHack.co = coroutine.create(function()
    if modem() then
    while running do
      e = {os.pullEventRaw()}
      if e[1] == "rednet_message" then
	    --insert malware here
        local id = e[2]
        local msg = e[3]
        rednet.send(id, msg)
      end
    end
    end
  end)
end
local function resume(eventD)
  coroutine.resume(tHack.co, unpack(eventD))
  term.redirect(tProcess.terminal)
  local ok, result = coroutine.resume(tProcess.co, unpack(eventD))
  tProcess.terminal = term.current()
end
--events
launch()
term.clear()
term.setCursorPos(1,1)
resume({})
while running do
  local eventData = {os.pullEventRaw()}
  local event = eventData[1]
  resume(eventData)
end
Wojbie #3
Posted 29 May 2014 - 10:25 PM

	  if e[1] == "rednet_message" then
			--insert malware here
		local id = e[2]
		local msg = e[3]
		rednet.send(id, msg)
	  end

Heh

Now that i think of it why are you over-complicating it that much? Couldn't you have used parallel api?
Edited on 29 May 2014 - 08:27 PM
PrinzJuliano #4
Posted 29 May 2014 - 10:29 PM
No. As I said i wanna learn to use the multitaking system using window API and coroutines. I know you can use parallel.waitForAny in an endless loop…
Wojbie #5
Posted 29 May 2014 - 10:55 PM
Ahh i get you. Still that comment makes me kinda worried :D/>

Anyways why are you creating tHack.window if it never uses it? Plan for future?
CometWolf #6
Posted 29 May 2014 - 10:58 PM
Using coroutines isn't nessacary or even a good option when creating "hacks", and what you've got here is essentially parallel, with some windows thrown in some reason.
PrinzJuliano #7
Posted 29 May 2014 - 11:32 PM
Yep planing to send the tHack window to the controller computer for seeing any executed programs