질문&답변
클라우드/리눅스에 관한 질문과 답변을 주고 받는 곳입니다.
리눅스 분류

안녕하세요 세마포어(semaphore) 관련해서 꼭 한 번만 도와주세요ㅠㅠ

작성자 정보

  • 도리뽕 작성
  • 작성일

컨텐츠 정보

본문

안녕하세요. 운영체제를 수강중인 학부생입니다.

다른게 아니라 광제가 하나 나왔는데요. 코드부분을 먼저 올려드리자면...

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>

#include <pthread.h>
#include "specs.h"

#include <semaphore.h>

#define LOCAL_TO_CURRENT_THREAD 0

sem_t display_mutex;

int right[ POSN_EACH_DIRECTION ];
int left[ POSN_EACH_DIRECTION ];

#define TRUE 1
#define FALSE (! TRUE)

int done = FALSE;
int num_cars;
int cars_out_right = 0;
int cars_out_left = 0;

void my_sleep() {
 usleep( 100000 );
}

void *enter_right(void *ptr) {
 int i;
 for( i = 0; i < num_cars; ++i ) {
  int car = floor( drand48 () * MAX_COLOURS );
  right [0] = car;
  sleep( 1 );
 }
 pthread_exit( NULL );
}

void *leave_right(void *ptr) {
 while( ! done ) {
  if( right[POSN_EACH_DIRECTION -1] != white ) {
   right[POSN_EACH_DIRECTION -1] = white;
   cars_out_right = cars_out_right + 1;
   printf("cars_out_right value : %d ", cars_out_right);
  }

  sem_wait( & display_mutex );
  update_display ( right , left );
  sem_post( & display_mutex );

  my_sleep ();
 }
 pthread_exit( NULL );
}

void *enter_left ( void *ptr ) {
 int i;
 for( i = 0; i < num_cars; ++i ) {
  int car = floor( drand48 () * MAX_COLOURS );
  left[POSN_EACH_DIRECTION -1] = car;
  sleep( 1 );
 }
 pthread_exit( NULL );
}

void *leave_left ( void *ptr ) {
 while( ! done ) {
  if( left [0] != white ) {
   left [0] = white;
   cars_out_left = cars_out_left + 1;
   printf("cars_out_left value : %d ", cars_out_left);
  }

  sem_wait( & display_mutex );
  update_display ( right , left );
  sem_post( & display_mutex );

  my_sleep ();
 }
 pthread_exit( NULL );
}

void *move( void *ptr ) {
 int i;
 while( ! done ) {
  for( i = POSN_EACH_DIRECTION - 2; i >= 0; i-- ) {
   if( right[i + 1] == white && right[i] != white ) {
    right[i + 1] = right[i];
    right[i] = white;

    sem_wait( & display_mutex );
    update_display ( right , left );
    sem_post( & display_mutex );

    my_sleep ();
   }
  }
  for( i = 1; i < POSN_EACH_DIRECTION; ++i ) {
   if( left[i - 1] == white && left[i] != white ) {
    left[i - 1] = left[i];
    left[i] = white;

    sem_wait( & display_mutex );
    update_display ( right , left );
    sem_post( & display_mutex );

    my_sleep ();
   }
  }
 }
 pthread_exit( NULL );
}

int main( int argc , char* argv [] ) {
 int i;
 pthread_t move_thread , leaver_thread , leavel_thread;
 pthread_t enterr_thread , enterl_thread;

 sem_init( &display_mutex , LOCAL_TO_CURRENT_THREAD , 1 );

 if( argc != 2 ) {
  fprintf( stderr , "Usage: ./ bridge <number of cars > " );
  exit( 1 );
 }
 num_cars = atoi( argv [1] );

 srand48( 145789 );

 for( i = 0; i < POSN_EACH_DIRECTION; ++i ) {
  right[i] = white;
  left[i] = white;
 }

 initialise_display ();

 update_display ( right , left );
 my_sleep ();

 pthread_create ( &move_thread , NULL , move , NULL );
 pthread_create ( &leaver_thread , NULL , leave_right , NULL );
 pthread_create ( &leavel_thread , NULL , leave_left , NULL );
 pthread_create ( &enterr_thread , NULL , enter_right , NULL );
 pthread_create ( &enterl_thread , NULL , enter_left , NULL );

 pthread_join( enterr_thread , NULL );
 pthread_join( enterl_thread , NULL );

 while( num_cars != cars_out_right ) {
  printf( "Right: %d cars entered , %d left ", num_cars , cars_out_right );
  sleep( 1 );
 }
 printf( "Right: %d cars entered , %d left ", num_cars , cars_out_right );

 while( num_cars != cars_out_left ) {
  printf( "Left: %d cars entered , %d left ", num_cars , cars_out_left );
  sleep( 1 );
 }
 printf( "Left: %d cars entered , %d left ", num_cars , cars_out_left );

 done = TRUE;

 close_display ();

 return 0;
}

위의 코드는 bridge.c 파일입니다.

이 프로그램을 실행해보면 문제점(차량이 중간에 없어짐!!!)이 발생하는것을 알수 있는데요

최종적으로 교수님께서 새로운 세마포어를 사용하면 위 프로그램에서 발생하는 문제점을

해결할수 있다고 하셨는데요. 전혀 보이지가 않고 있어서 코드가 길고 해서 고수님들께 죄송한 말씀

이지만 조금이라도 도움을 주셨으면 해서 이렇게 올려봅니다.

공유자원 때문에 문제가 발생을 하는데요.

새로운 세마포어를 어디에, 어떻게 추가를 시켜야 할까요?
(수정은 위의 코드만 수정을 하면 대지만 나머지 것들도 한 번 같이 봐주시면 대단히 감사드리겠습니다.)



 

관련자료

댓글 0
등록된 댓글이 없습니다.

공지사항


뉴스광장


  • 현재 회원수 :  60,133 명
  • 현재 강좌수 :  36,172 개
  • 현재 접속자 :  379 명