OSLab课堂作业2

日期:2019/3/23

内容:

实现内容

要求

mysys.c

实现函数mysys,用于执行一个系统命令。

  • mysys的功能与系统函数system相同,要求用进程管理相关系统调用自己实现一遍
  • 使用fork/exec/wait系统调用实现mysys
  • 不能通过调用系统函数system实现mysys

sh1.c

实现shell程序,要求具备如下功能。

  • 支持命令参数

$ echo arg1 arg2 arg3

$ ls /bin /usr/bin /home

  • 实现内置命令cd、pwd、exit

$ cd /bin

$ pwd

/bin

  • mysys.c

/*************************************************************************

    > File Name: mysys.c

    > Author: sinkinben

    > E-mail: sinkinben@qq.com

    > Created Time: Sat 23 Mar 2019 08:06:40 AM CST

************************************************************************/

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/wait.h>

#define SIZE 1024

static char program[SIZE];

void mysys(const char *command)

{

    if(command == NULL)

    {

        return;

    }

    //execl("/bin/bash", "bash", "-c", command, NULL); //这样做当前的进程就会清空,无法返回main

    memset(program, 0, sizeof(program));

    strcpy(program, command);

    char *arg;

    strtok(program, " ");

    arg = strtok(NULL, "");

    pid_t pid = fork();//新建一个进程

    if(pid == 0)

    {

        execlp(program, program, arg, NULL);

        exit(-1);

    }

    int status;

    wait(&status);//等待子进程结束

    if(status != 0)

        printf("command '%s' does not exist... ", command);

}

 

int main()

{

    printf("-------------------------------------------------- ");

    mysys("ls /");

    printf("-------------------------------------------------- ");

    mysys("echo HELLO WORLD");

    printf("-------------------------------------------------- ");

    mysys("ls -a");

    printf("-------------------------------------------------- ");

    mysys("not exist command");

    printf("-------------------------------------------------- ");

    return 0;

}

 

  • myshell.c

/*************************************************************************

    > File Name: myshell.c

    > Author: sinkinben

    > E-mail: sinkinben@qq.com

    > Created Time: Sat 23 Mar 2019 10:46:26 AM CST

************************************************************************/

 

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>

#define BUFF_SIZE 1024*2

static char buffer[BUFF_SIZE];

static char *usr = "shell@sin:";

 

void shell_handler(char *command)

{

    char *arg;

    int status;

    pid_t pid;

    strtok(command, " ");

    arg = strtok(NULL, ""); //arg maybe NULL

 

    if(strcmp(command, "cd") == 0)

    {

        chdir(arg);

        return;

    }

    pid = fork();

    if(pid == 0)

    {

        execlp(command, command, arg, NULL);

        exit(-1);

    }

    wait(&status);

    if(status != 0)

    {

        printf("command '%s' does not exist... ", command);

    }

 

}

 

int main(int argv, char *argc[])

{

    puts("Welcome to MyShell :) ");

    printf("%s", usr);

    while( gets(buffer) != NULL)

    {

        if(strcmp(buffer, "exit") == 0)

            break;

        shell_handler(buffer);

        memset(buffer, 0, sizeof(buffer));

        printf("%s", usr);

    }

    puts("Exit MyShell :)");

    return 0;

}

 

原文地址:https://www.cnblogs.com/sinkinben/p/10584257.html