本文整理汇总了Golang中syscall.Getpid函数的典型用法代码示例。如果您正苦于以下问题:Golang Getpid函数的具体用法?Golang Getpid怎么用?Golang Getpid使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Getpid函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestCurrentDir
func TestCurrentDir(t *testing.T) {
pids := ListPids()
if len(pids) < 1 {
t.Error("Unable to list process IDs")
}
foundCurrent := false
for _, pid := range pids {
proc, err := GetProcInfo(pid)
if err == nil {
if len(proc.CurrentDir) == 0 {
t.Errorf("GetProcInfo returned no error for %d but failed to read current dir", pid)
}
}
if pid == syscall.Getpid() {
foundCurrent = true
currentDir, _ := os.Getwd()
if currentDir != proc.CurrentDir {
t.Errorf("expected working dir for current process %s, got %s", currentDir, proc.CurrentDir)
}
}
}
if !foundCurrent {
t.Errorf("Current process %d not among listed processes", syscall.Getpid())
}
}
示例2: TestSignal
// Test that basic signal handling works.
func TestSignal(t *testing.T) {
// Ask for hangup
c := make(chan os.Signal, 1)
Notify(c, syscall.Note("hangup"))
defer Stop(c)
// Send this process a hangup
t.Logf("hangup...")
postNote(syscall.Getpid(), "hangup")
waitSig(t, c, syscall.Note("hangup"))
// Ask for everything we can get.
c1 := make(chan os.Signal, 1)
Notify(c1)
// Send this process an alarm
t.Logf("alarm...")
postNote(syscall.Getpid(), "alarm")
waitSig(t, c1, syscall.Note("alarm"))
// Send two more hangups, to make sure that
// they get delivered on c1 and that not reading
// from c does not block everything.
t.Logf("hangup...")
postNote(syscall.Getpid(), "hangup")
waitSig(t, c1, syscall.Note("hangup"))
t.Logf("hangup...")
postNote(syscall.Getpid(), "hangup")
waitSig(t, c1, syscall.Note("hangup"))
// The first SIGHUP should be waiting for us on c.
waitSig(t, c, syscall.Note("hangup"))
}
示例3: TestSighupHandler
func TestSighupHandler(t *testing.T) {
ranHandler := make(chan bool, 1)
handler := func(d daemon.Daemon) {
ranHandler <- true
}
conf, _ := config.New("example.yaml")
d, _ := daemon.New(conf)
setupSighupHandler(d, handler)
// Need to sleep here so that the goroutine has time to set up the signal listener, otherwise
// the signal gets missed
time.Sleep(1 * time.Second)
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
// Give the syscall 1 second to be handled, should be more than enough time
timeout := make(chan bool, 1)
go func() {
time.Sleep(time.Duration(1 * time.Second))
timeout <- true
}()
select {
case <-ranHandler:
case <-timeout:
t.Fatal("Didn't run handler")
}
// Try calling again and make sure it still happens
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
select {
case <-ranHandler:
case <-timeout:
t.Fatal("Didn't run handler second time")
}
}
示例4: TestSignal
// Test that basic signal handling works.
func TestSignal(t *testing.T) {
// Ask for SIGHUP
c := make(chan os.Signal, 1)
Notify(c, syscall.SIGHUP)
defer Stop(c)
// Send this process a SIGHUP
t.Logf("sighup...")
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
waitSig(t, c, syscall.SIGHUP)
// Ask for everything we can get.
c1 := make(chan os.Signal, 1)
Notify(c1)
// Send this process a SIGWINCH
t.Logf("sigwinch...")
syscall.Kill(syscall.Getpid(), syscall.SIGWINCH)
waitSig(t, c1, syscall.SIGWINCH)
// Send two more SIGHUPs, to make sure that
// they get delivered on c1 and that not reading
// from c does not block everything.
t.Logf("sighup...")
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
waitSig(t, c1, syscall.SIGHUP)
t.Logf("sighup...")
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
waitSig(t, c1, syscall.SIGHUP)
// The first SIGHUP should be waiting for us on c.
waitSig(t, c, syscall.SIGHUP)
}
示例5: main
func main() {
var i int
fmt.Scanf("%d", &i)
if i == 0 {
fmt.Println(syscall.Getpid())
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
}
fmt.Println("-----------")
}
示例6: Serve
/*
Serve accepts incoming HTTP connections on the listener l, creating a new
service goroutine for each. The service goroutines read requests and then call
handler to reply to them. Handler is typically nil, in which case the
DefaultServeMux is used.
In addition to the stl Serve behaviour each connection is added to a
sync.Waitgroup so that all outstanding connections can be served before shutting
down the server.
*/
func (srv *endlessServer) Serve() (err error) {
defer log.Println(syscall.Getpid(), "Serve() returning...")
srv.state = STATE_RUNNING
err = srv.Server.Serve(srv.EndlessListener)
log.Println(syscall.Getpid(), "Waiting for connections to finish...")
srv.wg.Wait()
srv.state = STATE_TERMINATE
return
}
示例7: start
func start(c *cli.Context) {
if pidfile != "" {
pid, err := fileOrPid(pidfile)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
if pid != 0 && pid != syscall.Getppid() {
fmt.Fprintf(os.Stderr, "There is already a pidfile at '%s' that appears to belong to another apiplexy instance.")
os.Exit(1)
}
}
if configPath == "" {
configPath = c.String("config")
}
ap, config, err := initApiplex(configPath)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
server := &http.Server{
Addr: "0.0.0.0:" + strconv.Itoa(config.Serve.Port),
Handler: ap,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 16,
}
fmt.Printf("Launching apiplexy on port %d.\n", config.Serve.Port)
l, err := net.Listen("tcp", server.Addr)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't start server on %s: %s\n", server.Addr, err.Error())
os.Exit(1)
}
// write pidfile and wait for restart signal
if pidfile != "" {
ioutil.WriteFile(pidfile, []byte(strconv.Itoa(syscall.Getpid())), 0600)
}
defer func() {
// server shuts down, delete pidfile if it's still our PID in there
if pidfile != "" {
rp, _ := ioutil.ReadFile(pidfile)
p, _ := strconv.Atoi(string(rp))
if p == syscall.Getpid() {
os.Remove(pidfile)
}
}
}()
server.Serve(l)
}
示例8: Test_Handle_With2_SIGHUP_ExecutesFnTwice
func Test_Handle_With2_SIGHUP_ExecutesFnTwice(t *testing.T) {
done := make(chan bool, 1)
signal.Handle(func() {
done <- true
})
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
receiveOnce(t, done)
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
receiveOnce(t, done)
}
示例9: Serve
/*
Serve accepts incoming HTTP connections on the listener l, creating a new
service goroutine for each. The service goroutines read requests and then call
handler to reply to them. Handler is typically nil, in which case the
DefaultServeMux is used.
In addition to the stl Serve behaviour each connection is added to a
sync.Waitgroup so that all outstanding connections can be served before shutting
down the server.
*/
func (srv *endlessServer) Serve() (err error) {
defer log.Println(syscall.Getpid(), "Serve() returning...")
srv.setState(STATE_RUNNING)
err = srv.Server.Serve(srv.EndlessListener)
log.Println(syscall.Getpid(), "Waiting for connections to finish...")
srv.waitTimeout(2 * time.Second)
log.Println(syscall.Getpid(), "Connections termiated")
srv.setState(STATE_TERMINATE)
return
}
示例10: testCancel
func testCancel(t *testing.T, ignore bool) {
// Send SIGWINCH. By default this signal should be ignored.
syscall.Kill(syscall.Getpid(), syscall.SIGWINCH)
time.Sleep(100 * time.Millisecond)
// Ask to be notified on c1 when a SIGWINCH is received.
c1 := make(chan os.Signal, 1)
Notify(c1, syscall.SIGWINCH)
defer Stop(c1)
// Ask to be notified on c2 when a SIGHUP is received.
c2 := make(chan os.Signal, 1)
Notify(c2, syscall.SIGHUP)
defer Stop(c2)
// Send this process a SIGWINCH and wait for notification on c1.
syscall.Kill(syscall.Getpid(), syscall.SIGWINCH)
waitSig(t, c1, syscall.SIGWINCH)
// Send this process a SIGHUP and wait for notification on c2.
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
waitSig(t, c2, syscall.SIGHUP)
// Ignore, or reset the signal handlers for, SIGWINCH and SIGHUP.
if ignore {
Ignore(syscall.SIGWINCH, syscall.SIGHUP)
} else {
Reset(syscall.SIGWINCH, syscall.SIGHUP)
}
// Send this process a SIGWINCH. It should be ignored.
syscall.Kill(syscall.Getpid(), syscall.SIGWINCH)
// If ignoring, Send this process a SIGHUP. It should be ignored.
if ignore {
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
}
select {
case s := <-c1:
t.Fatalf("unexpected signal %v", s)
case <-time.After(100 * time.Millisecond):
// nothing to read - good
}
select {
case s := <-c2:
t.Fatalf("unexpected signal %v", s)
case <-time.After(100 * time.Millisecond):
// nothing to read - good
}
// Reset the signal handlers for all signals.
Reset()
}
示例11: acquireLockfile
// Acquire an exclusive lock for the BlobStore in directory dir.
func acquireLockfile(path string) os.Error {
dir, fn := filepath.Split(path)
lockfn := filepath.Join(dir, fn)
lockfile, err := os.OpenFile(lockfn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if e, ok := err.(*os.PathError); ok && e.Error == os.EEXIST {
content, err := ioutil.ReadFile(lockfn)
if err != nil {
return err
}
pid, err := strconv.Atoi(string(content))
if err == nil {
errno := syscall.Kill(pid, 0)
if errno == 0 {
return ErrLocked
}
}
lockfile, err = ioutil.TempFile(dir, "lock")
if err != nil {
return err
}
_, err = lockfile.WriteString(strconv.Itoa(syscall.Getpid()))
if err != nil {
lockfile.Close()
return ErrLockAcquirement
}
curfn := lockfile.Name()
err = lockfile.Close()
if err != nil {
return err
}
err = os.Rename(curfn, lockfn)
if err != nil {
os.Remove(curfn)
return ErrLockAcquirement
}
} else if err != nil {
return err
} else {
_, err = lockfile.WriteString(strconv.Itoa(syscall.Getpid()))
if err != nil {
return err
}
lockfile.Close()
}
return nil
}
示例12: TestStop
// Test that Stop cancels the channel's registrations.
func TestStop(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
sigs := []string{
"alarm",
"hangup",
}
for _, sig := range sigs {
// Send the signal.
// If it's alarm, we should not see it.
// If it's hangup, maybe we'll die. Let the flag tell us what to do.
if sig != "hangup" {
postNote(syscall.Getpid(), sig)
}
time.Sleep(100 * time.Millisecond)
// Ask for signal
c := make(chan os.Signal, 1)
Notify(c, syscall.Note(sig))
defer Stop(c)
// Send this process that signal
postNote(syscall.Getpid(), sig)
waitSig(t, c, syscall.Note(sig))
Stop(c)
select {
case s := <-c:
t.Fatalf("unexpected signal %v", s)
case <-time.After(100 * time.Millisecond):
// nothing to read - good
}
// Send the signal.
// If it's alarm, we should not see it.
// If it's hangup, maybe we'll die. Let the flag tell us what to do.
if sig != "hangup" {
postNote(syscall.Getpid(), sig)
}
select {
case s := <-c:
t.Fatalf("unexpected signal %v", s)
case <-time.After(100 * time.Millisecond):
// nothing to read - good
}
}
}
示例13: Serve
/*
Serve accepts incoming HTTP connections on the listener l, creating a new
service goroutine for each. The service goroutines read requests and then call
handler to reply to them. Handler is typically nil, in which case the
DefaultServeMux is used.
In addition to the stl Serve behaviour each connection is added to a
sync.Waitgroup so that all outstanding connections can be served before shutting
down the server.
*/
func (srv *endlessServer) Serve() (err error) {
defer log.Println(syscall.Getpid(), "Serve() returning...")
srv.state = STATE_RUNNING
err = srv.Server.Serve(srv.EndlessListener)
if opErr, ok := err.(*net.OpError); ok {
// This is hacky, but unfortunately the net package doesn't export errClosing
if strings.Contains(opErr.Error(), "use of closed network connection") {
err = nil
}
}
log.Println(syscall.Getpid(), "Waiting for connections to finish...")
srv.wg.Wait()
srv.state = STATE_TERMINATE
return
}
示例14: TestStop
// Test that Stop cancels the channel's registrations.
func TestStop(t *testing.T) {
sigs := []syscall.Signal{
syscall.SIGWINCH,
syscall.SIGHUP,
syscall.SIGUSR1,
}
for _, sig := range sigs {
// Send the signal.
// If it's SIGWINCH, we should not see it.
// If it's SIGHUP, maybe we'll die. Let the flag tell us what to do.
if sig == syscall.SIGWINCH || (sig == syscall.SIGHUP && *sendUncaughtSighup == 1) {
syscall.Kill(syscall.Getpid(), sig)
}
time.Sleep(100 * time.Millisecond)
// Ask for signal
c := make(chan os.Signal, 1)
Notify(c, sig)
defer Stop(c)
// Send this process that signal
syscall.Kill(syscall.Getpid(), sig)
waitSig(t, c, sig)
Stop(c)
select {
case s := <-c:
t.Fatalf("unexpected signal %v", s)
case <-time.After(100 * time.Millisecond):
// nothing to read - good
}
// Send the signal.
// If it's SIGWINCH, we should not see it.
// If it's SIGHUP, maybe we'll die. Let the flag tell us what to do.
if sig != syscall.SIGHUP || *sendUncaughtSighup == 2 {
syscall.Kill(syscall.Getpid(), sig)
}
select {
case s := <-c:
t.Fatalf("unexpected signal %v", s)
case <-time.After(100 * time.Millisecond):
// nothing to read - good
}
}
}
示例15: main
func main() {
pid := syscall.Getpid()
flag.Parse()
// create the pipeline
pipeline := falcore.NewPipeline()
pipeline.Upstream.PushBack(falcore.NewRequestFilter(Filter))
// create the server with the pipeline
srv := falcore.NewServer(8090, pipeline)
// if passed the socket file descriptor, setup the listener that way
// if you don't have it, the default is to create the socket listener
// with the data passed to falcore.NewServer above (happens in ListenAndServer())
if *socketFd != -1 {
// I know I'm a child process if I get here so I can signal the parent when I'm ready to take over
go childReady(srv)
fmt.Printf("%v Got socket FD: %v\n", pid, *socketFd)
srv.FdListen(*socketFd)
}
// using signals to manage the restart lifecycle
go handleSignals(srv)
// start the server
// this is normally blocking forever unless you send lifecycle commands
fmt.Printf("%v Starting Listener on 8090\n", pid)
if err := srv.ListenAndServe(); err != nil {
fmt.Printf("%v Could not start server: %v", pid, err)
}
fmt.Printf("%v Exiting now\n", pid)
}