http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net
分类: LINUX
2011-12-20 21:39:13
#include
#include
#include
#include
#include
#include
#include
struct A{
int x;
int y;
char name[100];
};
int main()
{
int size=sizeof(struct A);
void *p1,*p2;
int ipc1,ipc2;
struct A *a,*b;
pid_t pid;
int re=-2;
ipc1= shmget(0x1234,size,0600|IPC_CREAT);
printf("the ipc1 is 0x%0xd\n",ipc1);
if ( ipc1== -1)
{
printf("shmget error\n");
exit(1);
}
p1 = shmat(ipc1,NULL,SHM_RND);
a=(struct A*)p1;
printf("first: address of p share_area is %p\n",p1 );
a->x=5;
a->y=20;
strcpy(a->name,"hello,world");
shmdt(p1);
ipc2 = shmget(0x1234,0,0600);
if ( ipc2== -1)
{
printf("shmget error\n");
exit(1);
}
printf("the ipc2 is 0x%0xd\n",ipc2);
p2 = shmat(ipc2,NULL,SHM_RND);
printf("second: address of p share_area is %p\n",p2 );
b=(struct A*)p2;
printf("b->x is: %d\n",b->x);
printf("b->y is: %d\n",b->y);
printf("b->name is:%s\n",b->name);
printf("\n\n");
/*
pid=fork();
if (pid == -1)
{
printf("fork error\n"),
exit(1);
}
else if (pid==0)
{ void *p3;
int ipc3;
struct A *c;
char addr[100];
char tmp[100];
printf("in child\n");
printf("in child second: address of p share_area is %p\n",p2 );
printf("b->x is: %d\n",b->x);
printf("b->y is: %d\n",b->y);
printf("b->name is:%s\n",b->name);
shmdt(p2);
printf("\n\n");
ipc3 = shmget(0x1234,0,0600);
if ( ipc3== -1)
{
printf("shmget error\n");
exit(1);
}
printf("the ipc3 is 0x%0xd\n",ipc3);
p3 = shmat(ipc3,NULL,SHM_RND);
// printf("second: address of p share_area is %p\n",p3 );
c=(struct A*)p3;
printf("c->x is: %d\n",c->x);
printf("c->y is: %d\n",c->y);
printf("c->name is:%s\n",c->name);
printf("\n\n");
memset(addr,0,sizeof(addr));
sprintf(addr,"%p",p3);
printf("string addr is %s\n",addr);
memset(tmp,0,sizeof(tmp));
strncpy(tmp,"world",sizeof("world"));
sleep(2);
execlp("./share_proc2","share_proc2",addr,tmp,NULL);
exit(0);
}
wait(NULL);
*/
printf("in parent 1111\n");
printf("ppid is:%d\n",getppid());
printf("pid is:%d\n",getpid());
printf("\n\n");
///注意 与system("./share_proc2 ") 的区别
re = system("./share_proc2 &");
printf("re 的值为:%d\n",re);
printf("\n\nin parent\n");
fflush(stdout);
exit(0);
}
文件share_proc2.c
#include
#include
#include
#include
#include
#include
#include
struct A{
int x;
int y;
char name[100];
};
int num[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char character[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
int map_arr(char c);
int str2hex(char str[]);
int main(int argc,char **argv)
{
int size=sizeof(struct A);
void *p2;
int ipc2;
struct A *b;
int i;
struct A *aa; //接受外部参数
ipc2 = shmget(0x1234,0,0600);
if ( ipc2== -1)
{
printf("shmget error\n");
exit(1);
}
printf(" in share_proc2\n");
printf("the ipc2 is 0x%0xd\n",ipc2);
p2 = shmat(ipc2,NULL,SHM_RND);
printf("second: address of p share_area is %p\n",p2 );
b=(struct A*)p2;
printf("b->x is: %d\n",b->x);
printf("b->y is: %d\n",b->y);
printf("b->name is:%s\n",b->name);
printf("\n\n");
for (i=0; i < argc; i++)
printf("argv[%d] is %s\n",i,argv[i]);
printf("accept the outside parameter\n");
if (argc > 2)
{
// aa=(struct A*)str2hex(&("0x1234"[2]));
fflush(stdout);
printf("hello\n");
aa=(struct A*)str2hex(&(argv[1][2]));
printf("aa 的值为:%p\n",aa);
printf("nihao\n");
printf("aa->x is: %d\n",aa->x);
printf("aa->y is: %d\n",aa->y);
printf("aa->name is:%s\n",aa->name);
printf("shijie\n");
fflush(stdout);
}
printf("first:\n");
printf("ppid is:%d\n",getppid());
printf("pid is:%d\n",getpid());
printf("\n\n");
//for (i=0; i < 1000; i++)
// printf("at loop\n");
sleep(3);
printf("second:\n");
printf("ppid is:%d\n",getppid());
printf("pid is:%d\n",getpid());
printf("\n\n");
fflush(stdout);
exit(0);
}
int map_arr(char c)
{
int j;
for (j=0; j < 16; j++)
if(c==character[j])
return j;
}
int str2hex(char str[])
{
int len;
int i,j;
int sum=0;
len =strlen(str);
for (i=0; i < len; i++)
{
j=map_arr(str[i]);
sum=16*sum + num[j];
}
return sum;
}