问题:
我正在 linux 上用 golang 编写程序,该程序需要执行长时间运行的进程,以便:
我正在以 root 权限运行我的程序。
尝试的解决方案:
func Run(pathToBin string, args []string, uid uint32, stdLogFile *os.File) (int, error) {
cmd := exec.Command(pathToBin, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Credential: &syscall.Credential{
Uid: uid,
},
}
cmd.Stdout = stdLogFile
if err := cmd.Start(); err != nil {
return -1, err
}
go func() {
cmd.Wait() //Wait is necessary so cmd doesn't become a zombie
}()
return cmd.Process.Pid, nil
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案似乎满足了我几乎所有的要求,除了当我将 SIGTERM/SIGKILL 发送到我的程序时,底层进程崩溃了。事实上,我希望我的后台进程尽可能独立:它与我的程序有不同的父进程号、组进程号等。我想将它作为守护进程运行。
stackoverflow 上的其他解决方案建议cmd.Process.Release() …
我正在参加我大学的操作系统课程,我们给出的任务之一是使用mmap实现简单的malloc.现在,我得到它的工作,我试图使用valgrind来检测任何错误.无论是否释放内存,valgrind都没有看到任何内存泄漏.作为示例,请考虑以下C代码:
int main()
{
int psize = getpagesize(),i;
int *ptr = mmap(NULL, psize, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
for(i = 0; i < psize/4; i++) ptr[i] = i;
for(i = 0; i < psize/4; i++) printf("%d\n", ptr[i]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
让我们用gcc编译它,并使用valgrind.这是valgrind返回的内容:
==17841== Memcheck, a memory error detector
==17841== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==17841== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==17841== Command: ./test
==17841==
------------ printing numbers …Run Code Online (Sandbox Code Playgroud)