當前位置: 首頁>>代碼示例>>Golang>>正文


Golang os.ForkExec函數代碼示例

本文整理匯總了Golang中os.ForkExec函數的典型用法代碼示例。如果您正苦於以下問題:Golang ForkExec函數的具體用法?Golang ForkExec怎麽用?Golang ForkExec使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了ForkExec函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: compile

func compile(w *World) *bytes.Buffer {
	ioutil.WriteFile(TEMPPATH+".go", []byte(w.source()), 0644)

	err := new(bytes.Buffer)

	re, e, _ := os.Pipe()

	os.ForkExec(
		bin+"/"+arch+"g",
		[]string{bin + "/" + arch + "g", "-o", TEMPPATH + ".6", TEMPPATH + ".go"},
		os.Environ(),
		"",
		[]*os.File{nil, e, nil})

	e.Close()
	io.Copy(err, re)

	if err.Len() > 0 {
		return err
	}

	re, e, _ = os.Pipe()
	os.ForkExec(
		bin+"/"+arch+"l",
		[]string{bin + "/" + arch + "l", "-o", TEMPPATH + "", TEMPPATH + ".6"},
		os.Environ(),
		"",
		[]*os.File{nil, e, nil})

	e.Close()
	io.Copy(err, re)

	return err
}
開發者ID:qrush,項目名稱:go-repl,代碼行數:34,代碼來源:main.go

示例2: main

func main() {
	if len(os.Args) < 2 {
		fmt.Printf("Usage: %s <script file>\n", os.Args[0])
		return
	}
	namehash_hash := md5.New()
	io.WriteString(namehash_hash, os.Args[1])
	namehash := hex.EncodeToString(namehash_hash.Sum())
	tempfile := temp_dir() + "/" + namehash

	readpipe, writepipe, _ := os.Pipe()
	stdfiles := [](*os.File){readpipe, os.Stdout, os.Stderr}

	syscall.Umask(0077)
	srcfile, err := os.Open(os.Args[1], os.O_RDONLY, 0)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	srcinfo, _ := srcfile.Stat()
	outfile, err := os.Open(tempfile+".ld", os.O_RDONLY, 0)
	var outinfo (*os.FileInfo)
	if err == nil {
		outinfo, _ = outfile.Stat()
		outfile.Close()
	}

	if err != nil || outinfo.Mtime_ns < srcinfo.Mtime_ns {
		//fmt.Printf("Compiling...")
		cpid, cerr := os.ForkExec(GOBIN+"8g", []string{GOBIN + "8g", "-o", tempfile + ".cd", "/dev/stdin"}, nil, "", stdfiles)

		src := textproto.NewReader(bufio.NewReader(srcfile))
		firstline := true
		for {
			s, err := src.ReadLine()
			if err != nil {
				break
			}
			if (len(s) > 0) && (s[0] == '#') && firstline {
				continue
			}
			firstline = false
			writepipe.WriteString(s + "\n")
		}
		writepipe.Close()
		srcfile.Close()

		stage_check(cpid, cerr, "compile")

		lpid, err := os.ForkExec(GOBIN+"8l", []string{GOBIN + "8l", "-o", tempfile + ".ld", tempfile + ".cd"}, nil, "", stdfiles)
		stage_check(lpid, err, "link")
		os.Chmod(tempfile+".ld", 0700)
		//fmt.Println("done.")
	} else {
		//fmt.Println("Running cached.")
	}
	err = os.Exec(tempfile+".ld", os.Args[1:], nil)
	fmt.Println(err)
	os.Exit(1)
}
開發者ID:stiletto,項目名稱:goscript,代碼行數:60,代碼來源:goscript.go

示例3: git_from_net

func git_from_net(url string) string {
	var args [3]string
	args[0] = "git"
	args[1] = "clone"
	args[2] = url
	var fds []*os.File = new([3]*os.File)
	fds[0] = os.Stdin
	fds[1] = os.Stdout
	fds[2] = os.Stderr

	_, str := path.Split(url)
	name := strings.Split(str, ".", -1)[0]
	var git_path string

	switch os.Getenv("GOOS") {
	case "darwin":
		git_path = "/usr/local/git/bin/git"
		break
	case "linux":
		git_path = "/opt/local/bin/git"
		break
	}

	/* Replace this with git's full path, or use a shell, and then call git in the args */
	pid, err := os.ForkExec(git_path, &args, os.Envs, os.Getenv("GOROOT")+"/src/pkg/", fds)
	if err != nil {
		log.Exit(err)
	}

	os.Wait(pid, 0)

	return string(os.Getenv("GOROOT") + "/src/pkg/" + name)
}
開發者ID:ox,項目名稱:GoStones,代碼行數:33,代碼來源:main.go

示例4: startProc

func startProc(path, args interface{}) interface{} {
	p, ok := path.(string)
	if !ok {
		TypeError("string", path)
	}
	argv := make([]string, ListLen(args))
	for cur, i := args, 0; cur != EMPTY_LIST; cur, i = Cdr(cur), i+1 {
		x := Car(cur)
		s, ok := x.(string)
		if !ok {
			TypeError("string", x)
		}
		argv[i] = s
	}
	inr, inw, err := os.Pipe()
	if err != nil {
		SystemError(err)
	}
	outr, outw, err := os.Pipe()
	if err != nil {
		SystemError(err)
	}
	_, err = os.ForkExec(p, argv, os.Envs, "", []*os.File{inr, outw, os.Stderr})
	if err != nil {
		SystemError(err)
	}
	return Cons(NewOutput(inw), NewInput(outr))
}
開發者ID:ypb,項目名稱:golisp,代碼行數:28,代碼來源:primitives.go

示例5: Keyspace

func Keyspace(servers string) (*KeyspaceProxy, os.Error) {
	serverList := strings.Split(servers, " ", -1)
	argv := make([]string, len(serverList)+1)
	argv[0] = scriptPath
	for idx, server := range serverList {
		argv[idx+1] = server
	}
	stdinRead, stdinWrite, err := os.Pipe()
	if err != nil {
		return nil, err
	}
	stdoutRead, stdoutWrite, err := os.Pipe()
	if err != nil {
		return nil, err
	}
	stderrRead, stderrWrite, err := os.Pipe()
	if err != nil {
		return nil, err
	}
	fd := []*os.File{stdinRead, stdoutWrite, stderrWrite}
	pid, err := os.ForkExec(scriptPath, argv, os.Environ(), "", fd)
	if err != nil {
		return nil, err
	}
	return &KeyspaceProxy{
			Servers: servers,
			pid:     pid,
			stdin:   stdinWrite,
			stdout:  stdoutRead,
			stderr:  stderrRead,
			lock:    &sync.Mutex{},
		},
		nil
}
開發者ID:strogo,項目名稱:ampify,代碼行數:34,代碼來源:pyksproxy.go

示例6: DateServer

// exec a program, redirecting output
func DateServer(c *http.Conn, req *http.Request) {
	c.SetHeader("content-type", "text/plain; charset=utf-8")
	r, w, err := os.Pipe()
	if err != nil {
		fmt.Fprintf(c, "pipe: %s\n", err)
		return
	}
	pid, err := os.ForkExec("/bin/date", []string{"date"}, os.Environ(), "", []*os.File{nil, w, w})
	defer r.Close()
	w.Close()
	if err != nil {
		fmt.Fprintf(c, "fork/exec: %s\n", err)
		return
	}
	io.Copy(c, r)
	wait, err := os.Wait(pid, 0)
	if err != nil {
		fmt.Fprintf(c, "wait: %s\n", err)
		return
	}
	if !wait.Exited() || wait.ExitStatus() != 0 {
		fmt.Fprintf(c, "date: %v\n", wait)
		return
	}
}
開發者ID:8l,項目名稱:go-learn,代碼行數:26,代碼來源:triv.go

示例7: run

func run() (*bytes.Buffer, *bytes.Buffer) {
	out := new(bytes.Buffer)
	err := new(bytes.Buffer)

	re, e, _ := os.Pipe()
	ro, o, _ := os.Pipe()
	os.ForkExec(
		TEMPPATH,
		[]string{TEMPPATH},
		os.Environ(),
		"",
		[]*os.File{nil, o, e})

	e.Close()
	io.Copy(err, re)

	if err.Len() > 0 {
		return nil, err
	}

	o.Close()
	io.Copy(out, ro)

	return out, err
}
開發者ID:qrush,項目名稱:go-repl,代碼行數:25,代碼來源:main.go

示例8: GetOutput

// Return the output from running the given command
func GetOutput(args []string) (output string, error os.Error) {
	read_pipe, write_pipe, err := os.Pipe()
	if err != nil {
		goto Error
	}
	defer read_pipe.Close()
	pid, err := os.ForkExec(args[0], args, os.Environ(), ".", []*os.File{nil, write_pipe, nil})
	if err != nil {
		write_pipe.Close()
		goto Error
	}
	_, err = os.Wait(pid, 0)
	write_pipe.Close()
	if err != nil {
		goto Error
	}
	buffer := &bytes.Buffer{}
	_, err = io.Copy(buffer, read_pipe)
	if err != nil {
		goto Error
	}
	output = buffer.String()
	return output, nil
Error:
	return "", &CommandError{args[0], args}
}
開發者ID:strogo,項目名稱:ampify,代碼行數:27,代碼來源:command.go

示例9: SpawnMonitor

func SpawnMonitor(argv0 string, argv []string, envv []string, dir string, fd []*File) os.Error {
	pid,err: = os.ForkExec(argv0, argv, envv, dir, fd) (pid int, err Error)
	if err != nil {
		return err
	}
	for ;os.Kill(pid, 0) == 0;{
		_=time.Sleep(10000)
	}
}
開發者ID:jessta,項目名稱:jinit,代碼行數:9,代碼來源:init.go

示例10: runSystemCommand

func runSystemCommand(argv []string, dir string) string {
	lookedPath, _ := exec.LookPath(argv[0])
	r, w, _ := os.Pipe()
	pid, _ := os.ForkExec(lookedPath, argv, nil, dir, []*os.File{nil, w, w})
	w.Close()
	os.Wait(pid, 0)
	var b bytes.Buffer
	io.Copy(&b, r)
	return b.String()
}
開發者ID:cc-syncsuite,項目名稱:piggydemon,代碼行數:10,代碼來源:piggydemon.go

示例11: Run

// Run starts the named binary running with
// arguments argv and environment envv.
// It returns a pointer to a new Cmd representing
// the command or an error.
//
// The parameters stdin, stdout, and stderr
// specify how to handle standard input, output, and error.
// The choices are DevNull (connect to /dev/null),
// PassThrough (connect to the current process's standard stream),
// Pipe (connect to an operating system pipe), and
// MergeWithStdout (only for standard error; use the same
// file descriptor as was used for standard output).
// If a parameter is Pipe, then the corresponding field (Stdin, Stdout, Stderr)
// of the returned Cmd is the other end of the pipe.
// Otherwise the field in Cmd is nil.
func Run(name string, argv, envv []string, dir string, stdin, stdout, stderr int) (p *Cmd, err os.Error) {
	p = new(Cmd)
	var fd [3]*os.File

	if fd[0], p.Stdin, err = modeToFiles(stdin, 0); err != nil {
		goto Error
	}
	if fd[1], p.Stdout, err = modeToFiles(stdout, 1); err != nil {
		goto Error
	}
	if stderr == MergeWithStdout {
		fd[2] = fd[1]
	} else if fd[2], p.Stderr, err = modeToFiles(stderr, 2); err != nil {
		goto Error
	}

	// Run command.
	p.Pid, err = os.ForkExec(name, argv, envv, dir, fd[0:])
	if err != nil {
		goto Error
	}
	if fd[0] != os.Stdin {
		fd[0].Close()
	}
	if fd[1] != os.Stdout {
		fd[1].Close()
	}
	if fd[2] != os.Stderr && fd[2] != fd[1] {
		fd[2].Close()
	}
	return p, nil

Error:
	if fd[0] != os.Stdin && fd[0] != nil {
		fd[0].Close()
	}
	if fd[1] != os.Stdout && fd[1] != nil {
		fd[1].Close()
	}
	if fd[2] != os.Stderr && fd[2] != nil && fd[2] != fd[1] {
		fd[2].Close()
	}
	if p.Stdin != nil {
		p.Stdin.Close()
	}
	if p.Stdout != nil {
		p.Stdout.Close()
	}
	if p.Stderr != nil {
		p.Stderr.Close()
	}
	return nil, err
}
開發者ID:IntegerCompany,項目名稱:linaro-android-gcc,代碼行數:68,代碼來源:exec.go

示例12: tryRunServer

func tryRunServer() os.Error {
	path, err := exec.LookPath("gocode")
	if err != nil {
		return err
	}

	args := []string{"gocode", "-s", "-sock", *sock, "-addr", *addr}
	_, err = os.ForkExec(path, args, os.Environ(), "", []*os.File{nil, nil, nil})
	if err != nil {
		return err
	}
	return nil
}
開發者ID:elazarl,項目名稱:gocode,代碼行數:13,代碼來源:gocode.go

示例13: UnmountFuse

func UnmountFuse(mountpoint string) (err os.Error) {
	var pid int
	fmt.Println("Unmounting", mountpoint, "...")
	for i := 0; i < len(paths); i++ {
		var args []string = []string{paths[i], "-u", mountpoint}
		pid, err = os.ForkExec(paths[i], args, []string{}, "",
			[]*os.File{os.Stdin, os.Stdout, os.Stderr})
		if err == nil {
			os.Wait(pid, 0)
			return err
		}
	}
	return err
}
開發者ID:droundy,項目名稱:myfs,代碼行數:14,代碼來源:main.go

示例14: run

// run runs the command argv, feeding in stdin on standard input.
// It returns the output to standard output and standard error.
// ok indicates whether the command exited successfully.
func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
	cmd, err := exec.LookPath(argv[0]);
	if err != nil {
		fatal("exec %s: %s", argv[0], err);
	}
	r0, w0, err := os.Pipe();
	if err != nil {
		fatal("%s", err);
	}
	r1, w1, err := os.Pipe();
	if err != nil {
		fatal("%s", err);
	}
	r2, w2, err := os.Pipe();
	if err != nil {
		fatal("%s", err);
	}
	pid, err := os.ForkExec(cmd, argv, os.Environ(), "", []*os.File{r0, w1, w2});
	if err != nil {
		fatal("%s", err);
	}
	r0.Close();
	w1.Close();
	w2.Close();
	c := make(chan bool);
	go func() {
		w0.Write(stdin);
		w0.Close();
		c <- true;
	}();
	var xstdout []byte;	// TODO(rsc): delete after 6g can take address of out parameter
	go func() {
		xstdout, _ = io.ReadAll(r1);
		r1.Close();
		c <- true;
	}();
	stderr, _ = io.ReadAll(r2);
	r2.Close();
	<-c;
	<-c;
	stdout = xstdout;

	w, err := os.Wait(pid, 0);
	if err != nil {
		fatal("%s", err);
	}
	ok = w.Exited() && w.ExitStatus() == 0;
	return;
}
開發者ID:8l,項目名稱:go-learn,代碼行數:52,代碼來源:util.go

示例15: run

// run runs the command argv, feeding in stdin on standard input.
// It returns the output to standard output and standard error.
// ok indicates whether the command exited successfully.
func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
	cmd, err := exec.LookPath(argv[0])
	if err != nil {
		fatal("exec %s: %s", argv[0], err)
	}
	r0, w0, err := os.Pipe()
	if err != nil {
		fatal("%s", err)
	}
	r1, w1, err := os.Pipe()
	if err != nil {
		fatal("%s", err)
	}
	r2, w2, err := os.Pipe()
	if err != nil {
		fatal("%s", err)
	}
	pid, err := os.ForkExec(cmd, argv, os.Environ(), "", []*os.File{r0, w1, w2})
	if err != nil {
		fatal("%s", err)
	}
	r0.Close()
	w1.Close()
	w2.Close()
	c := make(chan bool)
	go func() {
		w0.Write(stdin)
		w0.Close()
		c <- true
	}()
	var xstdout []byte // TODO(rsc): delete after 6g can take address of out parameter
	go func() {
		xstdout, _ = ioutil.ReadAll(r1)
		r1.Close()
		c <- true
	}()
	stderr, _ = ioutil.ReadAll(r2)
	r2.Close()
	<-c
	<-c
	stdout = xstdout

	w, err := os.Wait(pid, 0)
	if err != nil {
		fatal("%s", err)
	}
	ok = w.Exited() && w.ExitStatus() == 0
	return
}
開發者ID:ivanwyc,項目名稱:google-go,代碼行數:52,代碼來源:util.go


注:本文中的os.ForkExec函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。