Skip to content

Instantly share code, notes, and snippets.

@alban
Created May 20, 2015 10:35
Show Gist options
  • Save alban/257989967eb38aa6c849 to your computer and use it in GitHub Desktop.
Save alban/257989967eb38aa6c849 to your computer and use it in GitHub Desktop.
testing unshare CLONE_FS in go routines
package main
import "fmt"
import "os"
import "time"
import "syscall"
import "runtime"
func thr() {
runtime.LockOSThread()
runtime.Gosched()
err := syscall.Unshare(int(0x00000200)) // CLONE_FS
if err != nil {
fmt.Printf("error unsharing CLONE_FS: %v", err)
}
dir := fmt.Sprintf("/tmp/go-clone-test/thr-%d-%d", syscall.Getpid(), syscall.Gettid())
os.MkdirAll(dir, 0755)
if err != nil {
fmt.Printf("error mkdir: %v", err)
return
}
err = os.Chdir(dir)
if err != nil {
fmt.Printf("error chdir: %v", err)
return
}
cwd, _ := os.Getwd()
fmt.Printf("in go routine: pid=%d tid=%d cwd=%s\n", syscall.Getpid(), syscall.Gettid(), cwd)
}
func main() {
cwd, _ := os.Getwd()
fmt.Printf("BEFORE: in main(): pid=%d tid=%d cwd=%s\n", syscall.Getpid(), syscall.Gettid(), cwd)
var i int
for i = 0; i < 3; i++ {
go thr()
time.Sleep(10 * time.Millisecond)
}
time.Sleep(1000 * time.Millisecond)
cwd, _ = os.Getwd()
fmt.Printf("AFTER: in main(): pid=%d tid=%d cwd=%s\n", syscall.Getpid(), syscall.Gettid(), cwd)
// The output is:
//
//BEFORE: in main(): pid=12777 tid=12777 cwd=/home/alban/tmp/test
//in go routine: pid=12777 tid=12777 cwd=/tmp/go-clone-test/thr-12777-12777
//in go routine: pid=12777 tid=12779 cwd=/tmp/go-clone-test/thr-12777-12779
//in go routine: pid=12777 tid=12777 cwd=/tmp/go-clone-test/thr-12777-12777
//AFTER: in main(): pid=12777 tid=12779 cwd=/tmp/go-clone-test/thr-12777-12779
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment