Problem : Sending Array to Client Process Using Shared Memory

This is a discussion on Problem : Sending Array to Client Process Using Shared Memory within the C Programming forums, part of the General Programming Boards category; hi i want to send an array to a shared memory.but it should send 4 values at a time. this ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Smile Problem : Sending Array to Client Process Using Shared Memory

    hi i want to send an array to a shared memory.but it should send 4 values at a time.
    this code is working. but the client process doesn't read the 1st 4 numbers correctly. can some one help me please.
    here is the code,
    Last edited by dakshina11; 03-26-2010 at 01:08 AM. Reason: wrong code

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Question apologize

    apologize me i've put the wrong code,
    it should be,
    main process,
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<sys/ipc.h>
    #include<sys/shm.h>
    #define SHMSZ 4
    #define SHMSZ2 1
    
    main()
    {
    	int noofvals=0,i,num,shmid,*shm,*s,shmid2,j;
    	char *shm2,*s2;
    	printf("Enter the Number Values you enter\n");
    	scanf("%d",&noofvals);
    	int *arr=malloc(noofvals*sizeof(int));
    	for(i=0;i<noofvals;i++)
    	{
    		printf("Enter the Number %d\n",i+1);
    		scanf("%d",&num);
    		arr[i]=num;
    	}
    	printf("Numbers Read\n");
    	for(i=0;i<noofvals;i++)
    	{
    		printf("%d,",arr[i]);
    	}
    	printf("\n");
    
    	key_t key;
    	key=5678;
    	if((shmid=shmget(key, SHMSZ, IPC_CREAT | 0666))<0)
    	{
    		perror("shmget");
    		exit(0);
    	}
    	if ((shm = shmat(shmid, NULL, 0)) == (int *) -1) 
    	{
    	  perror("shmat");
    	  exit(1);
    	}
    	
    	key_t key2;
    	key2=5679;
    	if((shmid2=shmget(key2, SHMSZ2, IPC_CREAT | 0666))<0)
    	{
    		perror("shmget");
    		exit(0);
    	}
    	if((shm2=shmat(shmid2, NULL, 0))==(char *) -1)
    	{
    		perror("shmat");
    		exit(0);
    	}
    	
    // 	sending the Number of Values
    
    	printf("Sending the Number of Elements to Receive\n");
    	*shm2='w';
    	s=shm;
    	*s=noofvals;
    	while(*shm2!='r')
    	{
    	  wait(1);//waiting for Clients Reply
    	}
    	
    	printf("Element Count Sent\n");
    	*shm=NULL;
    // 	sending the Elements
    	num=0;
    	*shm2='w';
    	while(num<noofvals)
    	{
    
    	  for(i=0;i<4;i++)
    	  {
    	    printf("Sending %d\n",arr[num]);
    	    *s=arr[num];
    	    num++;
    	    if(num>=noofvals)
    	    {break;}
    	    *s++;
    	  }
    	  printf("\n");
    	  *shm2='w';
    	  while(*shm2!='r')
    	  {
    	    wait(1);//waiting for Client's Reply
    	  }
    	}
    	s=shm;
    	for(i=0;i<noofvals;i++)
    	  {
    	    printf("Sent val %d\n",*s);
    	    *s++;
    	  }
    	*shm2='w';
    //	Server Exiting
    	printf("Server Exiting\n");
    }


    client process,

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<sys/ipc.h>
    #include<sys/shm.h>
    #define SHMSZ 4
    #define SHMSZ2 1
    main()
    {
      	int noofvals=0,i,num,shmid,*shm,*s,shmid2,j;
    	char *shm2,*s2;
    	key_t key;
    	key=5678;
    	key_t key2;
    	key2=5679;
    	if((shmid=shmget(key, SHMSZ, 0666))<0)
    	{
    		perror("shmget");
    		printf("error 1");
    		exit(0);
    	}	
    	if((shmid2=shmget(key2, SHMSZ2, 0666))<0)
    	{
    		perror("shmget");
    		exit(0);
    	}
    	if ((shm = shmat(shmid, NULL, 0)) == (int *) -1) 
    	{
    	  perror("shmat");
    	  exit(1);
    	}
    	if((shm2=shmat(shmid2, NULL, 0))==(char *) -1)
    	{
    		perror("shmat");
    		exit(0);
    	}
    // 	wait till server Sends the Number of Elements
    	while(*shm2!='w')
    	{
    	  wait(1);//waiting for Server to Send Data
    	}
    	noofvals=*shm;
    	*shm=NULL;
    	printf("Received the Number of Elements as %d\n",noofvals);
    	*shm2='r';
    	int arr[noofvals];
    	
    // 	Receive Elements
    	printf("Waiting\n");
    	while(*shm2!='w')
    	{
    	  wait(1);//waiting for Server to Send Data
    	}
    	s=shm;
    	num=0;
    	while(num<noofvals)
    	{
    	  for(i=0;i<4;i++)
    	  {
    	    printf("Received %d\n",*s);
    	    arr[num]=*s;
    	    num++;
    	    if(num>=noofvals)
    	    {break;}
    	    s++;
    	  }
    	  
    	  printf("\n");
    	  *shm2='r';
    	  while(*shm2!='w')
    	  {
    	    wait(1);//waiting for Client's Reply
    	  }
    	}
    	*shm2='r';
    
    	  
    	int div=(int) ((noofvals/2)+1);
    	printf("\n\ndiv main - %d, div main2 - %d  \n\n",div,(noofvals-div));
    	int *ar1=malloc(div*sizeof(int));
    	int *ar2=malloc((noofvals-div)*sizeof(int));
    	
    	for(i=0;i<div;i++)
    	{
    	  ar1[i]=arr[i];
    	  printf("div - %d - %d\n",i,ar1[i]);
    	}
    
    	printf("\n");
    	int lp=noofvals-div;
    	for(i=0;i<lp;i++)
    	{
    	  ar2[i]=arr[div];
    	  printf("div - %d - %d\n",div,ar2[i]);
    	  div++;
    	}
    	
    	for(i=0;i<div;i++)
    	{
    	  printf("ptr arr %d\n",ar1[i]);
    	}
    	for(i=0;i<lp;i++)
    	{
    	  printf("ptr arr2 %d\n",ar2[i]);
    	}
    }
    Last edited by dakshina11; 03-26-2010 at 12:52 AM. Reason: wrong code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 06:36 PM
  3. RE: client/server shared memory problem
    By hampycalc in forum C Programming
    Replies: 0
    Last Post: 03-10-2006, 01:26 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21