Semaphores

Hello! Please help fix bugs! Here's the source code:
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;
}


And here errors that the compiler output:

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
g++ semaphor.cc
semaphor.cc:24:24: error: ‘Thread’ does not name a type
semaphor.cc:24:32: error: ISO C++ forbids declaration of ‘copy’ with no type
semaphor.cc: In constructor ‘SharedMemory<T>::SharedMemory(int)’:
semaphor.cc:57:30: error: ‘shmem’ was not declared in this scope
semaphor.cc:59:46: error: returning a value from a constructor
semaphor.cc:60:53: error: returning a value from a constructor
semaphor.cc: In constructor ‘Semaphore::Semaphore(char*)’:
semaphor.cc:89:13: error: ‘semid’ was not declared in this scope
semaphor.cc: In destructor ‘Semaphore::~Semaphore()’:
semaphor.cc:95:27: error: ‘semid’ was not declared in this scope
semaphor.cc: In member function ‘void Semaphore::lock()’:
semaphor.cc:103:15: error: ‘semid’ was not declared in this scope
semaphor.cc: In member function ‘void Semaphore::unlock()’:
semaphor.cc:111:15: error: ‘semid’ was not declared in this scope
semaphor.cc:111:28: error: no match foroperator[]’ in ‘((Semaphore*)this)->Semaphore::buf[1]’
semaphor.cc: In member function ‘virtual void Producer::run()’:
semaphor.cc:150:49: error: call of overloaded ‘SharedMemory(unsigned int)’ is ambiguous
semaphor.cc:26:5: note: candidates are: SharedMemory<T>::SharedMemory(int) [with T = memory*]
semaphor.cc:24:5: note:                 SharedMemory<T>::SharedMemory(const int&) [with T = memory*]
semaphor.cc:18:1: note:                 SharedMemory<memory*>::SharedMemory(const SharedMemory<memory*>&)
semaphor.cc:151:27: error: ‘class SharedMemory<memory*>’ has no member named ‘attach’
semaphor.cc:165:29: error: expected primary-expression before ‘->’ token
semaphor.cc:166:19: error: expected unqualified-id before ‘->’ token
semaphor.cc:170:15: error: expected unqualified-id before ‘->’ token
semaphor.cc:171:14: error: request for member ‘close’ in ‘read’, which is of non-class type ‘ssize_t(int, void*, size_t)’
semaphor.cc: In member function ‘virtual void Consumer::run()’:
semaphor.cc:182:49: error: call of overloaded ‘SharedMemory(unsigned int)’ is ambiguous
semaphor.cc:26:5: note: candidates are: SharedMemory<T>::SharedMemory(int) [with T = memory*]
semaphor.cc:24:5: note:                 SharedMemory<T>::SharedMemory(const int&) [with T = memory*]
semaphor.cc:18:1: note:                 SharedMemory<memory*>::SharedMemory(const SharedMemory<memory*>&)
semaphor.cc:183:27: error: ‘class SharedMemory<memory*>’ has no member named ‘attach’
semaphor.cc:192:22: error: expected primary-expression before ‘->’ token
semaphor.cc:195:30: error: expected primary-expression before ‘->’ token


Task is: to realize the prog to copy the file of any size in blocks of 128 bytes through the buffer 1 Kbyte
Thanks in advance!
Last edited on
You need to forward declare Thread. At the top of the program add:
 
class Thread;


Extra semicolon:
 
~SharedMemory(); { shmdt(shmem); }


In short, the compiler's telling you what's wrong. Look at what it's saying and correct your mistakes.
So after class Thread; are in the program (105 string)!
If not hard please help fix all the bugs !!!!!!
Only fix the first one then recompile. You can't seriously expect someone to fix all your syntax errors in a 200+ line piece of code.
Last edited on
I certainly understand that it is very much asked for this (((but at least some help to correct mistakes and please tell us what and where to insert "I beg you!!!
Topic archived. No new replies allowed.