Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Sharing variables between threads

Sharing variables between threads

Sharing variables between threads

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

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
int i;

if ( (pid=fork() ) !=0)
for (i=0; i<RUNS; i++)

sleep(rand_r(&(Writer->seed))%3); //Random sleep before
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?


RE: Sharing variables between threads


This would not work as fork creates a copy of the existing
process and also the data area.
You may like to use some kind interprocess communication
functionality like semaphores,shared memory.
Also,fcntl system call F_SETLK options which would lock a
file, please check the man page.

Also additional FYI, instead of fork if you use vfork
the parent and child would use the same data space.
So, if WriteLock is set over in child then
parent can see it and vice-versa.

Hope this may help.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close