LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 04-28-2002, 02:05 AM   #1
jdevanand
LQ Newbie
 
Registered: Jan 2002
Location: Memphis
Distribution: Redhat 7.2
Posts: 23

Rep: Reputation: 15
child and parent process error


plz help me..
.same address is used by k in parent and child.(it is wrong). why is that so...? is there any error in the progam..?


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<math.h>
#include<unistd.h>

#define SHM_SIZE 1024 /* make it a 1K shared memory segment */

unsigned char *str;
void test(int *);

int main()
{
int a;
a=100;
printf("\nIn Main: the address of a: %u\n",&a);
test(&a);
exit(0);
}


void test(int *k)
{
pid_t pid;
long z;
printf("\nCommon\n");

if((pid=fork())==0) { // child
printf("In Child\n");
printf(" child k = %lu\n", k);
printf(" child *k = %d\n", *k);
printf(" child increased k = %lu\n",k);
exit(0);
}
else { // server
printf("In Parent\n");
printf(" parent k = %lu\n", k);
printf(" parent *k = %d\n", *k);
k=k+1;
printf(" parent increased k = %lu\n",k);

}
}

void itoa(long n, char *s)
{
char temp[20];
int i=0,j=0,len;
do
{
temp[i++] = n%10 + '0';
}
while (n/=10);
temp[i]=0;
len = strlen(temp);
for(j=0;j<len;j++)
{
s[j] = temp[len-j-1];
}
s[j]=0;
str=s;
}
 
Old 04-29-2002, 08:13 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
What are you trying to do. Changing the contents of the variable in the server won't change the one in the client. The client gets a copy of the servers data segment as far as I know.

I think k=k+1 should be *k+=1
 
  


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
When I kill parent process, child isn't killed BengeBoy Linux - Software 0 08-19-2004 04:38 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
parent and child processes skora Programming 5 11-02-2003 10:41 AM
about parent and child process winwar Solaris / OpenSolaris 3 07-23-2003 06:07 AM
child-parent-&-fork mukul Programming 2 03-24-2001 01:12 PM


All times are GMT -5. The time now is 12:45 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration