1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#include <cstdlib>
#include <iostream>
//#include <ifstream>
#include <fstream>
#include <memory>
#include <pthread.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <errno.h>
using namespace std;
template <class T>
class SharedMemory
{
private:
int shmid;
key_t key;
char *pathname;
SharedMemory(const Thread& copy); // copy constructor denied
public:
SharedMemory(int size)
{
if((key = ftok(pathname,0)) < 0)
{
cout << "Can\'t generate key" << endl;
exit(-1);
}
if((shmid = shmget(key, size, 0666 | IPC_CREAT | IPC_EXCL)) < 0)
{
if(errno != EEXIST)
{
cout << "Can\'t create shared memory" << endl;
exit(-1);
} else {
if((shmid = shmget(key, size, 0)) < 0)
{
cout << "Can\'t find shared memory" << endl;
exit(-1);
}
}
}
~SharedMemory(); { shmdt(shmem); }
T attach(); { return (T)shmat(shmid, 0, 0); }
int detach(); { return shmctl(shmid, IPC_RMID, 0); }
}};
class Semaphore
{
private:
key_t key;
int count;
int shmid;
sembuf buf;
public:
Semaphore() {}
Semaphore(char *pathname)
{
if((key = ftok(pathname, 0)) < 0) {
printf("Can\'t generate key\n");
exit(-1);
}
if((semid = semget(key, 1, 0666 | IPC_CREAT)) < 0){
printf("Can\'t get semid\n");
exit(-1);
}
}
~Semaphore() { semctl(semid, 0, 2, IPC_RMID); }
void lock()
{
buf.sem_op = -1;
buf.sem_flg = 0;
buf.sem_num = 0;
semop(semid, &buf, 1);
}
void unlock()
{
buf.sem_op = 1;
buf.sem_flg = 0;
buf.sem_num = 0;
semop(semid, &buf[1], 1);
}
};
class Thread
{
private:
pthread_t thread;
Thread(const Thread& copy); // copy constructor denied
static void *thread_func(void *d) { ((Thread *)d)->run(); }
public:
Thread() {}
virtual ~Thread() {}
virtual void run() = 0;
int start() { return pthread_create(&thread, NULL,
Thread::thread_func, (void*)this); }
int wait () { return pthread_join (thread, NULL); }
};
struct memory
{
int status;
char bytes[1024];
};
class Producer: public Thread
{
private:
public:
void run()
{
Semaphore mutex;
SharedMemory<memory*> mem(sizeof(memory));
memory *buf = mem.attach();
mutex.lock();
ifstream file("read.txt"); //reading a file
if (!file.is_open())
{
cout << "Can`t open file read.txt." << endl;
exit(-1);
}
while (!file.eof())
{
mutex.lock();
file.read(memory->buf, 1024);
memory->status = 1;
mutex.unlock();
}
memory->status = 0;
read.close();
}
};
class Consumer: public Thread
{
public:
void run()
{
Semaphore mutex;
SharedMemory<memory*> mem(sizeof(memory));
memory *buf = mem.attach();
ofstream file("write.txt");//writing to a file
if (!file.is_open())
{
cout << "Can`t open file read.txt." << endl;
exit(-1);
}
while (memory->status)
{
mutex.lock();
file.write(memory->buf, 1024);
mutex.unlock();
}
file.close();
}
};
typedef std::auto_ptr<Thread> ThreadPtr;
int main(int argc, char *argv[])
{
ThreadPtr a(new Producer());
ThreadPtr b(new Consumer());
if (a->start() != 0 || b->start() != 0)
return 1;
if (a->wait() != 0 || b->wait() != 0)
return 1;
return 0;
}
|