별의 공부 블로그 🧑🏻‍💻
728x90
728x170

쉘(Shell) 프로그램 구현 (execl(), execlp() 사용)

 

리눅스에서 C 언어의 `execl()`, `execlp()` 함수를 사용하여 쉘 프로그램을 구현할 수 있다.

 

■ `execl()`, 함수를 사용하여 쉘 프로그램 구현하기

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#define MAXLINE 64
 
int main(int argc, char **argv) {
    char buf[MAXLINE];
    pid_t pid;
 
    printf("TestShell ver 1.0\n");
 
    while (1) {
        memset(buf, 0x00, MAXLINE);
        fgets(buf, MAXLINE - 1, stdin);
        // char *fgets (char *string, int n, FILE *stream)
        if (strncmp(buf, "exit\n"5== 0) {
            break;
        }
        buf[strlen(buf) - 1= 0x00;  // Remove Keyboard Input(Enter)
 
        pid = fork();
        if (pid == 0) {
            if (execl(buf, buf, NULL== -1) {
                printf("Command Execution is failed\n");
                exit(0);
            }
        }
        if (pid > 0) {
            wait(NULL);
        }
    }
    return 0;
}
 

 

- 콘솔에서 확인하기

 

ubuntu@ip-172-xx-xx-90:~/test/ForkExeclWaitProgram$ vim ShellTest.c 
ubuntu@ip-172-xx-xx-90:~/test/ForkExeclWaitProgram$ gcc ShellTest.c -o ShellTest
ubuntu@ip-172-xx-xx-90:~/test/ForkExeclWaitProgram$ ./ShellTest 

 

TestShell ver 1.0
ls
Command Execution is failed
/bin/ls
FEWProgram  ShellTest.c  execltest.c  execvetest.c          forktest
ShellTest   execltest     execvetest   forkexeclwaitProgram.c  forktest.c
exit

 

`execl()` 함수의 특성상 `/bin/ls` 경로를 붙여야 명령어를 수행할 수 있다.

 

 

■ `execlp()`, 함수를 사용하여 쉘 프로그램 구현하기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#define MAXLINE 64
 
int main(int argc, char **argv) {
    char buf[MAXLINE];
    pid_t pid;
 
    printf("TestShell ver 1.1\n");
 
    while (1) {
        memset(buf, 0x00, MAXLINE);
        fgets(buf, MAXLINE - 1, stdin);
        // char *fgets (char *string, int n, FILE *stream)
        if (strncmp(buf, "exit\n"5== 0) {
            break;
        }
        buf[strlen(buf) - 1= 0x00;  // Remove Keyboard Input(Enter)
 
        pid = fork();
        if (pid == 0) {
            if (execlp(buf, buf, NULL== -1) {   // execl() -> execlp()
                printf("Command Execution is failed\n");
                exit(0);
            }
        }
        if (pid > 0) {
            wait(NULL);
        }
    }
    return 0;
}
 

 

- 콘솔에서 확인하기

 

ubuntu@ip-172-xx-xx-90:~/test/ForkExeclWaitProgram$ vim ShellTest.c 
ubuntu@ip-172-xx-xx-90:~/test/ForkExeclWaitProgram$ gcc ShellTest.c -o ShellTest
ubuntu@ip-172-xx-xx-90:~/test/ForkExeclWaitProgram$ ./ShellTest 

 

TestShell ver 1.1
ls
FEWProgram  ShellTest.c  ShellTest2.c  execltest.c  execvetest.c        forktest
ShellTest   ShellTest2     execltest     execvetest   forkexeclwaitProgram.c  forktest.c
/bin/ls
FEWProgram  ShellTest.c  ShellTest2.c  execltest.c  execvetest.c        forktest
ShellTest   ShellTest2     execltest     execvetest   forkexeclwaitProgram.c  forktest.c
exit
 

 

`execlp()` 함수로 인하여 `/bin/ls` 경로를 붙이지 않아도 명령어를 수행할 수 있다.

728x90
그리드형(광고전용)
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖