2012-10-11 4 views
0

나는 리눅스에서 프로세스를 만들려고하는데 오류가 계속 발생한다. 내 C++ 코드에서 firefox.exe를 열고 싶다. 내 코드는 다음과 같습니다.리눅스에서 프로세스를 만드는 방법

//header files 
#include <sys/types.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <iostream> 

using namespace std; 

//main function used to run program 
int main() 
{ 
    //declaration of a process id variable 
    pid_t pid; 

    //fork a child process is assigned 
    //to the process id 
    pid=fork(); 

    //code to show that the fork failed 
    //if the process id is less than 0 
    if(pid<0) 
    { 
     fprintf(stderr, "Fork Failed");// error occurred 
     exit(-1); //exit 
    } 

    //code that runs if the process id equals 0 
    //(a successful for was assigned 
    else if(pid==0) 
    { 
     //this statement creates a specified child process 
     execlp("usr/bin","firefox",NULL);//child process 
    } 

    //code that exits only once a child 
    //process has been completed 
    else 
    { 
     wait(NULL);//parent will wait for the child process to complete 
     cout << pid << endl; 

     printf("Child Complete"); 
     exit(0); 
    } 
} 

wait() 함수에 오류가 있습니다. 나는 이것을 버리고 시도했지만, 아무 일도 일어나지 않았다.

+1

execlp()에 오류가 있는지 확인한 다음 자식이 종료되도록하려면 어떻게됩니까? – pilcrow

답변

2

당신은 쓸 필요가 :

execlp("/usr/bin/firefox","firefox",NULL); 

당신은 또한 실패 할 경우에 때 execlp 후 _exit를 넣어해야합니다.

+0

실제로'fork'가 실패하지 않았기 때문에 프로세스를 만들었지 만'execlp'가 잘못되어 프로세스가'firefox'를 실행하지 못했습니다. 레슨 : 항상 모든 시스템 호출의 성공 여부를 테스트하십시오 (사용자에게 알리지 않을 때'perror'를 사용할 수도 있습니다). –

2

execlp을 제대로 호출했다고 생각하지 않습니다.

"firefox""usr/bin"에 붙이지 않을 것입니다. 환경 변수 PATH을 검색하므로 execlp("firefox","firefox",NULL)으로 호출 할 수 있습니다.


는 제외 : 예, 기능의 exec 가족은 argv[0]이 실행 파일 이름을 지정해야한다는 명목 보증을 중단 할 수 있습니다. 죄송합니다, 그것이 바로 그 길입니다.

0

프로세스를 만들려면 시스템 호출, fork 호출, execl 호출을 사용할 수 있습니다. 이 호출을 사용하여 Linux에서 프로세스를 만드는 방법을 알고 싶으면 다음 링크를 클릭하십시오. 예를 들어 프로세스 생성에 대해 더 많이 이해하는 데 도움이 될 것입니다. http://www.firmcodes.com/process-in-linux/

관련 문제