시간 관련 함수 사용하기

익명 사용자의 이미지

초당 3번 이상의 서비스를 요청하는 패킷이 들어올 때라는
조건을 만들기 위해 시간과 관련된 함수를 사용하려 하는데요.

프로그램이 udp형식이라 생각으로는
recvfrom을 1초에 3번이상 부르게 되면...되지 않을까 싶은데..

time에 관련된 함수는 어떻게 사용해야하는지 모르겠네요.

시간을 알아내는 함수들은 있었지만...어떻게 해야할지를 모르겠네요.
답변 좀 꼭 부탁드립니다.

익명 사용자의 이미지

gettimeofday 란 함수가 있습니다.
http//man.kldp.org/man/man2/gettimeofday.2.html

두가지 인터넷에 찾은 예제.
1) http//khdp.org/docs/trans_doc/phrack-51-11.txt

* connect()함수를 이용해서 닫힌 포트에 갔다가 돌아오는 시간을 계산한
다. 더 좋은 방법은 ping을 쓰면 된다. 이 ping은
방화벽까지 갈 수 있고 root권한을 가진 사용자가 isup()이라는 함수를 사
용해서 쓸 수 있다. */
unsigned long calculate_sleep(struct in_addr target) {
struct timeval begin, end;
int sd;
struct sockaddr_in sock;
int res;

if ((sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{perror("Socket troubles"); exit(1);}

sock.sin_family = AF_INET;
sock.sin_addr.s_addr = target.s_addr;
sock.sin_port = htons(MAGIC_PORT);

gettimeofday(&begin, NULL);
if ((res = connect(sd, (struct sockaddr *) &sock,
sizeof(struct sockaddr_in))) != -1)
printf("You might want to change MAGIC_PORT in the include file, it
seems to be listening on the target host!\n");
close(sd);
gettimeofday(&end, NULL);
if (end.tv_sec - begin.tv_sec > 5 ) /*uh-oh!*/
return 0;
return (end.tv_sec - begin.tv_sec) * 1000000 + (end.tv_usec -
begin.tv_usec);
}

2) http//www.bomulsun.com/c/tip/brd_view.html?no=50
1/10000초를 구하는 함수

UNIX에서 DB를 사용할 때 시간을 PK로 사용하는 경우가 많죠.
요즈음은 H/W 성능이 워낙 좋아져서 동일 시간(초)에 발생하는
Event가 많습니다. 그래서 더욱 세분화해서 System time을
구해야 할 필요가 있습니다.
그럴경우에 사용하면 좋은 함수를 하나 만들었죠.

void vMkCurTime ( buf )
char *buf;
{
struct timeval sCurTimeVal;
struct tm *sTm;

gettimeofday (&sCurTimeVal, NULL);

sTm = localtime(&sCurTimeVal.tv_sec);

sprintf ( buf, "%04d%02d%02d%02d%02d%02d%04d",
sTm->tm_year+1900, sTm->tm_mon+1, sTm->tm_mday,
sTm->tm_hour, sTm->tm_min, sTm->tm_sec,
sCurTimeVal.tv_usec/100);
}

위 함수 중에서 초단위 이하 시간은 "sCurTimeVal.tv_usec/100"를
통해서 구해지는 데요....
더 세밀한 시간이 필요한 경우에는 "sCurTimeVal.tv_usec"를 그냥
사용하면 됩니다.

그리고 타임 관련된 메크로
http//www.ezdoum.com/upload/time.h

리눅스와 Windows 2000 에서의 고성능 프로그래밍 기술
타이밍 루틴 (timing routine)
http//www-903.ibm.com/developerworks/kr/linux/library/l-rt1.html

THE GNU C LIBRARY REFERANCE MANUAL의 시간관련 부분
http//database.sarang.net/study/glibc/17.htm
(경과된 CPU 시간을 계산
절대시각이나 달력시간 계산
알람과 타이머 설정)

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.