SmileeTiger
Programmer
I have created a threading application that has a couple of processes accessing the same variable. I want them to both check the variable to see if the other process is using a shared file. If one is the other sleeps.
This is what I have done thus far:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define FNAME "data.dat" //The file that will be read from and written to
#define MAXREADERS 100
#define MAXWRITERS 100
#define WRITERS 5
#define RUNS 1
void CreateWriter(struct state Reader[], struct state *Writer, int
*WriteLock);
int main()
{
pid_t mainPID;
FILE *inFile, *outFile;
int tmp=0;
int i,pid, NumReaders, NumWriters;
int seed; //Seed for random number gen
int WriteLock=0; //Shows if file is locked for writing
for (i=0; i<WRITERS; i++)
{
Writers.seed = i + (int)pid; //Gives each thread it's //own seed
Writers.who = i;
CreateWriter(Readers, &Writers, &WriteLock);
}
}
void CreateWriter(struct state Reader[], struct state *Writer, int
*WriteLock)
{
int i;
if ( (pid=fork() ) !=0)
{
for (i=0; i<RUNS; i++)
{
sleep(rand_r(&(Writer->seed))%3); //Random sleep before
//starting
while(WriteLock==1) //Checks to see if other writers are writing
{
printf("Waiting for writer finish"
sleep(rand_r(&(Writer->seed))%3); //Random sleep
}
//since no one is writing now the writer can lock writing for himself
printf("\nThe lock var is: %i",*WriteLock);
*WriteLock=1; //No one else can write
printf("\nJust locked The lock var is: %i",*WriteLock);
sleep(rand_r(&(Writer->seed))%3); //Random sleep for a bit
*WriteLock=0; //Let the other process take over
}
}
}
It seems however, that neither process can see the others variables so both assume that they are unlocked at all times.
Any ideas?
Cory
This is what I have done thus far:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define FNAME "data.dat" //The file that will be read from and written to
#define MAXREADERS 100
#define MAXWRITERS 100
#define WRITERS 5
#define RUNS 1
void CreateWriter(struct state Reader[], struct state *Writer, int
*WriteLock);
int main()
{
pid_t mainPID;
FILE *inFile, *outFile;
int tmp=0;
int i,pid, NumReaders, NumWriters;
int seed; //Seed for random number gen
int WriteLock=0; //Shows if file is locked for writing
for (i=0; i<WRITERS; i++)
{
Writers.seed = i + (int)pid; //Gives each thread it's //own seed
Writers.who = i;
CreateWriter(Readers, &Writers, &WriteLock);
}
}
void CreateWriter(struct state Reader[], struct state *Writer, int
*WriteLock)
{
int i;
if ( (pid=fork() ) !=0)
{
for (i=0; i<RUNS; i++)
{
sleep(rand_r(&(Writer->seed))%3); //Random sleep before
//starting
while(WriteLock==1) //Checks to see if other writers are writing
{
printf("Waiting for writer finish"
sleep(rand_r(&(Writer->seed))%3); //Random sleep
}
//since no one is writing now the writer can lock writing for himself
printf("\nThe lock var is: %i",*WriteLock);
*WriteLock=1; //No one else can write
printf("\nJust locked The lock var is: %i",*WriteLock);
sleep(rand_r(&(Writer->seed))%3); //Random sleep for a bit
*WriteLock=0; //Let the other process take over
}
}
}
It seems however, that neither process can see the others variables so both assume that they are unlocked at all times.
Any ideas?
Cory