proreza
سه شنبه 15 تیر 1389, 18:40 عصر
سلام
دوستان من یکم مبتدی هستم میخواهم بدونم کار این کدها چیه چه کار انجام میدن-خلاصه بگید ممنون میشم
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#define NWRITER 2 /* total number of producers - 1 */
#define NREADER 5 /* total number of consumers - 1 */
#define NORW 4 /* number of items writen/read */
typedef struct {
sem_t mutex; /* enforce mutual exclusion to shared data */
sem_t db;
} rwbuff_t;
/* instance shared of sbuf_t */
rwbuff_t shared;
/* number of processes reading or wanting to */
int rc = 0;
/* function prototypes */
void *Writer(void *arg);
void *Read(void *arg);
/* increments within the mutex */
int countW, countR;
int write_request = 0;
int main()
{
/* writer/read threads */
pthread_t tWrite, tRead;
/* creates semaphores */
sem_init(&shared.mutex, 0, 1);
sem_init(&shared.db, 0, 1);
/* loop control */
int index;
/* creates write thread */
for (index = 1; index < NWRITER; index++)
{
/* Create 1 write */
pthread_create(&tWrite, NULL, Writer, (void*)index);
}
/* creates 4 read threads */
for (index = 1; index < NREADER; index++)
{
pthread_create(&tRead, NULL, Read, (void*)index);
}
/* temp variables to compute the consumes and produces per hour */
int writePH, readPH, ratio;
/* keeps track of seconds */
int seconds = 1;
while(1)
{
sleep(1);
readPH = ((countR * 3600) / seconds);
printf("\nOperations after %d seconds: \n\n", seconds);
printf("Number of read operations completed per hour : %d\n", readPH);
if (countW == 0)
{
printf("Number of write operations completed per hour: %d\n", countW);
printf("The ratio cannot be calculated until a write operations "
"has taken place\n");
}
else
{
writePH = ((countW * 3600) / seconds);
ratio = writePH / readPH;
printf("Number of write operations completed per hour: %d\n", writePH);
printf("Ratio of reads per hour over writes per hour: %d\n\n", ratio);
} seconds++;
}
/* destroy semaphore */
sem_destroy(&shared.mutex);
sem_destroy(&shared.db);
pthread_exit(NULL);
}
void *Read(void *arg)
{
int index, i;
index = (int)arg;
printf("Read thread %d - in line to read...\n\n", index);
while (1)
{
sem_wait(&shared.mutex);
rc++;
if (rc == 1)
{
sem_wait(&shared.db);
sem_post(&shared.mutex);
printf("Reader %d in criticial section\n",index);
countR++;
sleep(1);
// read_data_base();
sem_wait(&shared.mutex);
rc--;
}
if(rc == 0)
{
sem_post(&shared.db);
sem_post(&shared.mutex);
printf("Reader %d in non-critical section\n", index);
// use_data_read();
}
}
}
void *Writer(void *arg)
{
int data, index, i;
index = (int)arg;
printf("Write thread %d - in line to write...\n\n", index);
while (1)
{
//think_up_data();
printf("Writer tries to enter the critical section \n");
sem_wait(&shared.db);
// write_data_base();
printf("Writer is in critical region\n");
countW++;
sleep(1);
sem_post(&shared.db);
printf("Writer is in non-critical region\n");
}
sleep(1);
}
ووقتی اجرا میکنم ارور میده درکدهای
#include <pthread.h>
و
#include <semaphore.h>
دوستان من یکم مبتدی هستم میخواهم بدونم کار این کدها چیه چه کار انجام میدن-خلاصه بگید ممنون میشم
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#define NWRITER 2 /* total number of producers - 1 */
#define NREADER 5 /* total number of consumers - 1 */
#define NORW 4 /* number of items writen/read */
typedef struct {
sem_t mutex; /* enforce mutual exclusion to shared data */
sem_t db;
} rwbuff_t;
/* instance shared of sbuf_t */
rwbuff_t shared;
/* number of processes reading or wanting to */
int rc = 0;
/* function prototypes */
void *Writer(void *arg);
void *Read(void *arg);
/* increments within the mutex */
int countW, countR;
int write_request = 0;
int main()
{
/* writer/read threads */
pthread_t tWrite, tRead;
/* creates semaphores */
sem_init(&shared.mutex, 0, 1);
sem_init(&shared.db, 0, 1);
/* loop control */
int index;
/* creates write thread */
for (index = 1; index < NWRITER; index++)
{
/* Create 1 write */
pthread_create(&tWrite, NULL, Writer, (void*)index);
}
/* creates 4 read threads */
for (index = 1; index < NREADER; index++)
{
pthread_create(&tRead, NULL, Read, (void*)index);
}
/* temp variables to compute the consumes and produces per hour */
int writePH, readPH, ratio;
/* keeps track of seconds */
int seconds = 1;
while(1)
{
sleep(1);
readPH = ((countR * 3600) / seconds);
printf("\nOperations after %d seconds: \n\n", seconds);
printf("Number of read operations completed per hour : %d\n", readPH);
if (countW == 0)
{
printf("Number of write operations completed per hour: %d\n", countW);
printf("The ratio cannot be calculated until a write operations "
"has taken place\n");
}
else
{
writePH = ((countW * 3600) / seconds);
ratio = writePH / readPH;
printf("Number of write operations completed per hour: %d\n", writePH);
printf("Ratio of reads per hour over writes per hour: %d\n\n", ratio);
} seconds++;
}
/* destroy semaphore */
sem_destroy(&shared.mutex);
sem_destroy(&shared.db);
pthread_exit(NULL);
}
void *Read(void *arg)
{
int index, i;
index = (int)arg;
printf("Read thread %d - in line to read...\n\n", index);
while (1)
{
sem_wait(&shared.mutex);
rc++;
if (rc == 1)
{
sem_wait(&shared.db);
sem_post(&shared.mutex);
printf("Reader %d in criticial section\n",index);
countR++;
sleep(1);
// read_data_base();
sem_wait(&shared.mutex);
rc--;
}
if(rc == 0)
{
sem_post(&shared.db);
sem_post(&shared.mutex);
printf("Reader %d in non-critical section\n", index);
// use_data_read();
}
}
}
void *Writer(void *arg)
{
int data, index, i;
index = (int)arg;
printf("Write thread %d - in line to write...\n\n", index);
while (1)
{
//think_up_data();
printf("Writer tries to enter the critical section \n");
sem_wait(&shared.db);
// write_data_base();
printf("Writer is in critical region\n");
countW++;
sleep(1);
sem_post(&shared.db);
printf("Writer is in non-critical region\n");
}
sleep(1);
}
ووقتی اجرا میکنم ارور میده درکدهای
#include <pthread.h>
و
#include <semaphore.h>