Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>
Docbook 원문

system(3)

1장. system(3)

차례
1.1. 사용법
1.2. 설명
1.3. 반환값
1.4. 예제
1.5. 참고문헌

shell 명령어 실행기


1.1. 사용법

#include <stdlib.h>

int system(const char *string);
		


1.2. 설명

system() 함수는 /bin/sh -c string를 호출하여 string에 지정된 명령어를 실행하고, 명령어가 끝난후 반환한다. 명령어가 실행동안, SIGCHLD는 블럭되며, SIGINT, SIGQUIT는 무시된다. system() 함수는 기본적으로 fork()와 execve()의 조합응용이다.


1.3. 반환값

만약 /bin/sh를 실행시키기 위한 execve()의 호출이 실패했다면 127이 리턴되며, 다른 에러가 있다면 -1, 그렇지 않다면 명령어의 리턴코드가 반환된다.

string값이 NULL이고, system()이 shell을 이용할 수 있다면 0이 아닌 값을 그렇지 않다면 0을 반환한다.

system()은 다른 wait()상태의 다른 자식에게 영향을 주지 않는다.


1.4. 예제

#include <unistd.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    int i = 0;
	int retval;
    retval = system("ls -al");
    printf("Exit Status %d\n", retval);
}
		


1.5. 참고문헌

  1. fork()

  2. execl()