Linux에서 process 생성하기 (fork/exec)
2012. 11. 19. 13:25 in 프로그래밍/Linux/Ubuntu

Linux에서 process를 생성하기 위해서는 fork를 실행한후 exec 함수군을 사용하면 됩니다.
제가 한동안 Windows 개발자라 Windows convention에 맞게 만든 CreateProcess를 공유합니다.
물론, Linux에서 사용되는 함수입니다.
와 같습니다.
각 argument는 다음과 같습니다.
필요없는 경우는 지우셔도 됩니다.
그리고, 필요한 경우 다음 define이 필요합니다.
사용법은 다음과 같습니다.
위는, /dev/sda1 장치를 mount하는 예를 나타낸 것입니다.
제가 한동안 Windows 개발자라 Windows convention에 맞게 만든 CreateProcess를 공유합니다.
물론, Linux에서 사용되는 함수입니다.
- // ...은 argument list (char*)
- bool CreateProcess(IN LPCSTR lpszCmdFullPath, IN bool bWait, OPTIONAL OUT int* pnExitCode, IN int nCountArg, ...)
- {
- bool bRtnValue = false;
- int nStatVal = 0;
- pid_t nPid = -1;
- va_list arg = 0;
- LPCSTR lpszArgList[256] = {0,};
- int nArgNum = 0;
- if (NULL != pnExitCode)
- {
- *pnExitCode = -1;
- }
- if (nCountArg >= 256)
- {
- ERRORLOG("Too many argument");
- bRtnValue = false;
- goto FINAL;
- }
- if (-1 == access(lpszCmdFullPath, X_OK))
- {
- // 명령을 실행할 수 없음
- bRtnValue = false;
- ERRORLOG("Error to access(%s), errno=%d", lpszCmdFullPath, errno);
- goto FINAL;
- }
- INFOLOG("before call fork");
- nPid = fork();
- if (-1 == nPid)
- {
- ERRORLOG("Fail to fork, errno=%d", errno);
- bRtnValue = false;
- goto FINAL;
- }
- else if (nPid == 0)
- {
- // child
- INFOLOG("in fork child, before exec %s", lpszCmdFullPath);
- // 첫번째 파라미터는 경로
- lpszArgList[0] = lpszCmdFullPath;
- nArgNum = 1;
- va_start(arg, nCountArg);
- {
- for (;;)
- {
- if (nArgNum >= nCountArg+1)
- {
- break;
- }
- lpszArgList[nArgNum] = va_arg(arg, char*);
- if (NULL == lpszArgList[nArgNum])
- {
- break;
- }
- INFOLOG("\tParameter : %s", lpszArgList[nArgNum]);
- nArgNum++;
- }
- }
- va_end(arg);
- if (-1 == execvp(lpszCmdFullPath, (char* const*)lpszArgList))
- {
- ERRORLOG("Fail to execl, errno=%d", errno);
- if (errno != 0)
- {
- _exit(errno);
- }
- }
- _exit(255);
- }
- else
- {
- INFOLOG("parent");
- if (true == bWait)
- {
- INFOLOG("Start to wait");
- if (-1 == wait(&nStatVal))
- {
- ERRORLOG("Fail to wait, errno=%d", errno);
- goto FINAL;
- }
- INFOLOG("Finished to wait");
- if (NULL != pnExitCode)
- {
- if (true == WIFEXITED(nStatVal))
- {
- // 정상 종료 했음
- *pnExitCode = WEXITSTATUS(nStatVal);
- }
- }
- }
- }
- bRtnValue = true;
- FINAL:
- return bRtnValue;
- }
와 같습니다.
각 argument는 다음과 같습니다.
- lpszCmdFullPath
; 명령의 Full path. argument가 없다. 예) /bin/ls - bWait
; 종료까지 기다릴지 여부 - nCountArg
; 가변인자(...)의 개수 - pnExitCode
; 프로세스의 Exit Code. 종료시까지 기다리고 제대로 종료되었을 때 의미있음 - ...
; 프로세스에 전달될 Argument. 여러개 가능. 예) -la - 리턴값
; 성공시 : true / 실패시 : false
필요없는 경우는 지우셔도 됩니다.
그리고, 필요한 경우 다음 define이 필요합니다.
- // char type
- typedef const char* LPCSTR;
- typedef char* LPSTR;
- #define IN
- #define OUT
- #define OPTIONAL
- #define COUNTOF(array) (sizeof(array)/sizeof(array[0]))
사용법은 다음과 같습니다.
- int nExitCode = 0;
- if (true == CreateProcess("/bin/mount",
- true,
- &nExitCode,
- 2,
- "/dev/sda1", "/mnt/sda1_mnt"))
- {
- // 실행 성공
- if (0 == nExitCode)
- {
- // 성공
- }
- else
- {
- // 실패
- // man mount의 마지막 RETURN CODE 참고
- }
- }
- else
- {
- // 실행 실패
- }
위는, /dev/sda1 장치를 mount하는 예를 나타낸 것입니다.
'프로그래밍 > Linux/Ubuntu' 카테고리의 다른 글
ubuntu vmware 공유 폴더(shared folder) 실패 해결 방법 (hgfs) (6) | 2015.06.03 |
---|---|
Linux(리눅스) Makefile 예제,강좌 (debug/release 경로 분리, rebuild, 자동 dependency) (4) | 2013.01.28 |
qtcreator에서 .cpp <--> .h 쉽게 전환하기 (0) | 2012.11.02 |
vmware에서 동작하는 ubuntu(11.04)에서 한글키 활성화하기 (0) | 2012.11.01 |
ubuntu(11.04)에서 qtcreator/kde/qt 한글 입력하기 (nabi 설치, sudo 프로세스) (0) | 2012.11.01 |