더 나은?

더 나은?
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<error.h>
#define size 30
char *get_command(int argc, char *argv[]);
int my_system(const char *command);

int my_system(const char *command)
{
    int ret = 0;

    ret = execl("/bin/sh", "sh", "-c", command, (char *)NULL);
    if (ret == -1)
        error(1, 0, "error occcured in the execl() system call\n");
    return 0;
}

char *get_command(int argc, char **argv)
{
    int i = 0;
    static char command[size];

    strcpy(command, argv[1]);
    for (i = 2; i < argc; i++) {
        strcat(command, " ");
        strcat(command, argv[i]);
    }
    return command;
}

int main(int argc, char *argv[])
{
    pid_t pid;
    pid_t ret;
    int ret_system;
    int i = 0;
    int wstatus;
    char *command;

    if (argc < 2)
        error(1, 0, "Too few arguments\n");
    printf("The pid of the parent-process is :%d\n", getpid());
    pid = fork();
    if (pid == -1) {
        error(1, 0, "error in creating the sub-process\n");
    } else if (pid == 0) {
        printf("The pid of the child- process is :%d\n", getpid());
        command = get_command(argc, argv);
        ret_system = my_system(command);
    } else {
        ret = waitpid(-1, &wstatus, 0);
        printf("The pid of the child that has terminated is %d and the status of exit is %d\n", ret, wstatus);
    }
    return 0;
}

exec 명령 대신 exec 쉘을 사용하지 않고 fork()를 my_system 함수로 옮기려고 하는데 그게 안되고 어려워요. 저를 도와주세요. 나는 초보자입니다. 감사합니다.

int my_system(const char *command)
{
    pid_t pid;
    int wstatus  = 0;
    int ret = 0;

    if (command == NULL)
        return 1;
    pid = fork();
    if (pid == -1)
        return -1;
    else if (pid == 0) {
        execle("/bin/sh", "sh", "-c",command, (char *)NULL);
    } else {
        ret = waitpid(-1, &wstatus, 0);
    }
    return wstatus;
}

char *get_command(int argc, char **argv)
{
    int i = 0;
    static char command[size];

    if (argc == 1)
        return NULL;

    strcpy(command, argv[1]);
    for (i = 2; i < argc; i++) {
        strcat(command, " ");
        strcat(command, argv[i]);
    }
    return command;
}

int main(int argc, char *argv[])
{
    int ret;
    char *command;

    command = get_command(argc, argv);
    ret = my_system(command);
    if (ret == 1)
        printf("Command is NULL, shell is available\n");
    else if (ret == -1)
        printf("Child process could not be created\n");
    else
        printf("The status is :%d\n", ret);
    return 0;
}

답변1

내장된 시스템 호출과 마찬가지로 시스템 호출을 하려면 해당 호출에 들어가야 합니다 fork.

get_command오류가 있습니다. 스택 변수에 대한 포인터를 반환합니다. 해당 동작은 정의되지 않았습니다(잠시 동안 작동한 후 중지될 수 있음).

반환 값을 확인할 필요가 없습니다 exec. 반환되면 오류가 있는 것입니다. 따라서 ret_system 변수는 0만 수신합니다.

또한 쉘이 필요하지 않습니다(사용하고 싶지 않은 경우: 와일드카드, 변수 확장. 그렇게 하지 않습니다).

더 나은?

  • system이식 가능합니다(MS-Windows 및 기타 하위 운영 ​​체제).
  • fork그리고 exec더욱 강력해졌습니다. 예를 들어 파이프를 설정할 수 있습니다.
  • 대부분의 목적에 적합한 추상화를 제공하는 다른 고급 라이브러리가 있습니다.
  • 높은 수준의 라이브러리를 사용하는 것(또는 직접 추상화하는 것)이 더 나을지라도 forkand를 사용하는 것은 학습에 좋습니다.exec

관련 정보