server program recived

                server program recived



#include <stdio.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
 key_t key = 10;
 int semid = semget(key,1,IPC_CREAT | 0666);
 if(semid == -1)
 {
  perror("semget failed :");
  exit(1);
 }
 else
 {
  printf("semid : %d\n",semid);
  int ctl = semctl(semid,0,GETVAL);
  if(ctl == -1)
  {
   perror("semctl failed :");
   exit(1);
  }
 // else if(ctl)
 // {
   printf("semaphore initialized\n");
 struct sembuf buf={0,-1,0};//user will release semaphore
   int op = semop(semid,&buf,1);
   if(op == -1)
   {
    perror("semop falied :");
    exit(1);
   }
   else
   {
    printf("semaphore Accessed!\n");
  /////////////////
  key_t key1 = 20;
  int shmid = shmget(key1,10000,IPC_CREAT | 0660);
  if(shmid == -1)
  {
   perror("Shmget failed :");
   exit(1);
  }
  else
  {
   printf("shmid : %d\n",shmid);
   char *ptr = shmat(shmid,NULL,0); //RDWR
   if(ptr)
   {
    printf("Attached \n");
    ////////////////////
   // strcpy(ptr,"hello world");
    printf("data from SHM : %s\n",ptr);
    ////////////////////
   }
   printf("Detaching shared memory segment\n");
   shmdt(ptr);
  }
  /////////////////
   }
   printf("Releasing semaphore\n");
   semctl(semid,0,SETVAL,1);
 /* }
  else
  {
   printf("semaphore is buzy\n");
   exit(1);
  }
 */}
}





Comments

Popular posts from this blog

TCP SERVER program