디시인사이드 갤러리

갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

형들 이거 락이 쫌 이상해.

주시미갤로그로 이동합니다. 2010.11.16 16:30:27
조회 76 추천 0 댓글 0

처음엔 괜찮은데 2번째부터 한 방향만 계속 락을 안 놔줘 -_-;
락을 제대로 놓게 설정은 했는데...왜그러는지 좀 봐줄 수 있겠어?

메인함수는 그냥 초기화값만 설정하구 스레드 만들고 기다리니까 생략했구,
실질적으로는 요 함수가 기능을 하는데...

일단 락 안 걸린 놈들은 잘 실행하고
락을 돌려주는게 문제인 거 같은데, 뭐가 문제인지 모르겠네;

#include "hmonitor.h"

// 스레드 초기화

void thr_init(thr_arg *data, int direct[], pthread_mutex_t *des, pthread_cond_t **cptr)
{
 int  i;
 data->passed = 0;
 data->wait_time = (int **)malloc(sizeof(int)*MAXTIME);
 data->resource[0] = &direct[0];
 data->resource[1] = &direct[1];
 data->resource[2] = &direct[2];
 data->resource[3] = &direct[3];
 data->mutex = des;
 data->condptr = cptr;

 for(i=0; i<MAXTIME; i++)
 {
  data->wait_time[i] = (int *)malloc(sizeof(int));
  *(data->wait_time[i]) = 0;
 }
}

// 동쪽으로 가는 방향의 스레드 관리

void *eastbound(void *data)
{
 thr_arg  *east = (thr_arg *)data;
 int  timecount=0;
 
 while(timecount!=MAXTIME)
 {
//진입 시도
  eastbound_enter(east);
// 성공시 1초 대기
  timecount++;
  sleep(1);
//탈출
  eastbound_exit(east);
 }
}

void eastbound_enter(thr_arg *eptr)
{
// 이 놈은 3번과 0번을 가져야 함.
 static int passed=0;
 int  flag=1;

 pthread_mutex_lock(eptr->mutex);
 while(flag){
  flag=0;
// 3번 방향이 비어있으면
  if(*(eptr->resource[3]) == FREE)
  {
//차지하되
   *(eptr->resource[3]) = EAST;
  }
  else
  {
// 그렇지 않으면
   puts("[EASTBOUND] Waiting D Section");
   if(*(eptr->resource[0]) == EAST)
   {
// 0번을 갖고 있는 경우 돌려준다.
    *(eptr->resource[0]) = FREE;
   }
   pthread_cond_wait(eptr->condptr[3], eptr->mutex);
   flag=1;
   continue;
  }
// 0번을 갖되
  if(*(eptr->resource[0]) == FREE)
  {
   *(eptr->resource[0]) = EAST;
  }
  else
  {
// 갖지 못하면
   puts("[EASTBOUND] Waiting A Section");
// 3번도 마저 돌려준다.
   if(*(eptr->resource[3]) == EAST)
   {
    *(eptr->resource[3]) = FREE;
   }
   pthread_cond_wait(eptr->condptr[0], eptr->mutex);
   flag=1;
   continue;
  }
 }
 printf("[EASTBOUND] %dth car is now passing!\\n", ++passed);
 pthread_mutex_unlock(eptr->mutex);
}

void eastbound_exit(thr_arg *eptr)
{
 pthread_mutex_lock(eptr->mutex);
// 갖고 있던걸 놔 준다.
 *(eptr->resource[3]) = FREE;
 *(eptr->resource[0]) = FREE;
 pthread_mutex_unlock(eptr->mutex);
 pthread_cond_broadcast(eptr->condptr[3]);
 pthread_cond_broadcast(eptr->condptr[0]);
}

void westbound_enter(thr_arg *wptr)
{
//1번, 2번을 가져야 함.
 static int passed=0;
 int  flag=1;
 pthread_mutex_lock(wptr->mutex);
 while(flag)
 {
  flag=0;
  if(*(wptr->resource[1]) == FREE)
  {
   *(wptr->resource[1]) = WEST;
  }
  else
  {
   puts("[WESTBOUND] Waiting B Section");
   if(*(wptr->resource[2]) == WEST)
   {
    *(wptr->resource[2]) = FREE;
   }
   pthread_cond_wait(wptr->condptr[1], wptr->mutex);
   flag=1;
   continue;
  }
  if(*(wptr->resource[2]) == FREE)
  {
   *(wptr->resource[2]) = WEST;
  }
  else
  {
   puts("[WESTBOUND] Waiting C Section");
   if(*(wptr->resource[1]) == WEST)
   {
    *(wptr->resource[1]) = FREE;
   }
   pthread_cond_wait(wptr->condptr[2], wptr->mutex);
   flag=1;
   continue;
  }
 }
 printf("[WESTBOUND] %dth car is now passing!\\n", ++passed);
 pthread_mutex_unlock(wptr->mutex);
}

void westbound_exit(thr_arg *wptr)
{
 pthread_mutex_lock(wptr->mutex);
 *(wptr->resource[1]) = FREE;
 *(wptr->resource[2]) = FREE;
 pthread_mutex_unlock(wptr->mutex);
 pthread_cond_broadcast(wptr->condptr[1]);
 pthread_cond_broadcast(wptr->condptr[2]);
}

void *westbound(void *data)
{
        thr_arg         *west = (thr_arg *)data;
        int             timecount = 0;

 while(timecount!=MAXTIME)
 { 
  westbound_enter(west);
  timecount++;
  sleep(1);
  westbound_exit(west);
 }
}

void southbound_enter(thr_arg *sptr)
{
// 남쪽 방향으로 가며
// 2번 3번을 가져야 한다
 static int      passed=0;
 int             flag=1;
 pthread_mutex_lock(sptr->mutex);
 while(flag)
 {
  flag=0;
  printf("[SOUTHBOUND] : Before wait, C Section Number : %d\\n", *(sptr->resource[2]));
  printf("%d\\n", *(sptr->resource[2]));
  printf("%d\\n", *(sptr->resource[2]));
  if(*(sptr->resource[2]) == FREE)
  {
   *(sptr->resource[2]) = SOUTH;
  }
  else
  {
   puts("[SOUTHBOUND] Waiting C Section");
   if(*(sptr->resource[3]) == SOUTH)
   {
    *(sptr->resource[3]) = FREE;
   }
   pthread_cond_wait(sptr->condptr[2], sptr->mutex);
   printf("[SOUTHBOUND] Wake up, current C Section Number : %d\\n", *(sptr->resource[2]));
   flag=1;
   continue;
  }
  if(*(sptr->resource[3]) == FREE)
  {
   *(sptr->resource[3]) = SOUTH;
  }
  else
  {
   puts("[SOUTHBOUND] Waiting D Section");
   if(*(sptr->resource[2]) == SOUTH)
   {
    *(sptr->resource[2]) = FREE;
   } 
   pthread_cond_wait(sptr->condptr[3], sptr->mutex);
   flag=1;
   continue;
  }
 }
 printf("[SOUTHBOUND] %dth car is now passing!\\n", ++passed);
 pthread_mutex_unlock(sptr->mutex);
}

void southbound_exit(thr_arg *sptr)
{
         pthread_mutex_lock(sptr->mutex);
  *(sptr->resource[2]) = FREE;
  *(sptr->resource[3]) = FREE;
  pthread_mutex_unlock(sptr->mutex);
  pthread_cond_broadcast(sptr->condptr[2]);
  pthread_cond_broadcast(sptr->condptr[3]);
}

void *southbound(void *data)
{
 thr_arg         *south = (thr_arg *)data;
 int             timecount = 0;

 while(timecount!=MAXTIME)
 {
  southbound_enter(south);
  timecount++;
  sleep(1);
  southbound_exit(south);
 }
}

void northbound_enter(thr_arg *nptr)
{
//북쪽 방향으로 가야하며
//0번 1번을 가져야 한다.
         static int      passed=0;
  int             flag=1;
  pthread_mutex_lock(nptr->mutex);
  while(flag)
  {
   flag=0;
   if(*(nptr->resource[0]) == FREE)
   {
    *(nptr->resource[0]) = NORTH;
   }
   else
   {
    puts("[NORTHBOUND] Waiting A Section");
    if(*(nptr->resource[1]) == NORTH)
    {
     *(nptr->resource[1]) = FREE;
    }
    pthread_cond_wait(nptr->condptr[0], nptr->mutex);
    puts("[NORTHBOUND] --Debug : Condition Wake up");
    flag=1;
    continue;
   }
   if(*(nptr->resource[1]) == FREE)
   {
    *(nptr->resource[1]) = NORTH;
   }
   else
   {
    puts("[NORTHBOUND] Waiting B Section");
    if(*(nptr->resource[0]) == NORTH)
    {
     *(nptr->resource[0]) = FREE;
    }
    pthread_cond_wait(nptr->condptr[1], nptr->mutex);
    flag=1;
    continue;
   }
  }
  printf("[NORTHBOUND] %dth car is now passing!\\n", ++passed);
  pthread_mutex_unlock(nptr->mutex);
}

void northbound_exit(thr_arg *nptr)
{
 pthread_mutex_lock(nptr->mutex);
 *(nptr->resource[0]) = FREE;
 *(nptr->resource[1]) = FREE;
 pthread_mutex_unlock(nptr->mutex);
 pthread_cond_broadcast(nptr->condptr[0]);
 pthread_cond_broadcast(nptr->condptr[1]);
}

void *northbound(void *data)
{
 thr_arg         *north = (thr_arg *)data;
 int             timecount = 0;

 while(timecount!=MAXTIME)
 {
  northbound_enter(north);
  timecount++;
  sleep(1);
  northbound_exit(north);
 }
}

추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
등록순정렬 기준선택
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 힘들게 성공한 만큼 절대 논란 안 만들 것 같은 스타는? 운영자 24/06/10 - -
230481 마음울적한날엔.. [1] ㅁㄴㅇㄹ(175.214) 11.01.12 104 0
230480 아 휘밤 책다 살려니 돈 장난아니게 깨지네 [3] 속결자갤로그로 이동합니다. 11.01.12 97 0
230478 변수들이 protected되있는데 이것때문에 문제 발생할수도 있는거야?? [20] 아오빡쳐(115.145) 11.01.12 200 0
230477 헐 성희롱 논란의 주인공 강용석의원 프로필돋네 [4] 빕뱟뱟갤로그로 이동합니다. 11.01.12 247 1
230474 책사러 서점 갔다 왔다 [2] ㅇ-ㅇ(183.101) 11.01.12 106 0
230473 은행강도 횽은 봅니다. [11] 오라클닭(112.216) 11.01.12 96 0
230472 비기닝 오라클 프로그래밍 책을 다시 꺼내들었다.. [11] 오라클닭(112.216) 11.01.12 341 0
230471 퇴근시간이 다가오고 있다 ! [2] 2417(211.232) 11.01.12 58 0
230470 회사에서 모바일개발자 고용안하고. 맥도 없고 [3] 2417(211.232) 11.01.12 130 0
230469 횽뜰 오른쪾 쒸프트끼 씀? [9] 꾸루룽갤로그로 이동합니다. 11.01.12 133 1
230467 형들 요즘 Dday계산기 어플을 만드는데... [3] 다소미럭키갤로그로 이동합니다. 11.01.12 180 1
230466 네트워크나 서버 관리자를 하려는 횽들 [9] 은행강도갤로그로 이동합니다. 11.01.12 153 1
230465 자바 입문책 추천좀여;;; [3] ㄱㄱ(218.148) 11.01.12 341 0
230464 이진휩은 대체 어따 서먹는 거냐? [2] 속결자갤로그로 이동합니다. 11.01.12 65 0
230463 왜 나를 위한 봇은 없는가? [4] 속결자갤로그로 이동합니다. 11.01.12 112 0
230462 자바 개객끼 해봐. 자바 개객끼 [4] 초밥술사갤로그로 이동합니다. 11.01.12 177 0
230461 너희들이 powerbuilder 의 위대함을 아느냐..... [2] 호비뉴갤로그로 이동합니다. 11.01.12 118 0
230460 횽들 프로그램이 실행이 안되는데, 초 간단한 문제인가? 좀 봐죠 ㅠㅠ [7] 아오 빡쳐(115.145) 11.01.12 146 0
230459 CCNA, CCNP 3개월 과정 무료로 진행합니다. 5명 먼저 [3] 무료교육(59.5) 11.01.12 180 0
230458 부장이 해달라는 코딩 지금 거의한달째 안하고 있다.... 호비뉴갤로그로 이동합니다. 11.01.12 120 0
230457 김여사 드리프트 [7] Vita500갤로그로 이동합니다. 11.01.12 161 0
230456 횽들 이런 프로그램 쓰면 어떨꺼 같음? [7] 키젤갤로그로 이동합니다. 11.01.12 181 0
230454 맥북사면 [3] ㅁㅁ(220.117) 11.01.12 146 0
230453 책은 역시 성의가 중요하지..열강 [1] ㅁㅁㅁㅁ(182.208) 11.01.12 101 0
230449 나두 이런곳에서 살고싶다 [1] Rei@디씨갤로그로 이동합니다. 11.01.12 128 0
230448 IT 하도급을 법으로 원청봉쇄 시키자. [2] 속결자갤로그로 이동합니다. 11.01.12 174 0
230446 스키장을 가자 [4] Vita500갤로그로 이동합니다. 11.01.12 105 0
230445 횽들 파이썬 이 에러는 머임?? [11] 파이썬입문 (211.110) 11.01.12 171 0
230443 삼성/엘지/sk 등 전화기 제조사 마다 한글 입력 키패드 어디 있나요? [1] iljeomobolt갤로그로 이동합니다. 11.01.12 177 0
230442 후 내친구썌기 문제에요 형들 [9] 전문고3(221.155) 11.01.12 181 0
230441 앗 글삭제됐당 .. 으흑 ㅠㅠ ㅋㅋ(121.66) 11.01.12 51 0
230440 언니야들. 문자가 왔는대. [6] iljeomobolt갤로그로 이동합니다. 11.01.12 359 0
230438 it 업계쪽 문제가 어중간하게 비슷비슷한 실력을 가진 사람이 많다는건가? [4] ㅁㄴㅇㄹ(121.137) 11.01.12 195 0
230437 흐음... ㅋㅋ(121.66) 11.01.12 46 0
230436 시장 원리 [6] 천회장(211.45) 11.01.12 127 0
230435 얼마나 걸리려나 [2] 천회장(211.45) 11.01.12 59 0
230434 Network 을 정복하기 위하여는.. [2] iljeomobolt갤로그로 이동합니다. 11.01.12 101 0
230433 기업에서 개인용 알집 쓰는거 불법아니냐는 ?? [10] 2417(211.232) 11.01.12 266 0
230431 형님들 C 끝내고 뭐 공부해야하나요? [6] 좆뉴비(219.249) 11.01.12 183 0
230430 겨울방학때 네트워킹 정복하고싶다 [7] 영광의비석갤로그로 이동합니다. 11.01.12 160 0
230429 프로그래머하다짤리면말이죠. [5] 디버깅햄버거(119.195) 11.01.12 301 0
230428 나도 이번주말 스키장고고씽~ [2] 씨발라드세요갤로그로 이동합니다. 11.01.12 52 0
230427 아오 오전부터 안되던 문제가 풀렸다 [3] 꿀레갤로그로 이동합니다. 11.01.12 79 0
230425 내 폰 배경사진 [39] 개쉛기갤로그로 이동합니다. 11.01.12 390 0
230424 소니놋북샀다 ㅋㅋ [4] .3(124.137) 11.01.12 113 0
230420 머리가아파서 도저히 공부를 못하겠어서 한숨잤숨미다 [1] 꿀레갤로그로 이동합니다. 11.01.12 80 0
230419 어플만들려면 필요한 언어가 뭐야? [8] ㅁㅁ(220.117) 11.01.12 334 0
230418 지방대 컴공은 눈앞이 깜깜.. [4] 1234567890(121.165) 11.01.12 727 0
230417 여러분, 이렇게 살면 곤란합니다. [2] 땡칠도사갤로그로 이동합니다. 11.01.12 202 0
230416 다람쥐v 횽은 봅니다. [1] 해커닭(112.216) 11.01.12 102 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2