#include
#include
#include
#include
#include
#include
#include
#include
#define NUM_THREADS 6
#define ID_BASE 101
#define CHAIR_COUNT 3
#define STUDENT_COUNT 25
#define MAX_MEETING_DURATION 5
#define OFFICE_HOUR_DURATION 60
#define BSIZE 4
#define NUMITEMS 30
typedef struct {
char buf[BSIZE];
int occupied;
int nextin, nextout;
pthread_mutex_t mutex;
pthread_cond_t more;
pthread_cond_t less;
} buffer_t;
buffer_t buffer;
#define NUM_THREADS2 2
pthread_t tid[NUM_THREADS2]; /* array of thread IDs */
#define NTHREADS 4
#define N 1000
#define MEGEXTRA 1000000
pthread_attr_t attr;
#define NUM_THREADS5 8
char *messages[NUM_THREADS5];
//-------------------------------------------------------------------------
/*进程1的线程*/
//--------------------------------------------------------------------------
int chairs[CHAIR_COUNT]; // circular buffer of chairs
pthread_mutex_t chairMutex; // mutex protects chairs and wait count
pthread_mutex_t printMutex; // mutex protects printing
sem_t filledChairs; // professor waits on this semaphore
struct itimerval profTimer; // professor's office hour timer
time_t startTime;
int in = 0, out = 0;
int meetingId = 0;
int arrivalsCount = 0;
int waitCount = 0;
int leavesCount = 0;
int meetingsCount = 0;
int parforeCount = 0;
int firstPrint = 1;
// Print a line for each event:
// elapsed time
// who is meeting with the professor
// who is waiting in the chairs
// what event occurred
void print(char *event)
{
time_t now;
time(&now);
double elapsed = difftime(now, startTime);
int min = 0;
int sec = (int) elapsed;
if (sec >= 60) {
min++;
sec -= 60;
}
// Acquire the mutex lock to protect the printing.
pthread_mutex_lock(&printMutex);
if (firstPrint) {
printf("TIME | MEETING | WAITING | EVENT
");
firstPrint = 0;
}
// Elapsed time.
printf("%1d:%02d | ", min, sec);
// Who's meeting with the professor.
if (meetingId > 0) {
printf("%5d |", meetingId);
}
else {
printf(" |");
}
// Acquire the mutex lock to protect the chairs and the wait count.
pthread_mutex_lock(&chairMutex);
int i = out;
int j = waitCount;
int k = 0;
// Who's waiting in the chairs.
while (j-- > 0) {
printf("%4d", chairs[i]);
i = (i+1)%CHAIR_COUNT;
k++;
}
// Release the mutex lock.
pthread_mutex_unlock(&chairMutex);
// What event occurred.
while (k++ < CHAIR_COUNT) printf(" ");
printf(" | %s
", event);
// Release the mutex lock.
pthread_mutex_unlock(&printMutex);
}
// A student arrives.
void studentArrives(int id)
{
char event[80];
arrivalsCount++;
if (waitCount < CHAIR_COUNT) {
// Acquire the mutex lock to protect the chairs and the wait count.
pthread_mutex_lock(&chairMutex);
// Seat a student into a chair.
chairs[in] = id;
in = (in+1)%CHAIR_COUNT;
waitCount++;
// Release the mutex lock.
pthread_mutex_unlock(&chairMutex);
sprintf(event, "Student %d arrives and waits", id);
print(event);
// Signal the "filledSlots" semaphore.
sem_post(&filledChairs); // signal
}
else {
leavesCount++;
sprintf(event, "Student %d arrives and leaves", id);
print(event);
}
}
// The student thread.
void *student(void *param)
{
int id = *((int *) param);
// Students will arrive at random times during the office hour.
sleep(rand()%OFFICE_HOUR_DURATION);
studentArrives(id);
return NULL;
}
int timesUp = 0; // 1 = office hour is over
// The professor meets a student (or works on ParFore).
void professorMeetsStudent()
{
// No student waiting, so work on ParFore language.
if (waitCount == 0) {
print("Professor works on ParFore");
parforeCount++;
}
if (!timesUp) {
// Wait on the "filledChairs" semaphore for a student.
sem_wait(&filledChairs);
// Acquire the mutex lock to protect the chairs and the wait count.
pthread_mutex_lock(&chairMutex);
// Critical region: Remove a student from a chair.
meetingId = chairs[out];
out = (out+1)%CHAIR_COUNT;
waitCount--;
// Release the mutex lock.
pthread_mutex_unlock(&chairMutex);
char event[80];
sprintf(event, "Professor meets with student %d", meetingId);
print(event);
// Meet with the student.
sleep(rand()%MAX_MEETING_DURATION + 1);
meetingsCount++;
sprintf(event, "Professor finishes with student %d", meetingId);
meetingId = 0;
print(event);
}
}
// The professor thread.
void *professor(void *param)
{
print("Professor opens her door");
// Set the timer for for office hour duration.
profTimer.it_value.tv_sec = OFFICE_HOUR_DURATION;
setitimer(ITIMER_REAL, &profTimer, NULL);
// Meet students until the office hour is over.
do {
professorMeetsStudent();
} while (!timesUp);
print("Professor closes her door");
return NULL;
}
// Timer signal handler.
void timerHandler(int signal)
{
timesUp = 1; // office hour is over
}
//--------------------------------------------------------------------------
/*进程3的线程*/
//--------------------------------------------------------------------------
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World!In process3, it's thread #%ld!
", tid);
pthread_exit(NULL);
//printf("goodbye! exit thread #%ld!
", tid);
}
//----------------------------------------------------------------------------
/*进程2的线程*/
//----------------------------------------------------------------------------
void * producer(void * parm)
{
char item[NUMITEMS]="IT'S A SMALL WORLD, AFTER ALL.";
int i;
printf("producer started.
");
for(i=0;i