unix - shared memory programme is not working in c -
there 2 programs there 1 call server put content in shared memory , other client received content shared memory both in both programs attached shared memory data not display in client side.
client.c
#include<fcntl.h> #include<sys/ipc.h> #include<sys/shm.h> void main(int argc,char * argv[]) { int shmid=shmget(124,70,0777); char * data; printf("%d\n",shmid); data=shmat(shmid,0,0); printf("%s",data); }
server.c
#include<fcntl.h> #include<sys/ipc.h> #include<sys/shm.h> void main(int argc,char * argv[]) { int shmid=shmget(124,70,0777|ipc_creat); char * data,*ptr; printf("%d\n",shmid); if((data=shmat(shmid,0,0))==(char *)-1); { printf("no attach\n"); } ptr=data; memset(data,0,1024); printf("%s",data); char c[]="my name milap pancholi"; int i=0; for(i=0;i<sizeof(c);i++) { printf("%c",c[i]); data+=c[i]; } printf("%s\n",ptr); }
your main problem this:
data+=c[i];
this pointer arithmetic, advancing data
, not want @ all. replace with:
data[i] = c[i];
other issues:
main
returnsint
, not void. useint main() { ... }
if don't need arguments count , values (to avoid warnings, should turn way up).- you're missing
#include <stdio.h>
printf
- you're missing
#include <string.h>
memset
Comments
Post a Comment