Esempio n. 1
0
/*ARGSUSED*/
int openmem(int trystart)
{
    initpkey();
    shmid = shmget(pkey, SHMFLAG, 0);
    if (shmid < 0) {            /* Could not find the shared memory */
  if (errno != ENOENT) {  /* Error other not created yet */
      perror("shmget");
      exit(1);
  }
  if (trystart==1){          /* Create the memory */
    startdaemon();
  }
  else if (trystart < 0){    /* Just checking if it exists */
    return 0;
  }
  else {                     /* Wanted to use it, but...   */
    fprintf(stderr,"Warning: Daemon not running!\n");
    exit(1);
  }
  shmid = shmget(pkey, SHMFLAG, 0);
  if (shmid < 0) {  /* This is a bummer of an error */
      ERROR(1, ("Daemon not running (err:%d)\n",errno));
      exit(1);
  }
    }
    sharedMemory = (struct memory *) shmat(shmid, 0, 0);
    if (sharedMemory == (struct memory *) -1) {
  printf("Error number: %d\n",errno);
  perror("shared memory");
  exit(1);
    }
    setup_memory(sharedMemory);
    opensem();
    return 1;
}
Esempio n. 2
0
int main()
{
	int	semid, shmid;
	char	*shmaddr;
	
	if ((shmid = createshm(".", 'm', SHM_SIZE)) == -1){
		exit (1);
	}
	
	if((shmaddr = shmat (shmid, (char *)0, 0)) == (char *)-1){
		perror ("attach shared memory error!\n");
		exit (1);
	}
	
	if((semid = opensem("." ,'s')) == -1){
		exit (1);
	}
	
	while(1){
		printf("reader: ");
		sem_p(semid,0);			/* P操作 */
		
		printf("%s", shmaddr);
		
		sem_v(semid,0);			/* V操作 */
	}
}
Esempio n. 3
0
int main()
{
	int semid,shmid;
	char *shmaddr=NULL;

	if((shmid=createshm(".",'m',SHM_SIZE))==-1)
	{
		exit(1);
	}

	if((shmaddr=shmat(shmid,(char*)0,0))==(char *)-1)
	{
		perror("attach shared memory error\n");
		exit(1);
	}

	if((semid=opensem(".",'s'))==-1)
	{
		exit(1);
	}

	while(1)
	{
                waitsem(semid,0);
		p(semid,0);
		printf("reader:");
		printf("%s\n",shmaddr);
		my_sleep(5);
		v(semid,0);
	//	my_sleep(5);
	}
	
}