peterson's solution 을 어떻게 사용하는지 모르겠네요...

kkw90111의 이미지

Peterson의 알고리즘으로 동기화를 하려고합니다!!
프로세스는 쓰레드를 2개 실행(pthread_create) 시키고 함수를 실행 시킵니다.
그리고 이 함수내에서 CS이 생깁니다.

제가 배운대로라면

enter_region(process)
Critical Section
leave_region(process)

이렇게만 짜면 되는건데 대체 process 값은 어떻게 불러오는 것인지요....
값이 0 또는 1 인것은 아는데 쓰레드에서 그 값을 어떻게 지정해주는 것인지 모르겠네요...

간단한 프로그램 코드 첨부합니다만, 이건 코드문제가 아니라 쓰레드에서 어떻게 현재 수행되고있는 프로세스 넘버를 가져오냐의 문제 같네요 ㅠ
아! 그리고 다른방식의 피터슨 알고리즘 소스도 있는데 꼭 이함수로 구현하고싶습니다!!

//......
 
int main(void)                            // 프로그램의 본체
{
        int i = 0;
        void *tret;     
 
        do {
                printf("%d\n",i);
                pthread_create(&id1, NULL, func1, NULL);                 // 이 쓰레드의 func1에서 CS이 발견
                pthread_create(&id1, NULL, func2, NULL);                 // 이 쓰레드의 func2에서 CS이 발견
 
                pthread_join(id1, &tret);
                pthread_join(id1, &tret);
 
                i++;
        }while( i == 1000);
}
 
......
 
void func2(...) {             
        enter_region(process);
          < CS >
        relave_region(process);
}
 
void func2(...) {           
        enter_region(process);
          < CS >
        relave_region(process);
}
 
.....//
 
 
///* Peterson's algorithm *///
 
void enter_region(int process) {                   // process 값을 쓰레드에게서 받아야 하는데 알 수가 없다.
        int other = 1 - process;
 
        interested[process] = TRUE;
        turn = process;
        while(turn == process && interested[other] == TRUE);
}
 
void leave_region(int process) {                
        interested[process] = FALSE;
}
jick의 이미지

Peterson의 알고리즘이나 기타 read/write만을 사용하는 거의 모든 알고리즘은, 프로세서가 각각의 read/write 명령을 순차적으로 처리하고 모든 프로세서가 같은 순서로 처리되는 read/write를 보게 된다고 가정하고 있습니다. 하지만 오늘날 프로세서에선 이런 가정이 통하지 않습니다.

다시 말하자면: Peterson의 알고리즘을 오늘날 프로세서에서 돌리면 제대로 된 동작을 보장 못합니다.

안타까우시겠지만 그냥 pthread_mutex_lock 쓰세요.

kkw90111의 이미지

동작이 되지 않더라도 한번 시험해보고 싶었는데... 혹시 사용법 알수없나요?
몇일째 고민해봐도 process값을 어떻게 받아낼지 모르겠네요....

jick의 이미지

pthread_create를 할 때 인자로 "process id"를 넘겨주면 되지요.

pthread_create(&ptid1, NULL, func, (void *) 0);
pthread_create(&ptid2, NULL, func, (void *) 1);
 
...
 
void *func(void *arg)
{
  if ((int) arg == 0)
    // 내가 process 0
  else
    // 내가 process 1
  ......
}

댓글 달기

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