srikbaba's Profile
Reputation: 0
Apprentice
- Group:
- New Members
- Active Posts:
- 2 (0.01 per day)
- Joined:
- 04-October 12
- Profile Views:
- 48
- Last Active:
Oct 05 2012 06:48 PM- Currently:
- Offline
Previous Fields
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: Problem with pointer to string conversion in mmap()
Posted 5 Oct 2012
Hi,
Sorry to confuse you with the coding. I added the header file and made a few changes. I am not an expert in programming.
Server Code
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> const int SHM_SIZE = 1024; /* make it a 1K shared memory segment */ //const char FILE_NAME[] = "ser1.c"; int main(int argc, char *argv[]) { key_t key; int shmid; int n; FILE *fileptr; char *stop="stop"; char data[1000]; fileptr=fopen("test1.txt","w"); char *shmflag,*shm; int flag=0; // Make the key if ((key = ftok("/Home/r/rameshnn/OS/A2/ser1.c", 1)) == -1) { perror("ftok"); exit(1); } /* connect to (and possibly create) the segment: */ if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) { perror("shmget"); exit(1); } /* attach to the segment to get a pointer to it: */ shm = shmat(shmid, (void *)0, 0); if (shm == (char *)(-1)) { perror("shmat"); exit(1); } /* attach to the segment to get a pointer to it: */ // FLAG POINTER shmflag = shmat(shmid, (void *)0, 0); if (shmflag == (char *)(-1)) { perror("shmat"); exit(1); } while(shmflag==0) { sleep(1); } while(flag==0) { fprintf(fileptr,shm); if(strcmp("stop\n",shm) != 0 ) { flag=0; } else { flag=1; } } fclose(fileptr); /* detach from the segment: */ if (shmdt(shm) == -1) { perror("shmdt"); exit(1); } /* delete the IPC structure */ shmctl(shmid, IPC_RMID, NULL); //exit(0); return 0; }
Client Code
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> const int SHM_SIZE = 1024; /* make it a 1K shared memory segment */ //const char FILE_NAME[] = "ser1.c"; int main(int argc, char *argv[]) { key_t key; int shmid; int flag=0; // FILE *fileptr; char name[1000]; //fileptr=fopen("test.txt","w"); char *shmflag,*shm; // shm=fileptr; // Make the key if ((key = ftok("/Home/r/rameshnn/OS/A2/ser1.c", 1)) == -1) { perror("ftok"); exit(1); } /* connect to (and possibly create) the segment: */ if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) { perror("shmget"); exit(1); } /* attach to the segment to get a pointer to it: */ shm = shmat(shmid, (void *)0, 0); if (shm == (char *)(-1)) { perror("shmat"); exit(1); } /* attach to the segment to get a pointer to it: */ // FLAG POINTER shmflag = shmat(shmid, (void *)0, 0); if (shmflag == (char *)(-1)) { perror("shmat"); exit(1); } shmflag=0; // Write with first character "*" printf("write to segment: \n"); while(flag==0) { fgets(shm, SHM_SIZE, stdin); //*shm=*shm+1; if(strcmp("stop\n",shm) == 0) { flag=1; } } shmflag=1; exit(0); return 0; }
It is looping infintely and printing only the first line of the input. We have to execute the server program first and then client program. When we type "stop" in a new line both programs should stop and the text which was typed before stop should be printed in test1.txt.
My Information
- Member Title:
- New D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
Contact Information
- E-mail:
- Click here to e-mail me
Friends
srikbaba hasn't added any friends yet.
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
srikbaba has no profile comments yet. Why not say hello?