#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/types.h>
void *thread_execl()
{
pid_t pid;
int ret;
if ((pid = fork()) < 0)
{
printf("fork error\n");
}
else if (pid == 0)
{
execl("/bin/ls", "ls" , "-al", "/etc/passwd", (char*)0);
} else {
int status;
wait(&status);
ret = WEXITSTATUS(status);
printf("ret: %d\n", ret);
}
pthread_exit(NULL);
}
int main()
{
pthread_t p_id;
int ret;
ret = pthread_create(&p_id, NULL, thread_execl, NULL);
if (ret != 0)
{
printf("create pthread error\n");
}
return 0;
}
Unix中pthread()+fork()+execl()解决system()导致主进程阻塞的例子
最新推荐文章于 2022-09-18 16:06:02 发布