Project2_small shell 개선

  1. Background process가 종료되면 signal을 받아 정상 종료가 완료되도록 처리

Untitled

main.c

#include "smallsh.h"
#include <setjmp.h>

//char *prompt = "Command> ";
char pdir[200];

extern sigjmp_buf position;

int main() {
        sigsetjmp(position, 1);

        struct sigaction sint;
        sigemptyset(&sint.sa_mask);
        sint.sa_handler = SIG_IGN;
        sigaction(SIGINT, &sint, NULL);

        getcwd(pdir,200);
        while(userin(pdir) != EOF){
        //      printf("pdir: %s\\n", pdir);
                procline();
                getcwd(pdir,200);
        }

        return 0;
}

smallsh.c

void sigchild_handler(int a ){
        int status;
        pid_t pid = waitpid(-1, &status, WNOHANG);

        if(pid > 0)
                snprintf(bg_pid,10, "%d", pid);
        siglongjmp(position, 1);
}

/..../

int runcommand(char **cline, int where) {

	/..../

									if (where == BACKGROUND) {
	                        struct sigaction sigchild;
	                        sigemptyset(&sigchild.sa_mask);
	                        sigchild.sa_handler = sigchild_handler;
	                        sigaction(SIGCHLD, &sigchild,NULL);
	                        printf("[Process id] %d\\n", pid);
	                        return 0;
	                }
	                if (waitpid(pid, &status, 0) == -1)
	                        return -1;
	                else {
	                        int temp = WEXITSTATUS(status);
	                        //printf("WEXITSTATUS: %d\\n", temp);
	                        snprintf(fg_status, 10,"%d", temp);
	                        return status;
	                }
}
  1. SIGINT 처리

Untitled

main.c

#include "smallsh.h"
#include <setjmp.h>

//char *prompt = "Command> ";
char pdir[200];

extern sigjmp_buf position;

int main() {
        sigsetjmp(position, 1);

        struct sigaction sint;
        sigemptyset(&sint.sa_mask);
        sint.sa_handler = SIG_IGN;
        sigaction(SIGINT, &sint, NULL);

        getcwd(pdir,200);
        while(userin(pdir) != EOF){
        //      printf("pdir: %s\\n", pdir);
                procline();
                getcwd(pdir,200);
        }

        return 0;
}