MCPcopy Create free account
hub / github.com/LanceLRQ/deer-executor / runAsync

Function runAsync

executor/runtime.go:68–132  ·  view source on GitHub ↗

运行目标程序

(ctx context.Context, session *JudgeSession, rst *commonStructs.TestCaseResult, isChecker bool)

Source from the content-addressed store, hash-verified

66
67// 运行目标程序
68func runAsync(ctx context.Context, session *JudgeSession, rst *commonStructs.TestCaseResult, isChecker bool) (*ProcessInfo, error) {
69 var err error
70
71 runSuccess := make(chan bool, 1)
72 pid := 0
73 pinfo := ProcessInfo{}
74
75 go func() {
76 var pstate *cmd.ProcessState
77 var pArgs *PArgs
78 var proc *cmd.Process
79 // Get process options
80 pArgs, err = getProcessOptions(session, rst, isChecker, false, nil)
81 if err != nil {
82 runSuccess <- false
83 return
84 }
85 // Start process
86 proc, err = cmd.StartProcess(pArgs.Name, pArgs.Args, pArgs.Attr)
87 if err != nil {
88 runSuccess <- false
89 return
90 }
91 // Collect process info
92 pinfo.Process = proc
93 pinfo.Pid = proc.Pid
94 pid = proc.Pid
95 //log.Printf("Start process (%d)...\n", pinfo.Pid)
96 // Wait for exit.
97 pstate, err = proc.Wait()
98 if err != nil {
99 runSuccess <- false
100 return
101 }
102 //log.Printf("Process (%d) exited.\n", pinfo.Pid)
103 pinfo.Status = pstate.Sys().(syscall.WaitStatus)
104 pinfo.Rusage = pstate.SysUsage().(*syscall.Rusage)
105 if pinfo.Rusage == nil {
106 err = errors.Errorf("get rusage failed")
107 runSuccess <- false
108 return
109 }
110 closeFiles(pArgs.Attr.Files)
111 runSuccess <- true
112 }()
113 for {
114 select {
115 case <-runSuccess:
116 goto finish
117 case <-ctx.Done(): // 触发超时
118 log.Println("Child process timeout!")
119 err = errors.Errorf("Child process timeout!")
120 goto doClean
121 }
122 }
123doClean:
124 if pid > 0 {
125 _ = syscall.Kill(pid, syscall.SIGKILL)

Callers 2

runNormalJudgeMethod · 0.85
runSpecialJudgeMethod · 0.85

Calls 8

WaitMethod · 0.95
SysMethod · 0.95
SysUsageMethod · 0.95
StartProcessFunction · 0.92
getProcessOptionsFunction · 0.85
closeFilesFunction · 0.85
ErrorfMethod · 0.80
KillMethod · 0.80

Tested by

no test coverage detected