a program based on shared memory
Please check these programs.....I got an error as segmentation fault...Coudn't find the mistake....
It is a program to just exchange a set of strings via shared memory.... ////////////////////////////// 1st program ////////////////////////////// shmclient2.c ///////////////////////////// #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/shm.h> #include<string.h> #define SIZE 100 int main() { char *shm[50],*s[50],n[10][10]; char msg[10]="end",change[10]="read"; key_t key; char c; int shmid,i; key=7858; /*locate the shared memory with key=7856.Here vomit the IPC_CREAT */ if((shmid=shmget(key,SIZE,0666))<0) { perror("shmget"); exit(1); } /*attach to our data space*/ if((shm[0]=shmat(shmid,0,0))==(char *)-1) { perror("shmat"); exit(1); } /*read data from shared memory*/ //for(i=0,s[0]=shm[0];s[i]!="end";i++) i=0; s[0]=shm[0]; while(strcmp(s[i],msg)!=0) { printf("%s\n",s[i]); } /*change the first character indicating that this reads data*/ strcpy(shm[0],change); exit(0); } /////////////////////////// 2nd program /////////////////////////// shmserver2.c /////////////////////////// #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/shm.h> #include<string.h> #define SIZE 100 int main() { char *shm[50],*s[50],n[50][50]; char msg[10]="end",change[10]="read"; key_t key; char c; int shmid,i,num; key=7858; /*create shared memory segment*/ if((shmid=shmget(key,SIZE,IPC_CREAT|0666))<0) { perror("shmget"); exit(1); } /*attach that to our data space*/ if((shm[0]=shmat(shmid,0,0))==(char *)-1) { perror("shmat"); exit(1); } /*put data on the shared memory*/ s[0]=shm[0]; printf("Enter 3 strings\n"); i=0; do { printf("\nEnter string %d",i+1); scanf("%s",n[i]); strcpy(s[i],n[i]); i++; }while(i<3); strcpy(s[i],msg); while(strcmp(s[i],msg)!=0) { printf("\ntest :%s",s[i]); } /*wait until other process changes the first string in the memory indicating that it reads what we put*/ while(strcmp(shm[0],change)!=0) sleep(1); exit(0); } |
Use CODE tags when posting code.
I don't think this belongs in Newbie. I mean it's not a post about being new to Linux and having some difficulty is it? If I were you I'd use the Report button to ask a mod to move it to the programming forum where it's more likely to be seen by people who are able to help you. |
Quote:
Quote:
Quote:
Quote:
Quote:
|
All times are GMT -5. The time now is 06:45 AM. |