我有无线USB适配器,我使用“pstree”命令来监控所有进程 当我将USB适配器插入Linux操作系统时,我看到新进程“wpa_supplicant”和“pstree”命令。
我使用的是C / C ++语言。我知道Linux操作系统将使用“NetworkManager”守护进程来监控网络(eth,蓝牙,wifi等),但我不知道如何才能启动“wpa_supplicant”?我可以使用dbus或systemd吗?
由于 Thong LT
答案 0 :(得分:2)
使用fork()系统调用,它将创建子进程,或者如果要通过C代码运行可执行文件,则使用exec()库函数并指定可执行文件的路径。
以下是代码:
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<sys/wait.h>
int main() {
pid_t pid,p;
int i,status;
printf("Parent process starts with PID = %d it's Parent ID %d\n",(int)getpid(),(int)getppid());
if((pid = fork())==-1) {
fprintf(stderr,"FORK FAILED\n");
return -1;
}
if(pid == 0 ) {
printf("Child process starts with PID = %d\n",(int)getpid());
for(i = 0 ; i < 5; i++) {
printf("Child prints [%d]\n",i);
sleep(2);
}
_exit(0);
//exit(0);
}
else {
p = wait(&status);
printf("Parent resumes execution, took control from the child %d \n",(int)p);
//printf("Return status of the child is %d\n",status);
for(i = 0; i< 5 ; i++) {
sleep(2);
printf("Parent prints [%d]\n",i);
//sleep(2);
}
_exit(0);
}
return 0;
}
答案 1 :(得分:0)
标准的 UNIX 方式是使用fork(2)后跟exec(3)电话(其中包括全家人 - 选择最适合您的需求)
说明用法的示例:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
pid_t pid;
printf("before fork\n");
if ((pid = fork()) < 0) {
//It may fail -- super rare
perror("Fork failed");
} else if (pid > 0) {
//If it returns a positive number, you're in the parent process and pid holds the pid of the child
printf("Mah kid's pid is %d\n", pid);
printf("Mine's %d\n", getpid());
} else {
//If it returns zero, you're in the child process
//you can do some preparatory work here (e.g., close filedescriptors)
printf("I'm the child and my pid is %d\n", getpid());
//exec will replace the process image with that of echo (wherever in the PATH environment variable it is be found (using the execlP version here)
execlp("echo", "echo", "hello world from echo; I'm now the child because I've replaced the original child because it called an exec function", (char*)NULL);
printf("This won't run because now we're running the process image of the echo binary. Not this.");
}
return EXIT_SUCCESS;
}