本文整理汇总了Golang中syscall.Pipe函数的典型用法代码示例。如果您正苦于以下问题:Golang Pipe函数的具体用法?Golang Pipe怎么用?Golang Pipe使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Pipe函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: epollinit
// epollinit opens an epoll file descriptor and creates a pipe which will be
// used to wake up the epoll_wait(2) function. Then, file descriptor associated
// with inotify event queue and the read end of the pipe are added to epoll set.
// Note that `fd` member must be set before this function is called.
func (i *inotify) epollinit() (err error) {
if i.epfd, err = syscall.EpollCreate1(0); err != nil {
return
}
if err = syscall.Pipe(i.pipefd); err != nil {
return
}
i.epes = []syscall.EpollEvent{
{Events: syscall.EPOLLIN, Fd: i.fd},
{Events: syscall.EPOLLIN, Fd: int32(i.pipefd[0])},
}
if err = syscall.EpollCtl(i.epfd, syscall.EPOLL_CTL_ADD, int(i.fd), &i.epes[0]); err != nil {
return
}
return syscall.EpollCtl(i.epfd, syscall.EPOLL_CTL_ADD, i.pipefd[0], &i.epes[1])
}
示例2: TestSelect
func TestSelect(t *testing.T) {
var p1, p2 [2]int
mustNil(syscall.Pipe(p1[:]))
mustNil(syscall.Pipe(p2[:]))
fs := NewFdSet(p1[0], p2[0])
var maxfd int
if p1[0] > p2[0] {
maxfd = p1[0] + 1
} else {
maxfd = p2[0] + 1
}
go func() {
syscall.Write(p1[1], []byte("to p1"))
syscall.Write(p2[1], []byte("to p2"))
syscall.Close(p1[1])
syscall.Close(p2[1])
}()
e := Select(maxfd+1, fs, nil, nil, nil)
if e != nil {
t.Errorf("Select(%v, %v, nil, nil, nil) => %v, want <nil>",
maxfd+1, fs, e)
}
syscall.Close(p1[0])
syscall.Close(p2[0])
}
示例3: Init
func (sc *selectCtx) Init() error {
sc.rset.Zero()
sc.wset.Zero()
sc.fdmax = -1
// Create and configure the pipe-ends.
b := make([]int, 2)
err := syscall.Pipe(b)
if err != nil {
return err
}
sc.pfdr, sc.pfdw = b[0], b[1]
syscall.CloseOnExec(sc.pfdr)
syscall.CloseOnExec(sc.pfdw)
err = syscall.SetNonblock(sc.pfdr, true)
if err != nil {
syscall.Close(sc.pfdr)
syscall.Close(sc.pfdw)
return err
}
err = syscall.SetNonblock(sc.pfdw, true)
if err != nil {
syscall.Close(sc.pfdr)
syscall.Close(sc.pfdw)
return err
}
// pfdr (read-end of pipe) is set (and remains set forever) on
// rset.
sc.rset.Set(sc.pfdr)
sc.fdmax = sc.pfdr
// allocate dummy selectCtx.{b,b1} buffers.
sc.b = make([]byte, 1)
sc.b1 = make([]byte, 128)
return nil
}
示例4: NewGTKUI
func NewGTKUI() *GTKUI {
gtk.Init(nil)
window := gtk.Window(gtk.GTK_WINDOW_TOPLEVEL)
window.SetPosition(gtk.GTK_WIN_POS_CENTER)
window.SetTitle("Pond")
window.SetDefaultSize(1000, 800)
ui := >KUI{
window: window,
actions: make(chan interface{}, uiActionsQueueLen),
events: make(chan interface{}, 8),
}
window.Connect("destroy", func(ctx *glib.CallbackContext) {
close(ui.events)
for {
if _, ok := <-ui.actions; !ok {
break
}
}
gtk.MainQuit()
})
if err := syscall.Pipe(ui.pipe[:]); err != nil {
panic(err)
}
syscall.SetNonblock(ui.pipe[0], true)
glib.FdWatchAdd(ui.pipe[0], glib.IOIn, func(conditions int) bool {
ui.onAction()
return true
})
return ui
}
示例5: main
func main() {
fd := make([]int, 2)
if err := syscall.Pipe(fd); err != nil {
log.Fatal("Socketpair:", err)
}
defer syscall.Close(fd[0])
defer syscall.Close(fd[1])
name := fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), fd[1])
fmt.Println(name)
cmd := exec.Command("ubertooth-btle", "-f", "-d", name)
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
f := os.NewFile(uintptr(fd[0]), "server")
buf := make([]byte, 1024)
for {
n, err := f.Read(buf)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("%q\n", buf[:n])
}
}
示例6: makeTestFd
func makeTestFd(t *testing.T) testFd {
var tfd testFd
errno := syscall.Pipe(tfd[:])
if errno != nil {
t.Fatalf("Failed to create pipe: %v", errno)
}
return tfd
}
示例7: Pipe
// Pipe returns a connected pair of Files; reads from r return bytes
// written to w. It returns the files and an error, if any.
func Pipe() (r *File, w *File, err error) {
var p [2]int
if e := syscall.Pipe(p[0:]); e != nil {
return nil, nil, NewSyscallError("pipe", e)
}
return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
}
示例8: forkExecPipe
// Try to open a pipe with O_CLOEXEC set on both file descriptors.
func forkExecPipe(p []int) error {
err := syscall.Pipe(p)
if err != nil {
return err
}
_, err = fcntl(p[0], syscall.F_SETFD, syscall.FD_CLOEXEC)
if err != nil {
return err
}
_, err = fcntl(p[1], syscall.F_SETFD, syscall.FD_CLOEXEC)
return err
}
示例9: Pipe
// Pipe returns a connected pair of Files; reads from r return bytes
// written to w. It returns the files and an error, if any.
func Pipe() (r *File, w *File, err error) {
var p [2]int
syscall.ForkLock.RLock()
if e := syscall.Pipe(p[0:]); e != nil {
syscall.ForkLock.RUnlock()
return nil, nil, NewSyscallError("pipe", e)
}
syscall.ForkLock.RUnlock()
return NewFile(uintptr(p[0]), "|0"), NewFile(uintptr(p[1]), "|1"), nil
}
示例10: Pipe
func Pipe() (r *File, w *File, err Error) {
var p [2]int
syscall.ForkLock.RLock()
if e := syscall.Pipe(p[0:]); iserror(e) {
syscall.ForkLock.RUnlock()
return nil, nil, NewSyscallError("pipe", e)
}
syscall.ForkLock.RUnlock()
return NewFile(p[0], "|0"), NewFile(p[1], "|1"), nil
}
示例11: launchQemu
// launchQemu run qemu and wait it's quit, includes
func launchQemu(ctx *VmContext) {
qemu, err := exec.LookPath("qemu-system-x86_64")
if err != nil {
ctx.hub <- &QemuExitEvent{message: "can not find qemu executable"}
return
}
args := ctx.QemuArguments()
if glog.V(1) {
glog.Info("cmdline arguments: ", strings.Join(args, " "))
}
go waitConsoleOutput(ctx)
pipe := make([]int, 2)
err = syscall.Pipe(pipe)
if err != nil {
glog.Error("fail to create pipe")
ctx.hub <- &QemuExitEvent{message: "fail to create pipe"}
return
}
err = daemon(qemu, append([]string{"qemu-system-x86_64"}, args...), pipe[1])
if err != nil {
//fail to daemonize
glog.Error("try to start qemu failed")
ctx.hub <- &QemuExitEvent{message: "try to start qemu failed"}
return
}
buf := make([]byte, 4)
nr, err := syscall.Read(pipe[0], buf)
if err != nil || nr != 4 {
glog.Error("try to start qemu failed")
ctx.hub <- &QemuExitEvent{message: "try to start qemu failed"}
return
}
syscall.Close(pipe[1])
syscall.Close(pipe[0])
pid := binary.BigEndian.Uint32(buf[:nr])
glog.V(1).Infof("starting daemon with pid: %d", pid)
err = ctx.watchPid(int(pid))
if err != nil {
glog.Error("watch qemu process failed")
ctx.hub <- &QemuExitEvent{message: "watch qemu process failed"}
return
}
}
示例12: splice
func (s *Splicer) splice(direction string, inFD, outFD int, wg *sync.WaitGroup) {
// Signal to the caller that we're done
defer func() {
// If a reliable delivery socket has data associated with it when a close takes place, the system continues to attempt data transfer.
if err := syscall.Shutdown(inFD, SHUT_RDWR); err != nil {
log.Printf("Shutdown err %v", err)
}
if err := syscall.Shutdown(outFD, SHUT_RDWR); err != nil {
log.Printf("Shutdown err %v", err)
}
if wg != nil {
wg.Done()
}
}()
pipe := make([]int, 2)
if err := syscall.Pipe(pipe); err != nil {
log.Fatal(err)
}
defer func() {
syscall.Close(pipe[0])
syscall.Close(pipe[1])
}()
var netWrittenBytes, netReadBytes int64
log.Printf("[%v] Splicing pipe %+v, tcpfds %+v", direction, s.pipe, s.tcpFD)
for {
// SPLICE_F_NONBLOCK: don't block if TCP buffer is empty
// SPLICE_F_MOVE: directly move pages into splice buffer in kernel memory
// SPLICE_F_MORE: just makes mysql connections slow
log.Printf("[input (%v)] Entering input", direction)
inBytes, err := syscall.Splice(inFD, nil, pipe[1], nil, s.bufferSize, SPLICE_F_NONBLOCK)
if err := s.checkSpliceErr(inBytes, err, fmt.Sprintf("input (%v)", direction)); err != nil {
log.Printf("ERROR [input (%v)] error: %v", direction, err)
return
}
netReadBytes += inBytes
log.Printf("[input (%v)] %d bytes read", direction, inBytes)
log.Printf("[input (%v)] Entering output", direction)
outBytes, err := syscall.Splice(pipe[0], nil, outFD, nil, s.bufferSize, SPLICE_F_NONBLOCK)
if err := s.checkSpliceErr(inBytes, err, fmt.Sprintf("output (%v)", direction)); err != nil {
log.Printf("ERROR [output (%v)] error: %v", direction, err)
return
}
log.Printf("[output (%v)] %d bytes written, out of given input %d", direction, outBytes, inBytes)
netWrittenBytes += outBytes
}
log.Printf("[%v] Spliced %d bytes read %d bytes written", direction, netWrittenBytes, netReadBytes)
}
示例13: init
// init initializes kqueue.
func (k *kqueue) init() (err error) {
if k.fd, err = syscall.Kqueue(); err != nil {
return
}
// Creates pipe used to stop `Kevent` call by registering it,
// watching read end and writing to other end of it.
if err = syscall.Pipe(k.pipefds[:]); err != nil {
return
}
var kevn [1]syscall.Kevent_t
syscall.SetKevent(&kevn[0], k.pipefds[0], syscall.EVFILT_READ, syscall.EV_ADD)
_, err = syscall.Kevent(k.fd, kevn[:], nil, nil)
return
}
示例14: launchQemu
// launchQemu run qemu and wait it's quit, includes
func launchQemu(qc *QemuContext, ctx *hypervisor.VmContext) {
qemu := qc.driver.executable
if qemu == "" {
ctx.Hub <- &hypervisor.VmStartFailEvent{Message: "can not find qemu executable"}
return
}
args := qc.arguments(ctx)
if glog.V(1) {
glog.Info("cmdline arguments: ", strings.Join(args, " "))
}
pipe := make([]int, 2)
err := syscall.Pipe(pipe)
if err != nil {
glog.Error("fail to create pipe")
ctx.Hub <- &hypervisor.VmStartFailEvent{Message: "fail to create pipe"}
return
}
err = daemon(qemu, append([]string{"qemu-system-x86_64"}, args...), pipe[1])
if err != nil {
//fail to daemonize
glog.Error("try to start qemu failed")
ctx.Hub <- &hypervisor.VmStartFailEvent{Message: "try to start qemu failed"}
return
}
buf := make([]byte, 4)
nr, err := syscall.Read(pipe[0], buf)
if err != nil || nr != 4 {
glog.Error("try to start qemu failed")
ctx.Hub <- &hypervisor.VmStartFailEvent{Message: "try to start qemu failed"}
return
}
syscall.Close(pipe[1])
syscall.Close(pipe[0])
pid := binary.BigEndian.Uint32(buf[:nr])
glog.V(1).Infof("starting daemon with pid: %d", pid)
err = ctx.DCtx.(*QemuContext).watchPid(int(pid), ctx.Hub)
if err != nil {
glog.Error("watch qemu process failed")
ctx.Hub <- &hypervisor.VmStartFailEvent{Message: "watch qemu process failed"}
return
}
}
示例15:
func ext۰os۰Pipe(fr *frame, args []value) value {
// func os.Pipe() (r *File, w *File, err error)
// The portable POSIX pipe(2) call is good enough for our needs.
var p [2]int
if err := syscall.Pipe(p[:]); err != nil {
// TODO(adonovan): fix: return an *os.SyscallError.
return tuple{nil, nil, wrapError(err)}
}
NewFile := fr.i.prog.ImportedPackage("os").Func("NewFile")
r := call(fr.i, fr, 0, NewFile, []value{uintptr(p[0]), "|0"})
w := call(fr.i, fr, 0, NewFile, []value{uintptr(p[1]), "|1"})
return tuple{r, w, wrapError(nil)}
}