#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#define MAXLINE 16 /*the wide of matrix*/
#define GHOST_NUM 100 /*quantity of ghost*/
pthread_mutex_t work_mutex;
int ghost[GHOST_NUM];
pthread_t ghost_thread[GHOST_NUM];
pthread_t man_thread;
void *ghost_thread_result[GHOST_NUM];
void *man_thread_result;
int ghost_x[GHOST_NUM] = {MAXLINE/2};
int ghost_y[GHOST_NUM] = {MAXLINE/2};
int running_flag = 1;
int a=1;
int b=1;
void *GhostPosition(void *arg);
void *ManPosition(void *arg);
/*a function used to judge the man is across the ghosts*/
int JudgePosition(int number) /*judge the position of ghost */
{
if(a == ghost_x[number] && b==ghost_y[number] )
return 0;
return 1;
}
/*function used to support a way to ghost */
void GhostMove(int number)
{
ghost_x[number] = rand()%MAXLINE;
ghost_y[number] = rand()%MAXLINE;
}
int JudgeSuccess(void)
{
if((a>=MAXLINE-1) && (b>=MAXLINE-1)) {
printf("the man is success to get out!/n");
running_flag = 0;
return 1;
} else {
return 0;
}
}
/*man's move way*/
void ManMove(void)
{
int step;
step = rand()%2;
if(step ) {
if( a<MAXLINE-1 ) {
a+=1;
} else {
b+=1;
}
} else {
if(b<MAXLINE-1) {
b+=1;
} else {
a+=1;
}
}
}
void *GhostPosition(void *arg)
{
while(running_flag) {
pthread_mutex_lock(&work_mutex);
if(JudgeSuccess()) {
break;
}
if(JudgePosition(*(int*)arg)) {
printf("before mve:the no. %d ghost's position is %d/t%d/n",*(int*)arg,ghost_x[*(int*)arg],ghost_y[*(int*)arg]);
GhostMove(*(int*)arg);
printf("after move:the no. %d ghost's position is %d/t%d/n",*(int*)arg,ghost_x[*(int*)arg],ghost_y[*(int*)arg]);
} else {
printf("the man have been killed by no. %d ghost!/n",*(int*)arg);
printf("the man's position is %d/t%d/n",a,b);
printf("the no. %d ghost's position is %d/t%d/n",*(int*)arg,ghost_x[*(int*)arg],ghost_y[*(int*)arg]);
running_flag =0;
break;
}
pthread_mutex_unlock(&work_mutex);
usleep(20);
}
pthread_mutex_unlock(&work_mutex);
pthread_exit(NULL);
}
int JudgeManPosition(void)
{
int i=0;
while(i++<GHOST_NUM) {
if(JudgePosition(i)) {
continue;
} else {
printf("the man have been killed!/n");
return 0;
}
}
return 1;
}
void *ManPosition(void *arg)
{
while(running_flag) {
pthread_mutex_lock(&work_mutex);
if(JudgeSuccess()) {
break;
}
if(JudgeManPosition()) {
printf("before move:the man's position is %d/t%d/n",a,b);
ManMove();
printf("after move:the man's position is %d/t%d/n",a,b);
} else {
printf("the man have been killed by himself!/n");
printf("the man's position is %d/t%d/n",a,b);
running_flag = 0;
break;
}
pthread_mutex_unlock(&work_mutex);
usleep(20);
}
pthread_mutex_unlock(&work_mutex);
pthread_exit(NULL);
}
void Thread_Creat(void *arg)
{
int res;
int i;
void GhostPosition(void *arg);
void ManPosition(void *arg);
res = pthread_create(&man_thread,NULL,ManPosition,NULL);
if (res != 0) {
perror("Ghost Thread creation failed");
exit(EXIT_FAILURE);
}else {
printf("the man thread creation success!/n");
}
for(i=0;i<GHOST_NUM;i++) {
ghost[i] = i;
res = pthread_create(&ghost_thread[i],NULL,GhostPosition,(void *)&ghost[i]);
if (res != 0) {
perror("Ghost Thread creation failed");
exit(EXIT_FAILURE);
} else {
printf("the no. %d ghost thread creation success!/n",i);
}
}
}
void Thread_Join(void)
{
int res;
int i;
for(i=0;i<GHOST_NUM;i++) {
res = pthread_join(ghost_thread[i], &ghost_thread_result[i]);
if (res != 0) {
perror("Ghost Thread join failed");
exit(EXIT_FAILURE);
}
}
printf("Thread joined/n");
res = pthread_join(man_thread,&man_thread_result);
if (res != 0) {
perror("Ghost Thread join failed");
exit(EXIT_FAILURE);
}
pthread_mutex_destroy(&work_mutex);
exit(EXIT_SUCCESS);
}
/* a function used to judge the man whether get out */
int main()
{
int res;
res = pthread_mutex_init(&work_mutex, NULL);
if (res != 0) {
perror("Mutex initialization failed");
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&work_mutex);
Thread_Creat(NULL);
pthread_mutex_unlock(&work_mutex);
printf("Waiting for thread to finish.../n");
Thread_Join();
}