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