LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2004, 08:11 AM   #1
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Rep: Reputation: 30
C - sharing variables between forked processes?


my program forks early on and each process runs a looping function.
I want one function to set the value of 'int range' and the other function in another process to change based on the value of range set by the first function.
i have something like this:


int range;

int loop_A()
{
while(1)
{

}
}


int loop_B()
{
while(1)
{

}
}

int loop_1()
{
while(1)
{
range = gpa_read(0); /* gets a distance reading from a sensor */
}
}

int loop_2()
{
while(1)
{
if(range == 0)
{
loop_A();
}
if(range > 0)
{
loop_B();
}
}
}

int main()
{
pid_t pid;
pid = fork();
int proc_val;
switch(pid)
{
case 0:
proc_val = 0;
break;
case 1:
proc_val = 1;
break;
}
if(proc_val == 0)
{
loop_1();
}
else
{
loop_2();
}
}

/* end code */

but loop_2() never seems to change.
how do i define vars to be accessable across all processes?
thanks
 
Old 03-13-2004, 12:33 PM   #2
haobaba1
Member
 
Registered: Jul 2003
Location: VA Tech
Distribution: Mandrake 9.1
Posts: 73

Rep: Reputation: 15
When the processes fork any global variables are passed but if you change the value of that variable in the forked process the change is only visible to the process that changed it.

It sounds to me like you should be looking at a threaded solution since threads can share variables but you will need to use semaphors or some equivalent to protect the variable from multiple concurrent writes to it.

Your other solution will be to use pipes between your processes, this would be more difficult to maintain than using threads.
 
Old 03-13-2004, 03:19 PM   #3
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
gonna give it a try using threads.

Thanks
 
Old 03-13-2004, 05:27 PM   #4
ocularbob
Member
 
Registered: Nov 2002
Location: brooklyn NYC
Distribution: gentoo
Posts: 212

Original Poster
Rep: Reputation: 30
awesome it's working great. I managed to get it going without semaphore because there is only one thread that will write to a variable while all the others are only reading.
thanks again
 
Old 03-13-2004, 08:45 PM   #5
shellcode
Member
 
Registered: May 2003
Location: Beverly Hills
Distribution: Slackware, Gentoo
Posts: 350

Rep: Reputation: 32
i suggest you read this: http://www.ecst.csuchico.edu/~beej/guide/ipc/

there are plenty of ways to share values before processes.
 
Old 03-14-2004, 03:29 AM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
BTW, the case statements in your 'switch(pid)' are incorrect. 0 is returned by fork() in the parent process (which you have correct), but it will almost certainly never return 1 for the child process. So proc_val will never be 1 and loop_2() will never run.

Might wanna try something like this instead:

Code:
pid = fork();
proc_val = pid?1:0;
if(proc_val)
  loop_2();
else
  loop_1();
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Conectiva 10 Forked thorn168 Conectiva 0 09-07-2005 10:58 AM
Two variables sharing memory? Ephracis Programming 2 04-01-2005 12:18 AM
sharing memory between processes spuzzzzzzz Programming 2 10-13-2004 07:18 AM
Parent child processes signal sharing iftiuk Programming 8 06-24-2004 01:32 PM
SCO Speaks with Forked Tongue arun79 Linux - General 1 06-20-2003 01:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration