*석*
개인인증
판매자 정보
- 학교정보
-
입력된 정보가 없습니다.
- 직장정보
-
입력된 정보가 없습니다.
- 자격증
-
판매지수
-
- 판매중 자료수
- 2개
-
- 전체 판매량
- 25개
-
- 최근 3개월 판매량
- 0개
-
- 자료후기 점수
- -
-
- 자료문의 응답률
- -
전체자료 2개
-
-
[알고리즘] 허프만 코드 과제/ 소스코드, 보고서, 결과화면 캡쳐 포함
-
"[알고리즘] 허프만 코드 과제/ 소스코드, 보고서, 결과화면 캡쳐 포함"에 대한 내용입니다.
-
프로그램소스|
2021.10.12|
4페이지| 2,000원|
조회(316)
-
미리보기
-
-
시스템 프로그래밍 리눅스 myshell 만들기
-
시스템 프로그래밍(O) 기말과제1. 소스코드#include #include #include #include #include #include #include #include #include #define MAX_CMD_ARG 20#define BUFSIZ 256#define MAXJOBS 128const char *prompt = "myshell> "; // 프롬프트 창char cmdline[BUFSIZ];pid_t fgpid = 0;int counter;typedef struct Job // myjobs 구현을 위한 job struct{pid_t jpid; // pidint jobNum; // 번호char *status; // 상태char *command; // 입력} Job;void fatal(char *str){perror(str);exit(1);}int makelist(char *s, const char *delimiters, char **list, int MAX_LIST){int numtokens = 0;char *snew = NULL;if ((s == NULL) || (delimiters == NULL))return -1;snew = s + strspn(s, delimiters); /* Skip delimiters */if ((list[numtokens] = strtok(snew, delimiters)) == NULL)return numtokens;numtokens = 1;while (1){if ((list[numtokens] = strtok(NULL, delimiters)) == NULL)break;if (numtokens == (MAX_LIST - 1))return -1;numtokens++;}return numtokens;}// chdir() 시스템 콜을 수행하는 함수 (HOME 디렉토리 이동 기능 적용)void newchdir(char **cargv, int cargc){uid_t uid = getuid(); // 프로세스를 실행한 사용자 id 저장struct passwd *pwd = getpwuid(uid); // uid의 여러 정보 저장char *pathname;if (cargc == 1 || !strcmp(cargv[1], "~")) // 입력된 경로가 없거나 ~인 경우pathname = pwd->pw_dir; // pwd에서 Home 디렉토리 경로를 꺼내어 저장elsepathname = cargv[1];if (chdir(pathname) != 0)perror("newchdir()");}//command line의 뒤에서부터 가장 처음 등장하는 공백과 탭을 제외한 문자가 &인지 확인int bgfinder(){int bg = 0;int index = strlen(cmdline) - 1;while (1){if (cmdline[index] != ' ' && cmdline[index] != 't'){if (cmdline[index] == '&' && cmdline[index - 1] != '&') //&&인 경우 skip{bg = 1; // 백그라운드 실행cmdline[index] = ' '; // &문자를 공백으로 대체}break;}index--;}return bg;}static void sigchldhandler(int signo) // 시그널 포착시 수행{pid_t pid;pid = wait(NULL);if (fgpid == pid)fgpid = 0;}// redirect 핸들러int redihandler(int *cargc, char **cargv){int fd, direction;int i = 0;for (; i < *cargc; i++){if (!strcmp(cargv[i], "")){direction = 1;break;}}if (i < *cargc) // < 또는 > 가 존재할 때{if (i < *cargc - 1) //문법 오류 판별{if (direction == 0) //redirection이 input일 때{if ((fd = open(cargv[i + 1], O_RDONLY)) == -1)fatal("redihandler()");}else //redirection이 output일 때{if ((fd = open(cargv[i + 1], O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)fatal("redihandler()");}dup2(fd, direction);close(fd);for (; i < *cargc; i++){if (i < *cargc - 2)cargv[i] = cargv[i + 2];elsecargv[i] = NULL;}*cargc -= 2;return 0;}else{fputs("redihandler(): Syntax errorn", stderr);exit(1);}}elsereturn 1;}void connpipe(int *argclist, char ***argvlist, int *pfd, int count, int idx){pid_t pid = fork();switch (pid){case -1:fatal("connpipe()");case 0:dup2(pfd[1], 1);close(pfd[1]);close(pfd[0]);execvp(argvlist[idx][0], argvlist[idx]);break;default:while (1){if (redihandler(&argclist[idx + 1], argvlist[idx + 1]))break;}dup2(pfd[0], 0);close(pfd[0]);close(pfd[1]);if (count > idx + 1){if (pipe(pfd) == -1) //또 다른 연결을 위한 파이프 재생성fatal("connpipe()");connpipe(argclist, argvlist, pfd, count, idx + 1); //재귀}elseexecvp(argvlist[idx + 1][0], argvlist[idx + 1]); //마지막 실행}}int pipehandler(int *cargc, char **cargv) // 파이프 핸들러(파이프 구현하는 김에 여러 파이프 동작도 구현 해봤습니다.{pid_t pid;int count = 0; //파이프 개수int start = 0;int subc[MAX_CMD_ARG];char **subv[MAX_CMD_ARG];int pfd[2];for (int i = 0; i < *cargc; i++){if (!strcmp(cargv[i], "|")){subc[count] = i - start;subv[count] = (char **)malloc(subc[count] * sizeof(char *));for (int j = 0; j < subc[count]; j++)subv[count][j] = cargv[j + start];start = i + 1;count++;}}if (count == 0)return 1; //pipe가 존재하지 않음subc[count] = *cargc - start;subv[count] = (char **)malloc(subc[count] * sizeof(char *));for (int j = 0; j < subc[count]; j++)subv[count][j] = cargv[j + start];while (1){if (redihandler(&subc[0], subv[0]))break;}if (pipe(pfd) == -1) //파이프 생성fatal("connpipe()");connpipe(subc, subv, pfd, count, 0); //파이프 연결 함수}int myjobs_cmd(char **argv){struct Job *jobList;jobList = malloc(MAXJOBS * sizeof(struct Job));if (!strcmp(argv[0], "jobs")){if (jobList[1].status == NULL){printf("No jobsn");}else{for (int i = 1; i < counter + 1; i++){printf("[%d] ", i);printf(" %dt", jobList[i].jpid);printf(" %st", jobList[i].status);printf(" %s", jobList[i].command);}}return 1;}}int main(int argc, char **argv){char *cmdvector[MAX_CMD_ARG];int cmdargc;int bg = 0;pid_t pid;if (signal(SIGINT, SIG_IGN) == SIG_ERR) // interrupt keyboardfatal("can't catch SIGINT");if (signal(SIGQUIT, SIG_IGN) == SIG_ERR) // quit from keyboardfatal("can't catch SIGQUIT");if (signal(SIGTSTP, SIG_IGN) == SIG_ERR) // 타이핑 중단fatal("can't catch SIGTSTP");if (signal(SIGCHLD, sigchldhandler) == SIG_ERR) // child 중단fatal("can't catch SIGCHLD");while (1){fputs(prompt, stdout);fgets(cmdline, BUFSIZ, stdin);cmdline[strlen(cmdline) - 1] = '