목차
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);
}
추천자료
haskell로 구현한 계산기
중소기업에서 ASP의 개념적 이해와 활성화 방안
나이키 아웃소싱전략 분석
[데이터베이스산업][DB산업]데이터베이스(DB)의 정의와 특성, 데이터베이스(DB) 시스템, 데이...
표준의 정의, 표준의 구분 고찰과 표준화의 중요성, 표준화 관련 주요기구, 표준화의 시장확...
[생산관리] ERP의 구조와 유연성 및 확장가능성, 시스템의 선정방식
국제경영 - 해외 아웃소싱 전략 성공 사례 (나이키)
애플 앱스토어 (APP STORE) 완벽정리 (개요, 정의, 장단점, 효과)
애플 앱스토어 (APP STORE) 완벽정리 (개요, 정의, 장단점, 효과)
BM특허(비즈니스모델특허)의 등장과 외국현황, BM특허(비즈니스모델특허)의 대상과 요건, BM...
컴퓨터 네트워크 보안 확보를 위한 접근통제
[아동건강교육 B형] 영유아를 위한 영양교육 내용을 계획하시오 - 영양교육