select함수에 대해서..

envy100의 이미지

select에 화일디스크립터를 넣어서 사용하려고 합니다.
그럼 소켓에서 사용하는거 처럼 추가된 내용이 있을때 반환해 주는건가요??
아니면 화일안에 내용이 있으면 무조건 반환하는건가요??

select에 화일을 사용하는 방법좀 알려주세요

stoneshim의 이미지

비슷한 내용에 대한 답변이 존재합니다.

http://bbs.kldp.org/viewtopic.php?t=2409&highlight=regular

참고하시고 추가 질문이 있으시면 해주세요.

우리 모두 리얼리스트가 되자. 그러나 가슴에 이룰 수 없는 꿈을 가지자

은빛연어의 이미지

위의 링크에 나와 있는 것에 대해 이해를 돕고자 code를 추가할께요~
아시는것처럼 select에 file fd를 read로 걸면 select를 호출할때마다 곧바로 리턴되어집니다. 따라서 fd를 read했을때 리턴값(read한 크기)이 0인 경우, 더이상 읽을 것이 없다는것을 의미합니다. 이경우 1초 쉬었다가 다시 select로 올라가게 되는데 마찬가지 select는 곧바로 리턴되죠.. 그러면 또다시 read해서 ret가 0이므로 1초쉬고.. 여기서 만약에 test.log를 열어서 마지막에 어떤것을 추가하면 추가된 것이 read되겟쬬.. ^^

tail의 방법을 select로 만들어 봤습니다~~ 어떤것이든 방법의 차이가 아닐까요??? ^^*

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/socket.h>

#define	TRUE	1


main(int argc, char *argv[])
{
	int	fd, ret;
	fd_set	fdSet;
	char	buffer;


	if ((fd = open("./test.log", O_RDONLY)) < 0) {
		printf("> test.log open error.\n");
		exit(0);
	}

	while(TRUE) {
		FD_ZERO(&fdSet);
		FD_SET(fd, &fdSet);

		if (select(fd+1, &fdSet, NULL, NULL, NULL) <= 0) {
			printf("select error...(errno=%d)\n", errno);
			close(fd);
			exit(0);
		}

		if (FD_ISSET(fd, &fdSet)) {
			ret = read(fd, &buffer, 1);
			switch(ret) {
				case 0:
					sleep(1);
					break;
				case 1:
					printf("%c", buffer);
					break;
				default:
					printf("read error(errno=%d)\n", errno);
					close(fd);
					exit(0);
			}
		}
	}/*while*/
	
}
envy100의 이미지

제가 찾는 내용이 있었네요
제가 좀 더 찾아보고 올렸어야 하는데 감사함니다. ^^
좋은 주말 보내세요

도전하라

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.