环境:linux gcc go gdb
如左边代码:
go buildtest.go
./test s
./test c
./test c
可见2个.dat 日志文件都有输出,说明2个父子进程都有accept 成功,进一步说明fork 在golang里面可用,也验证了操作系统的父子进程资源可共享
package main
import (
"fmt"
"io"
"net"
"os"
)
/*
#include <unistd.h>
int MyFork()
{
pid_t p = fork();
return p;
}
*/
import "C"
func handlerCli(cli net.Conn){
for {
buf := make([]byte, 128)
l,err := cli.Read(buf)
if err != nil {
fmt.Println("cli.Readerr")
} else {
buf = buf[:l]
ret,e := cli.Write(buf)
if e != nil || ret != l {
fmt.Println("cli.Writeerr")
}
}
}
}
func main() {
var pid C.int = C.MyFork()
if pid != 0 {
os.Exit(0)
}
if os.Args[1] == "s" {
serv,serr := net.Listen("tcp4", "127.0.0.1:8082")
if serr != nil {
fmt.Println("listenerr")
}
if true {
C.MyFork()
}
fW,_ := os.OpenFile(fmt.Sprintf("%d.dat", os.Getpid()),os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
defer fW.Close()
var clinum int64 = 0
for {
cli,cerr := serv.Accept()
if cerr != nil {
fmt.Println("accepterr")
io.WriteString(fW,"accept err")
continue
}
go handlerCli(cli)
clinum++
//if clinum%100 == 0 {
//fmt.Println(os.Getpid(),":",clinum)
io.WriteString(fW,fmt.Sprintf("%d", clinum))
//}
}
} else {
cli,err := net.Dial("tcp4", "127.0.0.1:8082")
if err != nil {
fmt.Print(err)
return
}
buf := make([]byte, 3)
buf[0]= 'a'
buf[1]= 'b'
buf[2]= 'c'
for {
ret,ew := cli.Write(buf)
if ew != nil {
fmt.Println(ew)
continue
}
if ret != 3 {
fmt.Println("writeless 3")
}
red,errr := cli.Read(buf)
if red != 3 {
fmt.Println("readless 3")
}
if errr != nil {
fmt.Println(errr)
}
}
}
return
}
有疑问加站长微信联系(非本文作者)