Solved

does fork() process share global variables

Posted on 2012-03-27
4
1,541 Views
Last Modified: 2012-03-27
Hi all,
As what i know when you fork() a process, parent process and child process will be able to access the same global variables.
But i'm having the issue that when i changes some values in the child process, parent process isn't taking the changed values instead it still getting the same value as i initialized at first?
Can anyone explain to me why is this happening, or do i mistaken anything?
Thanks in advance.

this code contains race condition.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int x;
int y;

int main(void)
{
	pid_t pid;	
	if( (pid = fork()) < 0)
	{
		perror("Fork error");
		exit(1);
	}

	else if( pid == 0)
	{
		if (x == 0)
		{
			y = x + 1;
		}
		else
		{
			y = 5;
		}
		printf("child: x = %d y = %d\n", x, y);
	}
	
	else
	{
		if (y == 0)
		{
			x = y + 1;
		}
		else
		{
			x = 5;
		}
		printf("parent: x = %d y = %d\n", x, y);
	}

	exit(0);
}

Open in new window


output:

child: x = 0 y = 1
parent: x = 1 y = 0 <--if child runs first, shouldn't the parent x = 5 y = 1???
0
Comment
Question by:crazy4s
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 37774429
> As what i know when you fork() a process, parent process and child process will be able to access the same global variables.

Ah, no! They don't share globals, they will both have a copy of the variables, with the values as at the point when fork is called, but after that changes made in one process won't affect the variable in the other process.
0
 

Author Comment

by:crazy4s
ID: 37774461
oh they didn't!
hmmm so is there any possible way that child and parent process to access the same variable? or is impossible?
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 500 total points
ID: 37774476
You can set up shared memory areas but that is a fairly advanced topic. Also, that is not limited to child/parent processes. Any processes can be coded to access a shared memory region, and hence this stuff is called IPC (Inter-process communication). Otherwise, no, they have there own separate address spaces and can't shared variables.

What is the point that you are trying to achieve here?
0
 

Author Comment

by:crazy4s
ID: 37774487
hmmm okay i'm actually doing on the race condition problem and bump into this fork problem.
oh well but did learned something new!
thank you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question