LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 06-28-2007, 04:33 AM   #1
hua
Member
 
Registered: Oct 2006
Location: Slovak Republic
Distribution: Slackware 14.2, current
Posts: 461

Rep: Reputation: 78
variable interoperation in fork()-ed processes


I am working on a code where I want to fork more processes which will all make its own role in the program.

Code:
int kill; 
int processStatus1; 

int main() 

int child1, child2, child3 ... 

child1 = fork();... calls the refresh function for example... 
child2 = fork();... calls the virtualSpace function ... and so on... 

kill =1;
the functions should run in the same time and if the kill state is received all should make their end processes and the whole program terminate...

I was trying define some global variables (kill, processStatus1) but even if I declare in the main -> kill =1; the childs dose note see this state value.
I thought that I can do that with pointers but I was wrong. Can you help me to solve my problem?
 
Old 06-28-2007, 05:48 AM   #2
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
Ever thought about using a pipe to communicate between processes? I would think that would work. There is a lot of information on the net about that.

HTH,

Centinul
 
Old 06-28-2007, 06:49 AM   #3
nautilus
Member
 
Registered: Jun 2007
Location: London, Athens
Distribution: Debian, Ubuntu
Posts: 36

Rep: Reputation: 15
Hello

hua, what you definitely are talking about is IPC (inter process communication) and it can be implemented by different ways (pipes is one of them). The simples I can suggest is just to send a signal from one process to the other... Just goggle for "Unix IPC" and you will get a better idea.

Now, when you fork off (excuse my french) process, it has a completely different stack, so one process cannot access the variables of the other. I think what you are confusing it with is threading. A thread is a much more lightweight operation then forking off (oops.. I did it again) a process, and on top of that they can access same resources.

No matter what you do and how you implement this be careful with race conditions (eg accessing a resource before it has been updated) and/or deadlocks (if you control a resource with locks and one process/thread locks it without allowing others to get their hands on).
 
Old 06-28-2007, 07:56 AM   #4
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
There is also shmget, shared memory.
For semaphore, to avoid race conditions or deadlocks, you would use semget.

This can become very tricky on an algorithm point of view.
An example for understanding the problem of deadlock and starvation:
http://www.cs.columbia.edu/~smb/clas...6-4118/l09.pdf
(don't read this before going to bed, you won't sleep )

There's some good introduction to threads here:
http://www-128.ibm.com/developerwork.../l-posix1.html
http://www-128.ibm.com/developerwork.../l-pthred.html
 
Old 06-28-2007, 07:58 AM   #5
hua
Member
 
Registered: Oct 2006
Location: Slovak Republic
Distribution: Slackware 14.2, current
Posts: 461

Original Poster
Rep: Reputation: 78
Yes, thats it IPC. I know that it should be already somewhere described but I didn't know how it is called. Now I know thanks. But I am also glad that I reached acceptable results bymyself (in this case I understand exactly what is that about).

My problem is that I done this:
Code:
int main() {

pid1=fork()
if(pid1==0)
{
      child1(0,0);   // calling a function whith new process
}
...
stat1=child(1,1);
I had done it this way: define 2 arguments for function child1(arg1,arg2);
Code:
        int child1(int runlevel, int mainARG)
        {

		if(runlevel==0)
		{
			status1=mainARG;
		}
		if(runlevel==1)
		{
			status1=mainARG;
		}
                if(status1==1)
                 {
                      return status1;  // killing child function
                 }
it works, it ends the function called and I did not get the 'broken pipe' error (or something like that because of unfinished function child1()).
The child1(0,0) initiates a forked process (function) and child(1,1) terminate the function. But not the child process.

I am sure, it will have a lot of bugs and problems (and there will be simpliest ways of doing that also). But till now I only read about arguments and now it starts to make a sense...

Time to read about IPC...
 
Old 06-28-2007, 08:23 AM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi

If all you want to do is kill the parent and the whole lot of children, there is an easier way. Before you do any fork, call setpgrp() to start a new process group. Then to kill, simply use 0 for the pid - kill(0,9) - and the whole lot gets killed.
 
Old 06-28-2007, 08:48 AM   #7
hua
Member
 
Registered: Oct 2006
Location: Slovak Republic
Distribution: Slackware 14.2, current
Posts: 461

Original Poster
Rep: Reputation: 78
Now I realised that I going to have more troubles if its so hard to pass data from one forked process to the other.
I was ment to use that interprocess communication in a graphical enviroment.
I want to handle whit one forked process a data collection where I will have saved a red,green,blue pixel information for x,y cordinates on graphical window (and its refreshing).

And with other forked process to handle objects which should be viewed by that screen...

but that is far from here...
 
Old 06-28-2007, 09:11 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
Lots of folks, when they first hear about forking and discover that they can do it, really get carried away. You know, they do it all the time... in the theatre, on the bus... (ahem)

Seriously... it really does not pay to have too many processes, or even threads, running around. They don't "multiply" the system's resources or capabilities: they rather-seriously "divide" it.
 
Old 06-29-2007, 05:53 AM   #9
hua
Member
 
Registered: Oct 2006
Location: Slovak Republic
Distribution: Slackware 14.2, current
Posts: 461

Original Poster
Rep: Reputation: 78
I think that this was exactly what happend. I was carried away by forking but now I think that it will be better to do the things without it.
 
  


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
creating sequential child processes using fork() BrokenFighter Programming 1 03-06-2007 10:11 PM
apache2, processes die and do not fork rjcrews Linux - Software 3 03-18-2006 03:07 PM
Run 4-processes concurrently using fork() ugp Programming 1 02-26-2006 08:49 AM
fork and multiple son processes biiiep Programming 4 05-11-2005 11:00 AM
why there're lots of child processes when fork? iclinux Programming 3 01-18-2005 07:09 AM

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

All times are GMT -5. The time now is 06:52 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