2015-06-15 100 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; 

}

相關問題