디시인사이드 갤러리

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

갤러리 본문 영역

형들 이거 락이 쫌 이상해.

주시미갤로그로 이동합니다. 2010.11.16 16:30:27
조회 75 추천 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/03 - -
229939 오.. sk녀석들이 괜춚한걸 개발했다능.. [7] Finalizer갤로그로 이동합니다. 11.01.09 190 0
229938 할짓없다 [5] elwlwlwk갤로그로 이동합니다. 11.01.09 136 0
229937 형들 나 MFC 공부 중인데 조언 점 해주셍 [2] dd(211.169) 11.01.09 120 0
229936 진짜 네이버 구글 다 찾아봐도 안나와서 질문해요! [4] 진저브레드(121.190) 11.01.09 81 0
229934 Obj C 는 C에 가깝나 C++에 가깝나 C# 에 가깝나.. [9] ㅇㅂㅇ(119.71) 11.01.09 205 0
229933 C Primer Plus 이책도 깔끔하군 [2] 왁스맛치즈갤로그로 이동합니다. 11.01.09 139 0
229932 내가 아이폰스도쿠 늅늅E인데 파싱을 하라고함. [2] 얘들아(175.199) 11.01.09 76 0
229931 내가 디게 좋아하는 안드로이드 어플 [2] DMW(125.138) 11.01.09 167 0
229930 안드로이드 개발 좀 하려는데 [6] ㅁㄴㅇㅁㄴ(121.144) 11.01.09 148 0
229929 맥주 한잔하고 자야겠다 [4] DMW(125.138) 11.01.09 130 0
229928 서울에 시카고 피자집 오픈하면 성공하겠냐? [4] Finalizer갤로그로 이동합니다. 11.01.09 166 0
229927 아침에 눈을뜨면 [16] elwlwlwk갤로그로 이동합니다. 11.01.09 214 0
229926 모바일 응용 개발자도 임베디드 S/W 개발자라 할수 있을까? [2] 후로그래머(119.196) 11.01.09 176 0
229925 c++ 탄생비화 진실.txt [2] .3(203.223) 11.01.09 311 0
229924 변수명 앞에 f붙는건 뭐의 약자임..?? [5] 오오미(58.141) 11.01.09 145 0
229923 근데 c++탄생비화 그거 사실임? [4] 마타마타(125.176) 11.01.09 190 0
229922 님들 C언어 비쥬얼스튜디오말구 [2] 금고래갤로그로 이동합니다. 11.01.09 147 0
229921 조중통 서버 폭팔 홍어(121.151) 11.01.09 108 0
229920 남자인지 여자인지 알아보는 그게 [1] 왁스맛치즈갤로그로 이동합니다. 11.01.09 151 0
229919 형들 자바나 c 하려면 컴 사양 좋아야하나요? [5] 멍청돋(183.101) 11.01.09 162 0
229918 우와 갤로그는 이게 되네 [7] BTTTS!갤로그로 이동합니다. 11.01.09 159 0
229917 님들 메모장으로 .C만든다음에 컴파일할수있나여? [4] 금고래갤로그로 이동합니다. 11.01.09 191 0
229915 리눅스용 바이러스 현황에 대해 좀 아는 능력자 읍써? [5] 분당살람갤로그로 이동합니다. 11.01.09 134 0
229914 형들 면접볼때 말하는거말이에요 [1] ㅜㅠ(121.162) 11.01.09 92 0
229913 컴퓨터가 남자인지 여자인지 알아보자 횽들... [13] 대마법서오즈갤로그로 이동합니다. 11.01.09 293 0
229911 요즘 보면 IT 업체들을 평가하면서 [5] Rei@디씨갤로그로 이동합니다. 11.01.09 258 0
229910 이런건 대체 어떻게 하는거냐? [1] BTTTS!갤로그로 이동합니다. 11.01.09 75 0
229909 시중에 있는 c언어 서적은 전부 문법서적인데 [5] 쏭사리(121.147) 11.01.09 166 0
229908 역시 WD-40은 최고인거 같아 [8] Rei@디씨갤로그로 이동합니다. 11.01.09 178 0
229907 하드 C 298 D149 인데 어떻게나눌까? [3] BTTTS!갤로그로 이동합니다. 11.01.09 75 0
229906 제 도메인 폭팔해버렸네요 북한으로 추청되는 자들에게 디도스 당한듯 [2] tiztv(121.151) 11.01.09 128 0
229905 ms는 실직적으로 XP이후로 줄줄히 망하는중 Finalizer갤로그로 이동합니다. 11.01.09 85 0
229904 북한의 패배 [3] BTTTS!갤로그로 이동합니다. 11.01.09 210 0
229903 jsp입문책 하나 끝나가는데 다음에 어떤 책을 보면 좋을까요? [2] ㅇ.ㅇ(58.239) 11.01.09 119 0
229902 비쥬얼 c++ 2010깔려는데 [2] ㅇㅇㄹㄴㅇㄹ(112.165) 11.01.09 179 0
229901 애플 공동창업자 스티브 워즈니악은 지금 뭐하고 사나요? [4] 쏭사리(121.147) 11.01.09 162 0
229900 어디서 프로그램 하나 주워갖고 나대는 초딩해커의 최강 결말. [12] BTTTS!갤로그로 이동합니다. 11.01.09 1279 0
229899 혹시 엑셀 잘하는 분 있나염 [4] 서르갤로그로 이동합니다. 11.01.09 99 0
229897 임베 잘 몰라서 그러는데요 ㅜㅜ [6] [성대아싸]갤로그로 이동합니다. 11.01.09 181 0
229896 구글 사이트하고 구글 크롬은 어떤 프로그래밍 언어로 만든건가요? [8] 컴맹(163.180) 11.01.09 368 0
229894 아하하 부팅검사중에 취소했더니 시스템이 병신이 되버렸구만 젠장할 BTTTS!갤로그로 이동합니다. 11.01.09 485 0
229892 갑자기 진짜 문득호기심이...생김 [19] 나낼군대가염갤로그로 이동합니다. 11.01.09 287 0
229891 이거 어떤 verilog 시뮬레이터 프로그램인지좀 알려주세요. [1] 유동닉(203.152) 11.01.09 87 0
229890 사업(인터넷/컴퓨터관련 및 연구개발)을하실분께 사무실을 무료임대해드립니다 kijoco(125.128) 11.01.09 55 0
229889 cain + wireshark 사용 해킹 실습 .TXT [1] 시방새1갤로그로 이동합니다. 11.01.09 248 0
229888 형들 이런거 프로그래밍해도 괜찮을까? [3] ㄴㅇㄹ(121.140) 11.01.09 150 0
229887 framework와 library의 차이점좀 가르쳐주세요 [5] 곽일산(61.106) 11.01.09 122 0
229886 창설멤버를 모집합니다. [4] kijoco(125.128) 11.01.09 131 0
229885 사업(인터넷/컴퓨터관련 및 연구개발)을하실분께 사무실을 무료임대해드립니다 kijoco(125.128) 11.01.09 55 0
229884 자기 컴퓨터 고추인지 봊이인지 확인하는 방법 [9] 시그란♬갤로그로 이동합니다. 11.01.09 5022 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

뉴스

디시미디어

디시이슈

1/2