목차
1. 문제정의
2. 문제분석
3. Design
4. 결과화면
5. 결론 및 소감
6. Source
7. 예제 11-6 (shell)
2. 문제분석
3. Design
4. 결과화면
5. 결론 및 소감
6. Source
7. 예제 11-6 (shell)
본문내용
fsplit(command, NARGS, args);
args[n] = NULL;
if(**args == '\0')
continue;
infile = NULL;
outfile = NULL;
for(cp=args; *cp != NULL; cp++)
{
if(strcmp(*cp, "<") == 0)
{
if(*(cp+1) == NULL)
{
fprintf(stderr, "You must specify ");
fprintf(stderr, "an input file.\n");
goto again;
}
*cp++ = NULL;
infile = *cp;
}
else if(strcmp(*cp, ">") == 0)
{
if(*(cp+1) == NULL)
{
fprintf(stderr, "You must specify ");
fprintf(stderr, "an output file.\n");
goto again;
}
*cp++ = NULL;
outfile = *cp;
}
}
status = execute(args, infile, outfile);
}
}
int execute(char **args, char *infile, char *outfile)
{
int status;
pid_t p, pid;
int infd, outfd;
extern int errno;
sigset_t mask, savemask;
struct sigaction ignore, saveint, savequit;
struct tms t; // tms 구조체 t를 선언
clock_t clock; // clock_t 자료형 clock 선언
infd = -1;
outfd = -1;
if(infile != NULL)
{
if((infd = open(infile, O_RDONLY)) < 0)
{
perror(infile);
return(-1);
}
}
if(outfile != NULL)
{
if((outfd = creat(outfile, 0666)) < 0)
{
perror(outfile);
close(infd);
return(-1);
}
}
sigemptyset(&ignore.sa_mask);
ignore.sa_handler = SIG_IGN;
ignore.sa_flags = 0;
sigaction(SIGINT, &ignore, &saveint);
sigaction(SIGQUIT, &ignore, &savequit);
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &mask, &savemask);
clock = times(&t); // fork함수가 실행되기 전에 times 함수를 호출
if((pid=fork()) < 0)
status = -1;
if(pid == 0)
{
sigaction(SIGINT, &saveint, (struct sigaction *) 0);
sigaction(SIGQUIT, &savequit, (struct sigaction *) 0);
sigprocmask(SIG_SETMASK, &savemask, (sigset_t *) 0);
if(infd > 0)
dup2(infd, 0);
if(outfd > 0)
dup2(outfd, 1);
execvp(*args, args);
perror("exec");
_exit(127);
}
while(waitpid(pid, &status, 0) < 0)
{
if(errno != EINTR)
{
status = -1;
break;
}
}
clock = times(&t); // wait 함수를 호출한 뒤에 times 함수를 호출
FILE *fp;
fp = fopen("log.txt","a"); // 프로세스 시간을 입력하기 위해 log.txt 파일을 연다
fprintf(fp,"Process Time : %f\n", (double)(t.tms_utime+t.tms_stime));
// 부모프로세스 시간을 입력한다.
fprintf(fp,"Children Process Time : %f\n", (double)t.tms_cutime+t.tms_cstime);
// 자식프로세스 시간을 입력한다.
fclose(fp); // 파일을 닫는다.
sigaction(SIGINT, &saveint, (struct sigaction *) 0);
sigaction(SIGQUIT, &savequit, (struct sigaction *) 0);
sigprocmask(SIG_SETMASK, &savemask, (sigset_t *) 0);
close(outfd);
close(infd);
return(status);
}
size_t bufsplit(char *buf, size_t n, char **a)
{
int i, nsplit;
static char *splitch = "\t\n";
if(buf != NULL && n == 0)
{
splitch = buf;
return(1);
}
nsplit = 0;
while(nsplit < n)
{
a[nsplit++] = buf;
if((buf = strpbrk(buf, splitch)) == NULL)
break;
*(buf++) = '\0';
if(*buf == '\0')
break;
}
buf = strrchr(a[nsplit-1], '\0');
for(i=nsplit; i
a[i] = buf;
return(nsplit);
}
void handler(int sig) // 종료시 받게 되는 SIG 값을 log 파일에 저장하기 위한 handler
{
FILE *fp;
char str[80];
fp = fopen("log.txt", "a"); // log.txt 파일을 덧붙여 쓰기 형태로 연다.
sprintf(str,"EXIT SIG : %s\n", strsignal(sig)); // EXIT SIG 문구를 출력하고 해당 sig 의 문구를 str에 입력한다.
fputs(str,fp); // str 의 문장을 fp가 가리키는 파일에 입력
fclose(fp);
printf("Program End !\n"); // 프로그램을 종료하는 문구를 출력
exit(0);
}
args[n] = NULL;
if(**args == '\0')
continue;
infile = NULL;
outfile = NULL;
for(cp=args; *cp != NULL; cp++)
{
if(strcmp(*cp, "<") == 0)
{
if(*(cp+1) == NULL)
{
fprintf(stderr, "You must specify ");
fprintf(stderr, "an input file.\n");
goto again;
}
*cp++ = NULL;
infile = *cp;
}
else if(strcmp(*cp, ">") == 0)
{
if(*(cp+1) == NULL)
{
fprintf(stderr, "You must specify ");
fprintf(stderr, "an output file.\n");
goto again;
}
*cp++ = NULL;
outfile = *cp;
}
}
status = execute(args, infile, outfile);
}
}
int execute(char **args, char *infile, char *outfile)
{
int status;
pid_t p, pid;
int infd, outfd;
extern int errno;
sigset_t mask, savemask;
struct sigaction ignore, saveint, savequit;
struct tms t; // tms 구조체 t를 선언
clock_t clock; // clock_t 자료형 clock 선언
infd = -1;
outfd = -1;
if(infile != NULL)
{
if((infd = open(infile, O_RDONLY)) < 0)
{
perror(infile);
return(-1);
}
}
if(outfile != NULL)
{
if((outfd = creat(outfile, 0666)) < 0)
{
perror(outfile);
close(infd);
return(-1);
}
}
sigemptyset(&ignore.sa_mask);
ignore.sa_handler = SIG_IGN;
ignore.sa_flags = 0;
sigaction(SIGINT, &ignore, &saveint);
sigaction(SIGQUIT, &ignore, &savequit);
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &mask, &savemask);
clock = times(&t); // fork함수가 실행되기 전에 times 함수를 호출
if((pid=fork()) < 0)
status = -1;
if(pid == 0)
{
sigaction(SIGINT, &saveint, (struct sigaction *) 0);
sigaction(SIGQUIT, &savequit, (struct sigaction *) 0);
sigprocmask(SIG_SETMASK, &savemask, (sigset_t *) 0);
if(infd > 0)
dup2(infd, 0);
if(outfd > 0)
dup2(outfd, 1);
execvp(*args, args);
perror("exec");
_exit(127);
}
while(waitpid(pid, &status, 0) < 0)
{
if(errno != EINTR)
{
status = -1;
break;
}
}
clock = times(&t); // wait 함수를 호출한 뒤에 times 함수를 호출
FILE *fp;
fp = fopen("log.txt","a"); // 프로세스 시간을 입력하기 위해 log.txt 파일을 연다
fprintf(fp,"Process Time : %f\n", (double)(t.tms_utime+t.tms_stime));
// 부모프로세스 시간을 입력한다.
fprintf(fp,"Children Process Time : %f\n", (double)t.tms_cutime+t.tms_cstime);
// 자식프로세스 시간을 입력한다.
fclose(fp); // 파일을 닫는다.
sigaction(SIGINT, &saveint, (struct sigaction *) 0);
sigaction(SIGQUIT, &savequit, (struct sigaction *) 0);
sigprocmask(SIG_SETMASK, &savemask, (sigset_t *) 0);
close(outfd);
close(infd);
return(status);
}
size_t bufsplit(char *buf, size_t n, char **a)
{
int i, nsplit;
static char *splitch = "\t\n";
if(buf != NULL && n == 0)
{
splitch = buf;
return(1);
}
nsplit = 0;
while(nsplit < n)
{
a[nsplit++] = buf;
if((buf = strpbrk(buf, splitch)) == NULL)
break;
*(buf++) = '\0';
if(*buf == '\0')
break;
}
buf = strrchr(a[nsplit-1], '\0');
for(i=nsplit; i
return(nsplit);
}
void handler(int sig) // 종료시 받게 되는 SIG 값을 log 파일에 저장하기 위한 handler
{
FILE *fp;
char str[80];
fp = fopen("log.txt", "a"); // log.txt 파일을 덧붙여 쓰기 형태로 연다.
sprintf(str,"EXIT SIG : %s\n", strsignal(sig)); // EXIT SIG 문구를 출력하고 해당 sig 의 문구를 str에 입력한다.
fputs(str,fp); // str 의 문장을 fp가 가리키는 파일에 입력
fclose(fp);
printf("Program End !\n"); // 프로그램을 종료하는 문구를 출력
exit(0);
}