i'm sorry. i dont know what to do this project. please please help me.
In this project we are going to implement reader-writer problem with circular buffer using various methods. Consider the following data structure:
typedef struct
{
int total;
short len, readPtr, writePtr, buf[BUFSIZE];
} CBuf;
where
Total: is the total number of integers that the writher has ever produced. In this project lets use 2000(starting with 1). You may adjust this number if you like.
len: is the number of integers left in the buffer,
readPtr: indicates the next location in the buffer that eh reader is going to read,
writePtr: is the next location in the buffer where the writer is going to place the number,
buf: is the circular buffer and lets assume it has the size of 20.
Assignment: For each of the following steps, you will need to use ctime and times functions in the beginning and/or the end of the program to record the time and read and write, please have it run (100,000..110,000) factorial. For steps 1 and 2, you may use spin lock, busy wait to synchronize the two processes/threads.
STEP 1: Write a c program of two processes using shared memory. You will need the following functions:
#include<sys/ipc.h>
#include<sys/shm.h>
int shmget(key_t key, int size, int shmflg);
void *shmat(int shmid, const void *shmaddr, int shmflg);
int shmdt(const void *shmaddr);
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
Given below are the proper steps to use these functions:
1. you will need to create or access the shared memory segment by something like following:
#define SHMKEY ((key_t) 8000)
if( (shmid = shmget( SHMKEY, sizeof(CBuf), 0666|IPC_CREAT)) < 0)
or
if( (shmid = shmget( SHMKEY, sizeof(CBuf), 0)) < 0)
2. you will attach the shared memory segment to your programs address space by something similar to the following:
if( (cbuf = (CBuf*) shmat (shmid,(char*) 0,0)) == (CBuf*) - 1)
3. when you are done you will need to have it detached from you programs address space by something similar to the following:
if( shmdt(cbuf) < 0)
4. one of the processes needs to destroy the shared memory segment using the following function:
if( shmctl(shmid, IPC_RMID, NULL) )
STEP 2: Modify step 1 using threads instead of processes. Since threads share the same address space, so shared memory is not needed here.
STEP 3: Modify step 2 using semaphore to synchronize the two threads.