LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   child and parent process error (https://www.linuxquestions.org/questions/programming-9/child-and-parent-process-error-19647/)

jdevanand 04-28-2002 02:05 AM

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;
}

Mik 04-29-2002 08:13 AM

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


All times are GMT -5. The time now is 01:22 AM.