파일락에 대해 코딩을 하고 있습니다. 문제점 조언을 해주십시오

heojaes의 이미지

제가 만들고 있는 파일락을 설정하는 부분을 코딩하고 있는데..
그부분만 따로 만들어서 올립니다.

main()
{
	char	test1[512], test2[512];
	int	i, ret = 0;

	strcpy(test1, "filelog1.txt");
	strcpy(test2, "filelog2.txt");

	for(i = 0 ; i < 2 ; i++)
	{
		/* 데이타 처리 루틴 */
		if( i == 0)
			ret = FileLock(test1);
		else if ( i == 1)
			ret = FileLock(test2);

	}

}

FileLock(char *filename)
{
	int	fd, ret = 0;
	FILE	*fp;
	struct flock stLock;
	char strReadBuff[512];
	fp = fopen(filename, "r");
	fd = fileno(fp);

	/* 파일에 락이 걸려있는지 검사 */
	if (fcntl(fd, F_GETLK, &stLock) == 0)
	{
		printf("Logfile %s is Locked!\n", filename);
		fclose(fp);
		return(-1);
	}
	/* file에 락을 건다. Read Lock 설정 값 */
	stLock.l_type = F_RDLCK;	/* 읽기 락 설정 */
	stLock.l_whence = 0;		/* 0:화일의처음, 1:현재의 위치, 2화일의 끝 */
	stLock.l_start = 0;		/* 잠금영역의 시작 위치*/
	stLock.l_len = 0;		/* 화일 주소 공간 전체 */
	stLock.l_pid = getpid();
	
	if (fcntl(fd, F_SETLKW, &stLock) == -1)
	{
		printf("Logfile %s is Lock Fail!\n", filename);
		fclose(fp);
		return(-1);
	}
	/* Data 처리 루틴 */
	while(fgets(strReadBuff, sizeof strReadBuff, fp) != NULL)
        {
		printf("Data[%s]\n", strReadBuff);
	}
	/* 파일의 Lock 을 푼다. */
	stLock.l_type = F_UNLCK;
	if (fcntl(fd, F_SETLKW, &stLock) == -1)
	{
		printf("Logfile %s is unLock Fail!\n", filename);
	}
	fclose(fp);

	return (ret);

}

위와 같이 코딩을 하고 있는데..
첫번째 test1 파일을 찾아서 락을 건후에 그내용 끝까지 읽은후에 락설정을 해제하고, 끝난후 다음파일인 test2를 읽어서 첫번째랑 같이 돌아가는데.
파일을 읽기전에 fcntl(fd, F_GETLK, &stLock) 함수로 락이 설정이 되어 있는지 검사를 하는데 두번째 파일로 넘어가서 읽을때는 락이 걸려있다고 나옵니다.
여러 책이나 예제 소스를 봐도 stLock.l_type = F_UNLCK; 으로 값셋팅후 fcntl(fd, F_SETLKW, &stLock)를 다시 하면 락이 풀린다고 나와 있는데 어떻게 해결을 해야 하는지 알려주십시오.
두파일이 모두 락이 설정이 되는것 같다는 생각이 듭니다.

한가지 더 물어볼것이 있는데..

txt읽는 프로그램을 2개의 프로세스(멀티프로세스)로 돌리려고 하는데

똑같이 동작하는 첫번째 프로세스에서 한 파일에 락을 걸고 있을때 두번째 프로세스를 기동하면 락검사를 하면 락이 안걸려 있다고 나오는데..
첫번째 읽은 것을 또 읽는 것을 막아 중복을 해결해야 되는데 파일 자체에 대해서는 락을 걸어 두번째 프로세스(프로그램)가 락이 걸린 파일을 못 읽게 하는 방법은 없습니까.

댓글 달기

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