5 Replies - 837 Views - Last Post: 05 October 2012 - 10:22 PM Rate Topic: -----

#1 srikbaba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-October 12

Problem with pointer to string conversion in mmap()

Posted 04 October 2012 - 11:59 PM

Hi, I am trying to write a client/server program which when server process creates a shared memory area by using System V’s shared memory segment. The client started separately on the same machine, can write some text to the shared buffer. The text will be less than 1000 bytes from keyboard or read the first 1000 bytes of a text file. When the server receives a new text from a client, it appends the text to a file. The server stops when it sees a “STOP!!!” string in the shared buffer.

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with pointer to string conversion in mmap()

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1621
  • View blog
  • Posts: 3,079
  • Joined: 30-May 10

Re: Problem with pointer to string conversion in mmap()

Posted 05 October 2012 - 12:18 AM

> serv.c:26: warning: incompatible implicit declaration of built-in function 'exit'
All of these you fix by having
#include <stdlib.h>

> serv.c:56: warning: passing argument 2 of 'strcmp' makes pointer from integer without a cast
Until you fix this, it's a waste of time even trying to run the code. It will most likely just crash anyway.
Ditto for all your other "makes pointer from integer" errors (yes, they are errors, not warnings).

Besides, you managed to edit away enough detail to make further help impossible, since there is no line 56 in your post.

> serv.c:52: warning: format not a string literal and no format arguments
Again, impossible to tell, you edited away the evidence.
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6035
  • View blog
  • Posts: 23,418
  • Joined: 23-August 08

Re: Problem with pointer to string conversion in mmap()

Posted 05 October 2012 - 02:25 AM

How the hell does one get to working with shared memory without being able to solve these basic problems?
Was This Post Helpful? 1
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,301
  • Joined: 25-December 09

Re: Problem with pointer to string conversion in mmap()

Posted 05 October 2012 - 06:53 AM

Probably by the cut/paste method. Notice the lack of any formatting.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 srikbaba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-October 12

Re: Problem with pointer to string conversion in mmap()

Posted 05 October 2012 - 03:25 PM

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.
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1621
  • View blog
  • Posts: 3,079
  • Joined: 30-May 10

Re: Problem with pointer to string conversion in mmap()

Posted 05 October 2012 - 10:22 PM

First, learn how to indent code.
http://sourceforge.n...tle=Indentation
Nobody wants to read code which looks like yours.

Server
#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("foo.txt", 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
#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;
  char name[1000];
  char *shmflag, *shm;

  // Make the key 
  if ((key = ftok("foo.txt", 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);
    if (strcmp("stop\n", shm) == 0) {
      flag = 1;
    }
  }

  shmflag = 1;

  return 0;
}



You're using the same shmid for two different shmat calls.
server.c:37: shm = shmat(shmid, (void *) 0, 0);
server.c:44: shmflag = shmat(shmid, (void *) 0, 0);

client.c:33: shm = shmat(shmid, (void *) 0, 0);
client.c:40: shmflag = shmat(shmid, (void *) 0, 0);



Fix these error messages.
$ gcc -Wall -o shm_server server.c
server.c: In function ‘main’:
server.c:55:5: warning: format not a string literal and no format arguments [-Wformat-security]
server.c:18:8: warning: unused variable ‘data’ [-Wunused-variable]
server.c:17:9: warning: unused variable ‘stop’ [-Wunused-variable]
server.c:15:7: warning: unused variable ‘n’ [-Wunused-variable]

$ gcc -Wall -o shm_client client.c
client.c: In function ‘main’:
client.c:58:11: warning: assignment makes pointer from integer without a cast [enabled by default]
client.c:17:8: warning: unused variable ‘name’ [-Wunused-variable]


In particular, note that
shmflag = 1;
is not the same as
*shmflag = 1;
There are several places in the code where you seem to be trashing the pointer to your shared memory, rather than updating the memory itself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1