Skip to content

Commit

Permalink
ch04 reorganize packages
Browse files Browse the repository at this point in the history
  • Loading branch information
spamegg1 committed Apr 8, 2024
1 parent 692d8ba commit 8c579fa
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 23 deletions.
9 changes: 4 additions & 5 deletions src/main/scala/ch04/01nativeFork.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package ch04.nativeFork
package ch04
package nativeFork

import scalanative.libc.stdlib
import ch04.common
import common.util

@main
def nativeFork(args: String*): Unit =
Expand All @@ -12,9 +11,9 @@ def nativeFork(args: String*): Unit =

println("about to fork")

val status = common.doAndAwait: () =>
val status = doAndAwait: () =>
println(s"in child, about to exec command: ${args.toSeq}")
common.runCommand(args)
runCommand(args)

println(s"wait status ${status}")

Expand Down
7 changes: 3 additions & 4 deletions src/main/scala/ch04/02badExec.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package ch04.badExec

import ch04.common
package ch04
package badExec

@main
def badExec(args: String*): Unit =
println("about to exec")
common.runCommand(Seq("/bin/ls", "-l", "."))
runCommand(Seq("/bin/ls", "-l", "."))
println("exec returned, we're done!")

// It is executed inside the .scala-build folder (does not see hidden .bloop folder).
Expand Down
9 changes: 4 additions & 5 deletions src/main/scala/ch04/03nativePipe.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package ch04.nativePipe
package ch04
package nativePipe

import scalanative.unsafe.stackalloc
import scalanative.posix.unistd // getpid
import collection.mutable.ArrayBuffer
import ch04.common
import common.util

@main
def nativePipe(args: String*): Unit =
Expand All @@ -31,7 +30,7 @@ def pipeMany(input: Int, output: Int, procs: Seq[Seq[String]]): Int =
val procsWithFds = procs.lazyZip(inputFds).lazyZip(outputFds)
val pids =
for (proc, inputFd, outputFd) <- procsWithFds
yield common.doFork: () =>
yield doFork: () =>
// close all pipes that this process won't be using.
for p <- 0 until 2 * (procs.size - 1) do
if pipeArray(p) != inputFd && pipeArray(p) != outputFd then
Expand All @@ -47,7 +46,7 @@ def pipeMany(input: Int, output: Int, procs: Seq[Seq[String]]): Int =
unistd.close(unistd.STDOUT_FILENO)
util.dup2(outputFd, unistd.STDOUT_FILENO)

common.runCommand(proc)
runCommand(proc)

for i <- 0 until 2 * (procs.size - 1) do unistd.close(pipeArray(i))
unistd.close(input)
Expand Down
15 changes: 7 additions & 8 deletions src/main/scala/ch04/04nativePipeTwo.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package ch04.nativePipeTwo
package ch04
package nativePipeTwo

import scalanative.unsafe.{stackalloc, CQuote}
import scalanative.posix.unistd // getpid
import scalanative.libc.{stdio}
import ch04.common
import common.util
import scalanative.libc.stdio

@main
def nativePipeTwo(args: String*): Unit =
Expand All @@ -22,24 +21,24 @@ def runTwoAndPipe(input: Int, output: Int, proc1: Seq[String], proc2: Seq[String
val outputPipe = pipeArray(1)
val inputPipe = pipeArray(0)

val proc1Pid = common.doFork: () =>
val proc1Pid = doFork: () =>
if input != 0 then
println(s"proc ${unistd.getpid()}: about to dup ${input} to stdin")
util.dup2(input, 0)

println(s"proc 1 about to dup ${outputPipe} to stdout")
util.dup2(outputPipe, 1)
stdio.printf(c"process %d about to runCommand\n", unistd.getpid())
common.runCommand(proc1)
runCommand(proc1)

val proc2Pid = common.doFork: () =>
val proc2Pid = doFork: () =>
println(s"proc ${unistd.getpid()}: about to dup")
util.dup2(inputPipe, 0)
if output != 1 then util.dup2(output, 1)

unistd.close(outputPipe)
stdio.printf(c"process %d about to runCommand\n", unistd.getpid())
common.runCommand(proc2)
runCommand(proc2)

unistd.close(input)
unistd.close(outputPipe)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/ch04/common.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch04.common
package ch04

import scalanative.unsigned.{USize, UnsignedRichInt} // .toUSize
import scalanative.unsafe.{CQuote, toCString, Ptr, CString}
Expand Down

0 comments on commit 8c579fa

Please sign in to comment.