C언어와 유닉스 프로그램 언어가 같기에 질문을 드려봅니다.
의사양반
질문 제목 :C언어와 유닉스 프로그램 언어가 같기에 질문을 드려봅니다.
질문 요약 :3개의 프로그램을 하나로 합치고 싶어요질문 내용 :
말그대로 세개의 프로그램을 하나로 합치고 싶습니다. 소스는 있습니다. 합쳐서 하나로 만들어 주세요 ㅠ.ㅠ
쪽지로 보내주셔도 좋고 메일도 좋습니다. 그리고 합칠때 어떻게 합쳤는지도 알려주셨으면 좋겠습니다. ㅠ.ㅠ
1. fifo를 사용해 1:1대화가 가능하게
2. 타이머 설정을 해서 타임이 다되면 1/1대화가 종료 되게
3. 1/1프로그램의 프로세스 실행 시간 측정
이렇게 3가지 입니다.
제가 가지고 있는 소스는 1번 1/1대화는 완변하게 구현되었습니다. 2번은 그냥 타이머 설정 입니다. 3번은 단순히 times 함수를 이용해서 실행시간측정하는 소스입니다.
이 세가지를 연결 할수 있나요??? ㅠ.ㅠ
연결 하면 이렇게 구성이 되겠죠 1:1대화중에 타이머가 시간이 다되면 프로그램이 종료되고 해당 프로그램의 실행 시간을 측정도와주세요 ㅠ.ㅠ1번 대화가 가능하게 하는 소스 입니다.서버 소스
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include stdio.h
#include stdlib.h
#include unistd.h
main() {
int pd, pd1, n;
char msg[256];
if(mkfifo(./myfifo, 0666) == -1) {
perror(mkfifo);
exit(1);
}
if(mkfifo(./myfifo1, 0666) == -1) {
perror(mkfifo);
exit(1);
}
pd = open(./myfifo, O_WRONLY);
pd1 = open(./myfifo1, O_RDONLY);
if(pd == -1) {
perror(open);
exit(1);
}
while(1) {
printf(Server : );
gets(msg);
n = write(pd, msg, strlen(msg)+1);
if(n == -1) {
perror(write);
exit(1);
}
n = read(pd1, msg, 255);
printf(Client : %s\n, msg);
}
}
클라이언트 소스
#include sys/types.h
#include sys/stat.h
#include fcntl.h
돋움체#include stdio.h
#include stdlib.h
#include unistd.h
main() {
int pd, pd1, n;
char inmsg[256];
pd = open(./myfifo, O_RDONLY);
pd1 = open(./myfifo1, O_WRONLY);
if(pd == -1) {
perror(open);
exit(1);
}
printf(Client =====\n);
while(1) {
n=read(pd, inmsg, 255);
printf(Server : %s\n, inmsg);
printf(Client : );
gets(inmsg);
write(pd1, inmsg, strlen(inmsg)+1);
}
}
2번 타이머 설정 소스 입니다.
#include sys/time.h
#include unistd.h
#include signal.h
#include stdlib.h
#include stdio.h
void handler() {
printf(Timer Invoked..\n);
}
int main(void) {
struct itimerval it;
sigset(SIGALRM, handler);
it.it_value.tv_sec = 3;
it.it_value.tv_usec = 0;
it.it_interval.tv_sec = 2;
it.it_interval.tv_usec = 0;
if (setitimer(ITIMER_REAL, &it, (struct itimerval *)NULL) == -1) {
 sp; perror(setitimer);
exit(1);
}
while (1) {
if (getitimer(ITIMER_REAL, &it) == -1) {
perror(getitimer);
exit(1);
}
printf(%d sec, %d msec.\n, (int)it.it_value.tv_sec,
(int)it.it_value.tv_usec);
sleep(1);
}
return 0;
}
3. 1:1프로그램의 프로세스 실행 시간 측정 소스#include sys/types.h
#include sys/times.h
#include limits.h
#include unistd.h
#include stdlib.h
#include stdio.h
int main(void) {
int i;
time_t t;
struct tms mytms;
clock_t t1, t2;
if ((t1 = times(&mytms)) == -1) {
perror(times 1);
exit(1);
}
for (i = 0; i 999999; i++)
time(&t);
if ((t2 = times(&mytms)) == -1) {
perror(times 2);
exit(1);
}
printf(Real time : %.1f sec\n, (double)(t2 - t1) / CLK_TCK);
printf(User time : %.1f sec\n, (double)mytms.tms_utime / CLK_TCK);
printf(System time : %.1f sec\n, (double)mytms.tms_stime / CLK_TCK);
return 0;
}