Hi everyone,
I'm new here ... haven't had my warm welcome yet ;P
So i need your help with my program. I'm supposed to program a simulator of the reader writer problem without using threads. So i thought i could use a timer to interrupt the running process every 2 seconds to see if other processes want to enter the critical section (which is in my case a txt file where readers and writers manipulates data) like a trap and i can do what i want in the routine... and all of this is to show the use of semaphores.
The question is, is such timer possible? can i do that? and how ?
Can someone help me plz :)

Edited 5 Years Ago by zidaneez: n/a

Post your code. Maybe we can help.

Thank you ;)

This is my program ... timer not yet added, neither the semaphore

#include <ctime>    // For time()
#include <cstdlib>  // For srand() and rand()
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

class Reader
{
private:
	string city[5];
	int tempr[5];

public:
	Reader()
	{
		for(int i=0; i<5; i++)
			tempr[i]=0;
	}
	void read()
	{
		ifstream inClientFile("record.txt",ios::in);
		if(!inClientFile)
		{
			cerr<<"File could not be opened"<<endl;
			exit(1);
		}

		for(int i=0;i<5; i++)
		{
			inClientFile>>city[i]>>tempr[i];
			cout<<city[i]<<"\t\t"<<tempr[i]<<endl;
		}
	}
	};

	class Writer
	{
	private:
		string city[5];
		int tempreture;

	public:

		Writer(){
			city[0]="Kuwait";
			city[1]="Manama";	
			city[2]="Ryadh";
			city[3]="Abu Dhabi";
			city[4]="Muscat";
	}
	
	void write()
	{
		srand(time(0));  
		

		ofstream outClientFile("record.txt",ios::out);
		if(!outClientFile)
		{
			cerr<<"File could not be opened"<<endl;
			exit(1);
		}


		for(int i=0; i<5; i++)
		{
			tempreture = (rand() % 60) + 1;
			outClientFile<<city[i]<<"\t\t"<<tempreture<<endl;
			cout<<city[i]<<"\t\t"<<tempreture<<"\n";	
		}
	}
	
	};



int main()
{
	srand(time(0)); // seeding the random generator function.

	cout<<"Welcome to our reader writer simulator :)\n\nIn this simulator, 5 reading processes and 3 writer processes are operating.\n";
	
	Reader* rdrProc[5];
	Writer* wrtProc[3];

	// Creating processes.
	for(int i=0; i<5; i++)
	{
		rdrProc[i]=new Reader();
	}

		for(int j=0; j<3; j++)
	{
		wrtProc[i]=new Writer();
	}
	
		// let any writer process write data to the file for starter.
		//wrtProc[1]->write();
		for(int count=0; count<20; count++)
		{
			int r,p;
			r = (rand() % 8) + 1;// randomly selecting a process to enter the file (1,2 & 3 are the writer processes. the rest are readers)
			if(r<=3)// the process is a writer process.
			{
				p=(rand() % 3) + 1;
				cout<<"Hi, I'm writer "<<p<<" and I wrote to the file the following data:\n";
				wrtProc[p]->write();
			}
			else // the process is a writer process.
			{
				p=(rand() % 5) + 1;
				cout<<"Hi, I'm reader "<<p<<" and I read the following data from the file:\n";
				rdrProc[p]->read();
			}
		}

	return 0;
}

and this one is a timer class that i was trying to change into something i can use.

class Timer1
{
public:
    static void Main() {
        System::Timers::Timer* aTimer = new System::Timers::Timer;
        aTimer->Elapsed += new ElapsedEventHandler(0, Timer1::OnTimedEvent);
        // Set the Interval to 5 seconds.
        aTimer->Interval=5000;
        aTimer->Enabled=true;
    }
private:
    // Specify what you want to happen when the Elapsed event is raised.
     static void OnTimedEvent(Object* /*source*/, ElapsedEventArgs* /*e*/)
     {
         Console::WriteLine(S"Hello World!");
     }
};

int main()
{
    Timer1::Main();

    Console::WriteLine(S"Press \'q\' to quit the sample.");
    while(Console::Read()!='q');
}

Can't help much with the code. It looks Microsoft-specific? But I don't see where you're creating separate processes. I see Reader and Writer objects being created, but no separate Reader and Writer processes? Maybe I'm missing something.

I'm a little confused as to what you want to do with this timer. I presume the assignment means you can't used the mutexes provided by the language and have to write your own? That's the assignment? Presumably you are to have a bunch of processes running and then reading and writing at random times and you have to set up some sort of policing to make sure everyone gets their turn, no one has to wait too long, and obviously, no one is reading something that someone else is writing to. I'm not sure what purpose the timer serves here.

If the question is "Can I make a daemon that signals other processes every two seconds and interrupt their program execution?", yes. Set up some signal catchers and send any processes an "I've woken up" signal using the csignal library.

http://www.cplusplus.com/reference/clibrary/csignal/

But again, I'm not sure how this would help you for this problem. I would think the signals for this type of problem would be more on the lines of "I want to enter" and "I'm done" and "You may enter now" rather than a timer based check from some main daemon process functioning as the gatekeeper.

Can't help much with the code. It looks Microsoft-specific? But I don't see where you're creating separate processes. I see Reader and Writer objects being created, but no separate Reader and Writer processes? Maybe I'm missing something.

I'm a little confused as to what you want to do with this timer. I presume the assignment means you can't used the mutexes provided by the language and have to write your own? That's the assignment? Presumably you are to have a bunch of processes running and then reading and writing at random times and you have to set up some sort of policing to make sure everyone gets their turn, no one has to wait too long, and obviously, no one is reading something that someone else is writing to. I'm not sure what purpose the timer serves here.

If the question is "Can I make a daemon that signals other processes every two seconds and interrupt their program execution?", yes. Set up some signal catchers and send any processes an "I've woken up" signal using the csignal library.

http://www.cplusplus.com/reference/clibrary/csignal/

But again, I'm not sure how this would help you for this problem. I would think the signals for this type of problem would be more on the lines of "I want to enter" and "I'm done" and "You may enter now" rather than a timer based check from some main daemon process functioning as the gatekeeper.

Actually, you're quite close.. am trying to virtualize how semaphores serve as mutexs or locks. But i'm a beginner so i don't know much and that is why i thought of timers and time interrupt (like in embedded systems ;D).
The idea you represented looks great. Never heared of signal catchers and all the things you mentioned ;D I will look it up.
Thanks a lot ... (f)

Can't help much with the code. It looks Microsoft-specific? But I don't see where you're creating separate processes. I see Reader and Writer objects being created, but no separate Reader and Writer processes? Maybe I'm missing something.

I'm a little confused as to what you want to do with this timer. I presume the assignment means you can't used the mutexes provided by the language and have to write your own? That's the assignment? Presumably you are to have a bunch of processes running and then reading and writing at random times and you have to set up some sort of policing to make sure everyone gets their turn, no one has to wait too long, and obviously, no one is reading something that someone else is writing to. I'm not sure what purpose the timer serves here.

If the question is "Can I make a daemon that signals other processes every two seconds and interrupt their program execution?", yes. Set up some signal catchers and send any processes an "I've woken up" signal using the csignal library.

http://www.cplusplus.com/reference/clibrary/csignal/

But again, I'm not sure how this would help you for this problem. I would think the signals for this type of problem would be more on the lines of "I want to enter" and "I'm done" and "You may enter now" rather than a timer based check from some main daemon process functioning as the gatekeeper.

on second thought, i don't think that works for me ... because i need processes trying to access the file at any time, including the time when one process is already inn and so they would have to wait for the process to issue a signal.
This is why i wanted to use a timer .... to be able to interrupt the execution of a process in the critical section and randomly pick others and say they wanted to enter but couldn't because of the mutex ( I know, it's cheating ;P)

>> to be able to interrupt the execution of a process in the critical section

That's what signals do. They interrupt the program flow of the receiving process. In particular, they can wake up a sleeping process. A process tries to enter, can't, goes to sleep, then later, when it's their turn, another process wakes them up and tells them it's their turn and they Read or Write.

>> to be able to interrupt the execution of a process in the critical section

That's what signals do. They interrupt the program flow of the receiving process. In particular, they can wake up a sleeping process. A process tries to enter, can't, goes to sleep, then later, when it's their turn, another process wakes them up and tells them it's their turn and they Read or Write.

Ok, but where to issue the signal?? In a multi threaded program it's more than fine. yet i don't see how it's efficient in a single threaded program like mine!

Ok, but where to issue the signal?? In a multi threaded program it's more than fine. yet i don't see how it's efficient in a single threaded program like mine!

That's where I come back to my original question a few posts back. Your program is not only single-THREADED, but it's a single PROCESS as far as I can tell. All the Readers and Writers exist as objects in the same process, so the whole problem is moot as far as I can tell since the problem only makes sense if the readers/writers are all separate processes or threads (any timers, of course, will involve the OS creating new threads or processes in the background and will likely use signals or whatever in the background). So please confirm whether in fact all readers and writers are indeed intended to be in the same process. If they are, I don't imagine you'd use signals.

That's where I come back to my original question a few posts back. Your program is not only single-THREADED, but it's a single PROCESS as far as I can tell. All the Readers and Writers exist as objects in the same process, so the whole problem is moot as far as I can tell since the problem only makes sense if the readers/writers are all separate processes or threads (any timers, of course, will involve the OS creating new threads or processes in the background and will likely use signals or whatever in the background). So please confirm whether in fact all readers and writers are indeed intended to be in the same process. If they are, I don't imagine you'd use signals.

It's just a simulator! not a real thing. This is why we don't have to have real processes although we can but it would be to complicated for us.
I don't know. The more i think about it, the further i stray from the target :(
I'm sorry for bothering you and waisting your time :(

>> It's just a simulator! not a real thing. This is why we don't have to have real processes

There are all sorts of simulators. No need to get upset. You're not wasting my time and I'm not wasting your time. I was just trying to clarify and help and you said you were creating new processes in the comments, but didn't actually create any. You made a point about not creating new threads, so it looked like you were intending to actually create the new processes and making an intentional distinction between threads and processes.

Anyway, good luck.

>> It's just a simulator! not a real thing. This is why we don't have to have real processes

There are all sorts of simulators. No need to get upset. You're not wasting my time and I'm not wasting your time. I was just trying to clarify and help and you said you were creating new processes in the comments, but didn't actually create any. You made a point about not creating new threads, so it looked like you were intending to actually create the new processes and making an intentional distinction between threads and processes.

Anyway, good luck.

oops, you misunderstood me due to my bad english ... I apologize
I wasn't upset + you're definitly not waisting my time ... i actually learned a lot from you in just one day ;) + when i reffered to the objects as processes, i was imagining them to be so :D that's why

Good Luck to you too and thnx for your help ;P

This article has been dead for over six months. Start a new discussion instead.