데드락(deadlock) 발생 테스트

익명 사용자의 이미지

데드락 deadlock
세개의 프로세스 pa.c pb.c pc.c 를
그대로 옮기시고,
touch로 빈파일 a ,b ,c 를 만드신후
pa &
pb &
pc &
이렇게 실행하시면 됩니다.
수행결과
첫번째결과
pa.c fcntl write lock Success for file a
pb.c fcntl write lock Success for file b
pc.c fcntl write lock Success for file c
/goodday>deadlock pc.c
pc.c fcntl read lock try Deadlock situation detected/avoided
pc.c fcntl read lock Success for file a
Finished pb.c fcntl read lock Success for file pc.c
c
Finished pa.c fcntl read lock Success for file b
pb.c
Finished pa.c

두번째 결과
pa.c fcntl write lock Success for file a
pb.c fcntl write lock Success for file b
pc.c fcntl write lock Success for file c
/goodday>deadlock pa.c
pa.c fcntl read lock try Deadlock situation detected/avoided
pa.c fcntl read lock Success for file b
Finished pa.c
pc.c fcntl read lock Success for file a
Finished pb.c fcntl read lock Success for file pc.c
c
Finished pb.c

------pa.c

#include
#include
#include
#include
#include
#include
#include
#define TARGETFILE "a"
#define SIDETARGET "b"

void fatal(int result , char *title);
void mysig(int signo);
int main( int argc , char *argv[] )
{
int fd, i, nwrite , res;
char buf[200];
static struct sigaction act;
struct flock ldata;

act.sa_handler = mysig;
sigaction( SIGALRM , &act , 0 );
alarm(2);
fd = open( TARGETFILE , O_RDWR );
fatal( fd , "pa.c open" );
/* file write locking */
ldata.l_type = F_WRLCK;
ldata.l_whence = SEEK_SET;
ldata.l_start = 0;
ldata.l_len = 0;
res = fcntl( fd , F_SETLKW , &ldata ); /* write lock */
fatal( res , "pa.c fcntl write lock try");
fprintf( stderr , "pa.c fcntl write lock Success for file %
s\n" , TARGETFILE );
for ( i = 0 ; i < 500000; i++ )
{
sprintf( buf , "%07d\n" , i );
nwrite = write ( fd , buf , strlen(buf) );
fatal(nwrite , "pa.c write");
}
close ( fd );
fprintf( stderr , "Finished %s\n" , __FILE__ );
return 0;
}

void fatal(int result , char *title)
{
if ( result == -1 )
{
if ( errno == EDEADLK )
{
fprintf(stderr, "deadlock %s\n" , __FILE__ );
perror(title);
return ;
}
perror(title);
exit(1);
}
}

void mysig(int signo)
{
int fd , res , nread;
char buf[20+1];
struct flock ldata;
if (signo == SIGALRM)
{
//fprintf( stderr , "Sigalarm received\n");
fd = open( SIDETARGET , O_RDWR );
fatal( fd , "pa.c open 2" );
/* file write locking */
ldata.l_type = F_RDLCK;
ldata.l_whence = SEEK_SET;
ldata.l_start = 0;
ldata.l_len = 0;
res = fcntl( fd , F_SETLKW , &ldata ); /* write
lock */
fatal( res , "pa.c fcntl read lock try");
fprintf( stderr , "pa.c fcntl read lock Success
for file %s\n" , SIDETARGET);
memset ( buf , 0 , sizeof buf );
nread = read ( fd , buf , sizeof buf-1 );
fatal( nread , "pa.c fcntl read");
}
}

--------pb.c

#include
#include
#include
#include
#include
#include
#include
#define TARGETFILE "b"
#define SIDETARGET "c"

void fatal(int result , char *title);
void mysig(int signo);
int main( int argc , char *argv[] )
{
int fd, i, nwrite , res;
char buf[200];
static struct sigaction act;
struct flock ldata;

act.sa_handler = mysig;
sigaction( SIGALRM , &act , 0 );
alarm(2);
fd = open( TARGETFILE , O_RDWR );
fatal( fd , "pb.c open" );
/* file write locking */
ldata.l_type = F_WRLCK;
ldata.l_whence = SEEK_SET;
ldata.l_start = 0;
ldata.l_len = 0;
res = fcntl( fd , F_SETLKW , &ldata ); /* write lock */
fatal( res , "pb.c fcntl write lock try");
fprintf( stderr , "pb.c fcntl write lock Success for file %
s\n" , TARGETFILE );
for ( i = 0 ; i < 500000; i++ )
{
sprintf( buf , "%07d\n" , i );
nwrite = write ( fd , buf , strlen(buf) );
fatal(nwrite , "pb.c write");
}
close ( fd );
fprintf( stderr , "Finished %s\n" , __FILE__ );
return 0;
}

void fatal(int result , char *title)
{
if ( result == -1 )
{
if ( errno == EDEADLK )
{
fprintf(stderr, "deadlock %s\n" , __FILE__ );
perror(title);
return ;
}
perror(title);
exit(1);
}
}

void mysig(int signo)
{
int fd , res , nread;
char buf[20+1];
struct flock ldata;
if (signo == SIGALRM)
{
//fprintf( stderr , "Sigalarm received\n");
fd = open( SIDETARGET , O_RDWR );
fatal( fd , "pb.c open 2" );
/* file write locking */
ldata.l_type = F_RDLCK;
ldata.l_whence = SEEK_SET;
ldata.l_start = 0;
ldata.l_len = 0;
res = fcntl( fd , F_SETLKW , &ldata ); /* write
lock */
fatal( res , "pb.c fcntl read lock try");
fprintf( stderr , "pb.c fcntl read lock Success
for file %s\n" , SIDETARGET);
memset ( buf , 0 , sizeof buf );
nread = read ( fd , buf , sizeof buf-1 );
fatal( nread , "pb.c fcntl read");
}
}

-----pc.c

#include
#include
#include
#include
#include
#include
#include
#define TARGETFILE "c"
#define SIDETARGET "a"

void fatal(int result , char *title);
void mysig(int signo);
int main( int argc , char *argv[] )
{
int fd, i, nwrite , res;
char buf[200];
static struct sigaction act;
struct flock ldata;

act.sa_handler = mysig;
sigaction( SIGALRM , &act , 0 );
alarm(2);
fd = open( TARGETFILE , O_RDWR );
fatal( fd , "pc.c open" );
/* file write locking */
ldata.l_type = F_WRLCK;
ldata.l_whence = SEEK_SET;
ldata.l_start = 0;
ldata.l_len = 0;
res = fcntl( fd , F_SETLKW , &ldata ); /* write lock */
fatal( res , "pc.c fcntl write lock try");
fprintf( stderr , "pc.c fcntl write lock Success for file %
s\n" , TARGETFILE );
for ( i = 0 ; i < 500000; i++ )
{
sprintf( buf , "%07d\n" , i );
nwrite = write ( fd , buf , strlen(buf) );
fatal(nwrite , "pc.c write");
}
close ( fd );
fprintf( stderr , "Finished %s\n" , __FILE__ );
return 0;
}

void fatal(int result , char *title)
{
if ( result == -1 )
{
if ( errno == EDEADLK )
{
fprintf(stderr, "deadlock %s\n" , __FILE__ );
perror(title);
return ;
}
perror(title);
exit(1);
}
}

void mysig(int signo)
{
int fd , res , nread;
char buf[20+1];
struct flock ldata;
if (signo == SIGALRM)
{
//fprintf( stderr , "Sigalarm received\n");
fd = open( SIDETARGET , O_RDWR );
fatal( fd , "pc.c open 2" );
/* file write locking */
ldata.l_type = F_RDLCK;
ldata.l_whence = SEEK_SET;
ldata.l_start = 0;
ldata.l_len = 0;
res = fcntl( fd , F_SETLKW , &ldata ); /* write
lock */
fatal( res , "pc.c fcntl read lock try");
fprintf( stderr , "pc.c fcntl read lock Success
for file %s\n" , SIDETARGET);
memset ( buf , 0 , sizeof buf );
nread = read ( fd , buf , sizeof buf-1 );
fatal( nread , "pc.c fcntl read");
}
}

댓글 달기

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