我在同一个进程中有关于主线程和其他线程的问题.当主函数返回时,其他线程也退出?我有些困惑.我写了一些测试代码,如下所示:
void* test1(void *arg)
{
unsigned int i = 0;
while (1){
i+=1;
}
return NULL;
}
void* test2(void *arg)
{
long double i = 1.0;
while (1){
i *= 1.1;
}
return NULL;
}
void startThread ( void * (*run)(void*), void *arg) {
pthread_t t;
pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0
|| pthread_create(&t, &attr, run, arg) != 0
|| pthread_attr_destroy(&attr) != 0
|| pthread_detach(t) != 0) {
printf("Unable to launch a thread\n");
exit(1);
}
}
int main()
{
startThread(test1, …Run Code Online (Sandbox Code Playgroud) 出于某些安全目的,我使用ptrace来获取系统调用号码,如果这是一个危险的调用(例如10 for unlink),我想取消这个系统调用.
这是测试程序的源代码del.c.编译gcc -o del del.c.
#include <stdio.h>
#include <stdlib.h>
int main()
{
remove("/root/abc.out");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是安全管理器源代码test.c.编译gcc -o test test.c.
#include <signal.h>
#include <syscall.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <sys/user.h>
#include <sys/reg.h>
#include <sys/syscall.h>
int main()
{
int i;
pid_t child;
int status;
long orig_eax;
child = fork();
if(child == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execl("/root/del", "del", NULL);
}
else {
i = …Run Code Online (Sandbox Code Playgroud)