file lock을 쓰레드에서는 쓸수 없나요?

익명 사용자의 이미지

다음과 같은 소스에서

#include
#include
#include
#include
#include

void *do_write(void *data)
{
int fp;
struct flock lType;

fp = open("./test.txt", O_RDWR | O_CREAT);

lType.l_type = F_WRLCK;
lType.l_whence = SEEK_SET;
lType.l_start = 2;
lType.l_len = 2;

fcntl(fp, F_SETLKW, lType);
printf("1 is lock now!!\n");
sleep(4);

lType.l_type = F_UNLCK;
fcntl(fp, F_SETLKW, lType);
printf("1 is unlock now!!\n");
sleep(5);

}

void *do_read(void *data)
{
int fp;
struct flock lType;

fp = open("./test.txt", O_RDWR | O_CREAT);

sleep(2);

lType.l_type = F_WRLCK;
lType.l_whence = SEEK_SET;
lType.l_start = 2;
lType.l_len = 2;

printf("2 is try lock now!!\n");
fcntl(fp, F_SETLKW, lType);
printf("2 is get lock now!!\n");
sleep(2);

lType.l_type = F_UNLCK;
fcntl(fp, F_SETLKW, lType);
printf("2 is unlock now!!\n");
sleep(5);

}

int main()
{
pthread_t p_thread[5];
int thr_id;
int status;
int a = 1;
int b = 2;
FILE *fp;

char arr[10];

fp = fopen("./test.txt", "w");
fprintf(fp, "123456789");
fclose(fp);

thr_id = pthread_create(&p_thread[0], NULL, do_write, (void *)&a);
thr_id = pthread_create(&p_thread[1], NULL, do_read, (void *)&b);

pthread_join(p_thread[0], (void **) status);
pthread_join(p_thread[1], (void **) status);

return 0;
}

컴파일후 실행을 하니
$ ./a.out
1 is lock now!!
2 is try lock now!!
2 is get lock now!!
1 is unlock now!!
2 is unlock now!!
$

위와 같은 결과가 나옵니다..

저의 생각으로는
1 is lock now!!
2 is try lock now!!
1 is unlock now!!
2 is get lock now!!
2 is unlock now!!

이렇게 나와야 할것 같거든요....

뭐가 잘못된건지.. 아니면 쓰레드에서는 file lock을 쓸수 없는건가요?

익명 사용자의 이미지

man 페이지를 보면..

fcntl - 파일 기술자(file descriptor) 조작

사용법
#include
#include

int fcntl(int fd, int cmd);
int fcntl(int fd, int cmd, long arg);
int fcntl(int fd, int cmd, struct flock * lock);

이렇습니다..

int fcntl(int fd, int cmd, struct flock * lock);
에서 3번째 인자를 넘길때... 포인터를 넘겨야 하죠..
fcntl(fp, F_SETLKW, lType); 를

fcntl(fp, F_SETLKW, &lType); 이렇게 바꿔야 겠죠..
그리고 잠금 같은 경우엔...
rtn = fcntl(fp, F_SETLKW, &lType);
식으로 해서 rtn값을 체크할 필요가 있습니다..

이것은 여담이지만.. 잠금의 빠르기를 벤치마크한 경우가 있는데.

AcceptMutex
Introduced in Apache 1.3.21 and in Apache 2.0, the AcceptMutex
directive reveals a significant opportunity for performance tuning.
This directive allows configuration at runtime of how Apache will
tune accept() handling. On some systems with only a single listener,
accept locking is not required. This strategy is called Single
Listen Unserialized Accept (SLUA). However, for those configurations
that have multiple listeners or operating systems that have a
thundering herd on the accept system call (no matter the number of
listeners), the accept routines must be serialized.

Sander Temme of Covalent did some performance analysis of the accept
() locking strategies. This report led to the current ordering in
Apache 1.3.21 as listed here

Irix's uslock (uslock)
POSIX cross-process locks (pthread)
SystemV Semaphores (sysvsem)
fcntl() locking (fcntl)
flock() locking (flock)
OS/2 Semaphores (os2sem)
TPF locking (tpfcore)
None

이런 순이랍니다...
pthread가 파일 락킹 보다는 빠르지요..;;

댓글 달기

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