I am getting the following warnings because of which I am getting the wrong output too. The output also prints stop and the text written after that.
Warnings in Server Program
serv.c: In function 'main':
serv.c:26: warning: incompatible implicit declaration of built-in function 'exit'
serv.c:33: warning: incompatible implicit declaration of built-in function 'exit'
serv.c:40: warning: incompatible implicit declaration of built-in function 'exit'
serv.c:52: warning: format not a string literal and no format arguments
serv.c:55: warning: passing argument 2 of '__builtin___strcpy_chk' makes pointer from integer without a cast
serv.c:55: warning: passing argument 2 of '__inline_strcpy_chk' makes pointer from integer without a cast
serv.c:56: warning: passing argument 2 of 'strcmp' makes pointer from integer without a cast
Warnings in Client Program
client.c: In function 'main':
client.c:29: warning: incompatible implicit declaration of built-in function 'exit'
client.c:36: warning: incompatible implicit declaration of built-in function 'exit'
client.c:43: warning: incompatible implicit declaration of built-in function 'exit'
client.c:56: warning: incompatible implicit declaration of built-in function 'exit'
Code for Server program
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <unistd.h>
#include <string.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 data[1000];
fileptr=fopen("test.txt","w");
char *s,*shm;
// Make the key
if ((key = ftok("/Home/g/govindsh/osassign2/serv.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);
}
//fprintf(fileptr,shm);
//fclose(fileptr);
int i = strcmp(a,shm);
fileptr=fopen("test.txt","w+");
if(i==0)
break;
else
{
fprintf(fileptr,"%s",shm);
shm++;
}
return 0;
}
Code for Client Program
// Client
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.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 name[1000];
fileptr=fopen("test.txt","w");
char *s,*shm;
// shm=fileptr;
// Make the key
if ((key = ftok("/Home/g/govindsh/osassign2/serv.c", 1)) == -1) {
perror("ftok");
exit(0);
}
/* connect to (and possibly create) the segment: */
if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) {
perror("shmget");
exit(0);
}
/* attach to the segment to get a pointer to it: */
shm = shmat(shmid, (void *)0, 0);
if (shm == (char *)(-1))
{
perror("shmat");
exit(0);
}
// Write with first character "*"
printf("write to segment: \n");
fgets(shm, SHM_SIZE, stdin);
/* detach from the segment: */
if (shmdt(shm) == -1) {
perror("shmdt");
exit(0);
}
/* delete the IPC structure */
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
I think the problem is in the server program in string comparison but not able to resolve it. Kindly help.

New Topic/Question
Reply



MultiQuote





|