├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── argument.go ├── deadlock.go ├── exec_process.go ├── exit_code.go ├── flag.go ├── hello_world.go ├── mkdir.go ├── mkdir_umask.go ├── print_pid.go ├── print_ppid.go ├── process_name.go ├── spawn_process.go ├── stdin.go └── web_server.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.4 2 | MAINTAINER tobe tobeg3oogle@gmail.com 3 | 4 | ADD . /go/src/ 5 | 6 | WORKDIR /go/src/ 7 | 8 | CMD /bin/bash -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 tobe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | understand_linux_process_examples 2 | ================================= 3 | 4 | Examples of Understand Linux Process 5 | 6 | 7 | https://registry.hub.docker.com/u/tobegit3hub/understand_linux_process_examp/ -------------------------------------------------------------------------------- /argument.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "os" 4 | import "fmt" 5 | 6 | func main() { 7 | argsWithProg := os.Args 8 | argsWithoutProg := os.Args[1:] 9 | 10 | arg := os.Args[3] 11 | fmt.Println(argsWithProg) 12 | fmt.Println(argsWithoutProg) 13 | fmt.Println(arg) 14 | } 15 | -------------------------------------------------------------------------------- /deadlock.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | ch := make(chan int) 5 | <-ch 6 | } 7 | -------------------------------------------------------------------------------- /exec_process.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "syscall" 4 | import "os" 5 | import "os/exec" 6 | 7 | func main() { 8 | binary, lookErr := exec.LookPath("ls") 9 | if lookErr != nil { 10 | panic(lookErr) 11 | } 12 | 13 | args := []string{"ls", "-a", "-l", "-h"} 14 | env := os.Environ() 15 | execErr := syscall.Exec(binary, args, env) 16 | if execErr != nil { 17 | panic(execErr) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /exit_code.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() { 4 | panic("Call panic()") 5 | } 6 | -------------------------------------------------------------------------------- /flag.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "flag" 4 | import "fmt" 5 | 6 | func main() { 7 | wordPtr := flag.String("word", "foo", "a string") 8 | numbPtr := flag.Int("numb", 42, "an int") 9 | boolPtr := flag.Bool("fork", false, "a bool") 10 | var svar string 11 | flag.StringVar(&svar, "svar", "bar", "a string var") 12 | 13 | flag.Parse() 14 | 15 | fmt.Println("word:", *wordPtr) 16 | fmt.Println("numb:", *numbPtr) 17 | fmt.Println("fork:", *boolPtr) 18 | fmt.Println("svar:", svar) 19 | fmt.Println("tail:", flag.Args()) 20 | } 21 | -------------------------------------------------------------------------------- /hello_world.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Println("Hello World") 9 | } 10 | -------------------------------------------------------------------------------- /mkdir.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func main() { 9 | err := os.MkdirAll("/tmp/gotest/", 0777) 10 | if err != nil { 11 | panic(err) 12 | } 13 | 14 | fmt.Println("Mkdir /tmp/gotest/") 15 | } 16 | -------------------------------------------------------------------------------- /mkdir_umask.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "syscall" 7 | ) 8 | 9 | func main() { 10 | mask := syscall.Umask(0) 11 | defer syscall.Umask(mask) 12 | 13 | err := os.MkdirAll("/tmp/gotest/", 0777) 14 | if err != nil { 15 | panic(err) 16 | } 17 | 18 | fmt.Println("Mkdir /tmp/gotest/") 19 | } 20 | -------------------------------------------------------------------------------- /print_pid.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func main() { 9 | fmt.Println(os.Getpid()) 10 | } 11 | -------------------------------------------------------------------------------- /print_ppid.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func main() { 9 | fmt.Println(os.Getppid()) 10 | } 11 | -------------------------------------------------------------------------------- /process_name.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func main() { 9 | processName := os.Args[0] 10 | 11 | fmt.Println(processName) 12 | } 13 | -------------------------------------------------------------------------------- /spawn_process.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | import "io/ioutil" 5 | import "os/exec" 6 | 7 | func main() { 8 | dateCmd := exec.Command("date") 9 | dateOut, err := dateCmd.Output() 10 | if err != nil { 11 | panic(err) 12 | } 13 | fmt.Println("> date") 14 | fmt.Println(string(dateOut)) 15 | grepCmd := exec.Command("grep", "hello") 16 | grepIn, _ := grepCmd.StdinPipe() 17 | grepOut, _ := grepCmd.StdoutPipe() 18 | grepCmd.Start() 19 | grepIn.Write([]byte("hello grep\ngoodbye grep")) 20 | grepIn.Close() 21 | grepBytes, _ := ioutil.ReadAll(grepOut) 22 | grepCmd.Wait() 23 | fmt.Println("> grep hello") 24 | fmt.Println(string(grepBytes)) 25 | lsCmd := exec.Command("bash", "-c", "ls -a -l -h") 26 | lsOut, err := lsCmd.Output() 27 | if err != nil { 28 | panic(err) 29 | } 30 | fmt.Println("> ls -a -l -h") 31 | fmt.Println(string(lsOut)) 32 | } 33 | -------------------------------------------------------------------------------- /stdin.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | bytes, err := ioutil.ReadAll(os.Stdin) 11 | if err != nil { 12 | panic(err) 13 | } 14 | 15 | fmt.Println(string(bytes)) 16 | } 17 | -------------------------------------------------------------------------------- /web_server.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | ) 7 | 8 | func handler(w http.ResponseWriter, r *http.Request) { 9 | fmt.Println("Handle request") 10 | } 11 | 12 | func main() { 13 | http.HandleFunc("/", handler) 14 | http.ListenAndServe(":8000", nil) 15 | } 16 | --------------------------------------------------------------------------------