2015-06-15 181 views
-1

我有无线USB适配器,我使用“pstree”命令监视所有进程。
当我将USB适配器插入到我的Linux操作系统中时,我看到带有“pstree”命令的新进程“wpa_supplicant”。如何在C,C++的Linux操作系统上启动进程

我在C/C++语言中使用。我知道Linux操作系统将使用“NetworkManager”守护程序来监控网络(eth,bluetooth,wifi等),但我不知道如何启动“wpa_supplicant”?我可以使用dbus或systemd吗?

感谢 通LT

+0

你有没有真的试图DBUS或systemd,看看他们是否还工作吗? – Kmeixner

+1

你究竟想实现什么?尽快建立无线网络,开始连接? –

回答

0

标准UNIX方式是通过电话exec(3)使用fork(2)其次(还有的全家他们,选择任何一个适合您需要的最好的)。

例子来说明使用

#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; 
} 
+0

fork()或exec将创建子进程,但我看到wpa_supplicant是父进程。我将再次检查fork和exec –

+0

我已经添加了一个注释示例。希望这应该有所帮助。 – PSkocik

0

使用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; 

}

相关问题