LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-11-2008, 11:19 PM   #1
sagsriv
LQ Newbie
 
Registered: Jan 2008
Location: Kolkata India
Distribution: fedora 6
Posts: 21

Rep: Reputation: 15
how to retain changes made in a global variable by a child process after in exits


I want to retain changes made by a child process in a variable after the child process exits.Consider following program

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
volatile int i=0;
void child(){
i++;
printf("i in child is %d \n",i);
kill(getpid(),SIGKILL);
}
int main()
{
pid_t pid;
printf("fork program starting\n");
pid = fork();
if (pid==0)
child();
else wait(NULL);
printf("i in parent is %d \n",i);
}

it gives output :
fork program starting
i in child is 1
i in parent is 0

but I want that i,after execution of child process (which made it 1) remains 1. Is there a way of doing it?

Last edited by sagsriv; 03-11-2008 at 11:20 PM.
 
Old 03-12-2008, 12:46 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,877

Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
My C programming skill aren't the greatest, but I see things which do not conform to what I was taught.

Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
volatile int i=0;
void child(){
i++;
printf("i in child is %d \n",i);
kill(getpid(),SIGKILL);
}
int main()
You have the function definition placed where the function prototype should be. Furthermore, the function is defined as void child (void), meaning it receives nothing and returns nothing.
Code:
void child(){
i++;
printf("i in child is %d \n",i);
kill(getpid(),SIGKILL);}
Perhaps some rearranging might help.
Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

*/prototype declatartion - change child() to type int
int child();

volatile int i=0;

int main()
{
pid_t pid;
printf("fork program starting\n");
pid = fork();
if (pid==0)
child();
else wait(NULL);
printf("i in parent is %d \n",i);
}

/* function definition relocated after main(), defined as type int /*
int child(){
i++;
printf("i in child is %d \n",i);
kill(getpid(),SIGKILL);
return i;
}
A void function returns nothing; an int function returns an interer value.

Now you need to finish the correction to use the return value.

Last edited by bigrigdriver; 03-12-2008 at 12:48 AM.
 
Old 03-12-2008, 01:13 AM   #3
sagsriv
LQ Newbie
 
Registered: Jan 2008
Location: Kolkata India
Distribution: fedora 6
Posts: 21

Original Poster
Rep: Reputation: 15
thanx for replying, Mr. bigrigdriver.
C allows(as per my knowledge) function definition before main() in which case you need not declare the prototype. void functions are also syntactically correct.Did u notice the kill() in child? it actually kills the child process. then what would it return? For example, try following:
int child(){
i++;
printf("i in child is %d \n",i);
kill(getpid(),SIGKILL);
printf("child not died");
return i;
}
You ll never see "child not died". So, its expected that return statement is also not executed.
This doesnot solve the problem. Probably u had a misunderstanding with my question.Suppose a child process modifies a global variable. What I am getting is that after the child dies, change in that variable is not retained, which is unlike the case that when the living process modifies it, global variable change is retained.Comment out the if and else statement in main(). You ll see the difference.
 
Old 03-12-2008, 01:44 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
In short you can't. Once you fork, you are running 2 separate processes.
You can use various mechanisms to get around this eg shared mem, or threads or a temp file, or even proc-to-proc comms via a pipe or socket.
This area is known as IPC (Inter Process Communication).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 08:26 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 01:40 AM
How to share the same variable between parent and child process? zwylinux Programming 6 03-26-2007 03:47 AM
retain a variable each time a script re-runs cachemonet Programming 4 08-01-2006 01:11 AM
Killing a child process from another child marri Programming 6 10-01-2004 08:08 PM


All times are GMT -5. The time now is 07:27 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration