• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ shmctl函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中shmctl函数的典型用法代码示例。如果您正苦于以下问题:C++ shmctl函数的具体用法?C++ shmctl怎么用?C++ shmctl使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了shmctl函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: dsm_impl_sysv


//.........这里部分代码省略.........
		 */
		ident_cache = MemoryContextAlloc(TopMemoryContext, sizeof(int));

		/*
		 * When using shmget to find an existing segment, we must pass the
		 * size as 0.  Passing a non-zero size which is greater than the
		 * actual size will result in EINVAL.
		 */
		segsize = 0;

		if (op == DSM_OP_CREATE)
		{
			flags |= IPC_CREAT | IPC_EXCL;
			segsize = request_size;
		}

		if ((ident = shmget(key, segsize, flags)) == -1)
		{
			if (errno != EEXIST)
			{
				int			save_errno = errno;

				pfree(ident_cache);
				errno = save_errno;
				ereport(elevel,
						(errcode_for_dynamic_shared_memory(),
						 errmsg("could not get shared memory segment: %m")));
			}
			return false;
		}

		*ident_cache = ident;
		*impl_private = ident_cache;
	}

	/* Handle teardown cases. */
	if (op == DSM_OP_DETACH || op == DSM_OP_DESTROY)
	{
		pfree(ident_cache);
		*impl_private = NULL;
		if (*mapped_address != NULL && shmdt(*mapped_address) != 0)
		{
			ereport(elevel,
					(errcode_for_dynamic_shared_memory(),
				   errmsg("could not unmap shared memory segment \"%s\": %m",
						  name)));
			return false;
		}
		*mapped_address = NULL;
		*mapped_size = 0;
		if (op == DSM_OP_DESTROY && shmctl(ident, IPC_RMID, NULL) < 0)
		{
			ereport(elevel,
					(errcode_for_dynamic_shared_memory(),
				  errmsg("could not remove shared memory segment \"%s\": %m",
						 name)));
			return false;
		}
		return true;
	}

	/* If we're attaching it, we must use IPC_STAT to determine the size. */
	if (op == DSM_OP_ATTACH)
	{
		struct shmid_ds shm;

		if (shmctl(ident, IPC_STAT, &shm) != 0)
		{
			ereport(elevel,
					(errcode_for_dynamic_shared_memory(),
					 errmsg("could not stat shared memory segment \"%s\": %m",
							name)));
			return false;
		}
		request_size = shm.shm_segsz;
	}

	/* Map it. */
	address = shmat(ident, NULL, PG_SHMAT_FLAGS);
	if (address == (void *) -1)
	{
		int			save_errno;

		/* Back out what's already been done. */
		save_errno = errno;
		if (op == DSM_OP_CREATE)
			shmctl(ident, IPC_RMID, NULL);
		errno = save_errno;

		ereport(elevel,
				(errcode_for_dynamic_shared_memory(),
				 errmsg("could not map shared memory segment \"%s\": %m",
						name)));
		return false;
	}
	*mapped_address = address;
	*mapped_size = request_size;

	return true;
}
开发者ID:adam8157,项目名称:gpdb,代码行数:101,代码来源:dsm_impl.c


示例2: main


//.........这里部分代码省略.........

      /* Exchange envids */
      write(writefd, &env, sizeof(env));
      read(pfdr[0], &child, sizeof(child));
      printf("parent: %d child %d\n", env, child);

      printf("YIELD\n");

      gettimeofday(&s, NULL);
      val = 0;
      for (i = 0; i < N; i++) {
	  *shmaddr = val +  1;
	  val += 2;
	  do {
	      yield(child);
	  } while (*shmaddr < val);
      }
      gettimeofday(&t, NULL);
      printf("%d: yield: in %lu usec\n", N, 
	     (t.tv_sec - s.tv_sec) * 1000000 + t.tv_usec - s.tv_usec);

      gettimeofday(&s, NULL);
      for (i = 0; i < N; i++) {
	  *shmaddr = val +  1;
	  val += 2;
	  do {
	      yield(-1);
	  } while (*shmaddr < val);
      }
      gettimeofday(&t, NULL);
      printf("%d: undirected yield: in %lu usec\n", N, 
	     (t.tv_sec - s.tv_sec) * 1000000 + t.tv_usec - s.tv_usec);

      if (shmctl(sid, IPC_RMID, 0)) {
	  perror("shmctl");
      }

      printf("PINGPONG one byte\n");
    
      aarray[0] = 0;

      gettimeofday(&s, NULL);
      for (i = 0; i < N; i++) {
	  write(writefd, &aarray[0], 1);
	  yield(child);
	  read(pfdr[0], &aarray[0], 1);
      }
      gettimeofday(&t, NULL);
      printf("%d: pingpong: %d bytes in %lu usec\n", N, 1, 
	     (t.tv_sec - s.tv_sec) * 1000000 + t.tv_usec - s.tv_usec);

      printf("PINGPONG %d bytes\n", SIZE);

      gettimeofday(&s, NULL);
      for (i = 0; i < N; i++) {
	  write(writefd, &aarray[0], SIZE);
	  read(pfdr[0], &aarray[0], SIZE);
      }
      gettimeofday(&t, NULL);

      printf("%d pingpong: %d bytes in %ld usec\n", N, SIZE, 
	     (t.tv_sec - s.tv_sec) * 1000000 + t.tv_usec - s.tv_usec);
      close(pfdw[0]);

      printf("TESTING HUMONGOUS WRITE TO PIPE\n");
      for (i = 0; i < TESTSZ; i++) {
开发者ID:aunali1,项目名称:exopc,代码行数:67,代码来源:test_pipe.c


示例3: do_shm

static NOINLINE void do_shm(void)
{
	int maxid, shmid, id;
	struct shmid_ds shmseg;
	struct shm_info shm_info;
	struct shminfo shminfo;
	struct ipc_perm *ipcp = &shmseg.shm_perm;
	struct passwd *pw;

	maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
	if (maxid < 0) {
		printf("kernel not configured for %s\n", "shared memory");
		return;
	}

	switch (format) {
	case LIMITS:
		printf("------ Shared Memory %s --------\n", "Limits");
		if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
			return;
		/* glibc 2.1.3 and all earlier libc's have ints as fields
		 * of struct shminfo; glibc 2.1.91 has unsigned long; ach */
		printf("max number of segments = %lu\n"
				"max seg size (kbytes) = %lu\n"
				"max total shared memory (pages) = %lu\n"
				"min seg size (bytes) = %lu\n",
				(unsigned long) shminfo.shmmni,
				(unsigned long) (shminfo.shmmax >> 10),
				(unsigned long) shminfo.shmall,
				(unsigned long) shminfo.shmmin);
		return;

	case STATUS:
		printf("------ Shared Memory %s --------\n", "Status");
		printf("segments allocated %d\n"
				"pages allocated %lu\n"
				"pages resident  %lu\n"
				"pages swapped   %lu\n"
				"Swap performance: %lu attempts\t%lu successes\n",
				shm_info.used_ids,
				shm_info.shm_tot,
				shm_info.shm_rss,
				shm_info.shm_swp,
				shm_info.swap_attempts, shm_info.swap_successes);
		return;

	case CREATOR:
		printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
		printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
				"shmid", "perms", "cuid", "cgid", "uid", "gid");
		break;

	case TIME:
		printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
		printf("%-10s %-10s %-20s %-20s %-20s\n",
				"shmid", "owner", "attached", "detached", "changed");
		break;

	case PID:
		printf("------ Shared Memory %s --------\n", "Creator/Last-op");
		printf("%-10s %-10s %-10s %-10s\n",
				"shmid", "owner", "cpid", "lpid");
		break;

	default:
		printf("------ Shared Memory %s --------\n", "Segments");
		printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
				"key", "shmid", "owner", "perms", "bytes", "nattch",
				"status");
		break;
	}

	for (id = 0; id <= maxid; id++) {
		shmid = shmctl(id, SHM_STAT, &shmseg);
		if (shmid < 0)
			continue;
		if (format == CREATOR) {
			print_perms(shmid, ipcp);
			continue;
		}
		pw = getpwuid(ipcp->uid);
		switch (format) {
		case TIME:
			if (pw)
				printf("%-10d %-10.10s", shmid, pw->pw_name);
			else
				printf("%-10d %-10d", shmid, ipcp->uid);
			/* ctime uses static buffer: use separate calls */
			printf(" %-20.16s", shmseg.shm_atime
					? ctime(&shmseg.shm_atime) + 4 : "Not set");
			printf(" %-20.16s", shmseg.shm_dtime
					? ctime(&shmseg.shm_dtime) + 4 : "Not set");
			printf(" %-20.16s\n", shmseg.shm_ctime
					? ctime(&shmseg.shm_ctime) + 4 : "Not set");
			break;
		case PID:
			if (pw)
				printf("%-10d %-10.10s", shmid, pw->pw_name);
			else
				printf("%-10d %-10d", shmid, ipcp->uid);
//.........这里部分代码省略.........
开发者ID:beyond2002,项目名称:GT813C,代码行数:101,代码来源:ipcs.c


示例4: main

int main(void){

	pid_t pere,pid1,pid2,pid3;
	int msg,i=1;
	//key_t key = ftok(CLE, 'a');


	msg = msgget(CLE,IPC_CREAT|IPC_EXCL | 0600);
	if(msg == -1){
		perror("Probleme avec la file");
	}

	pere = getpid();
	while(pere == getpid() && i<4){
		
		if(fork()==0){


			printf("Processus %i cree\n",i);
			message m;
			strcpy(m.data,"je suis le process ");
			char tmp[2];
			sprintf(tmp,"%d",i);
			strcat(m.data,tmp);
			m.mtype = 5;

			msgsnd(msg,&m,1000,IPC_NOWAIT);
			
			int mymemory;
			mymemory = shmget(IPC_PRIVATE,sizeof(char),0666);

			char* c = (char*) shmat(mymemory,NULL,0);
			
			cleMemoire cm;
			

			if(i>1){
			cm.id = i -1;
			cm.cle = mymemory;
			strcpy(cm.data,m.data);
			printf("Je suis %i et j'envoie à %i\n",i,(int)cm.id);
			msgsnd(msg,&cm,1000,0);
			}
			if(i<3){
			msgrcv(msg,&cm,1000,i,0);
			printf("Je suis %i et j'ai recu : %s\n",i,cm.data);
			}
			shmdt(c);
			shmctl(mymemory,IPC_RMID,NULL);

			printf("JE MEURS %i\n",i);
			exit(0);
		}
		i++;
	}
		message m;

		for(i=1;i<4;i++) wait(NULL);
		for(i=1;i<4;i++){
			//printf("Je lis %i\n",i);
			msgrcv(msg,&m,1000,5,0);
			printf("%s\n",m.data);
			//printf("J'ai lu %i\n",i);
		}
		msgctl(msg,IPC_RMID,0);
		printf("Je m'en vais, bisous\n");
		return 0;

}
开发者ID:ValentinJ,项目名称:TheLastHope,代码行数:69,代码来源:TestChaine.c


示例5: main

int main(int argc, char* argv[]) {

	key_t cle;

	int taille = 0;

	int nb_fils, i;

	if (argc != 2) {
		printf("%derror syntaxe : %s nbProcessus \n", argc, argv[0]);
		exit(1);
	}
	nb_fils = atoi(argv[1]);

	/*------------------------------------------------------*/
	/* creation segment memoir	*/
	/*------------------------------------------------------*/

	taille = (nb_fils + 1) * sizeof(int);

	char code = 0;

	code = getpid() & 255;
	if (code == 0) {
		printf("error code = 0");
		exit(-1);
	}
	if ((cle = ftok(argv[0], code)) == -1) {
		perror("ftok");
		exit(-1);
	}
	if ((shm_id = shmget(cle, taille, IPC_CREAT | 0666)) < 0) {
		perror("shmget");
		exit(-1);
	}

	if ((adr_att = shmat(shm_id, NULL, 0600)) == (void *) -1) {
		perror("shmat");
		exit(-1);
	}
	tab_val_random = (int*) adr_att;
	tab_val_random[0] = 0;

	/*------------------------------------------------------*/
	/* traitement liberation memoir partage	*/
	/*------------------------------------------------------*/
	remonte_partagee(nb_fils);
	while (tab_val_random[0] == 0) {
		printf("attend while %d \n", tab_val_random[0]);
		sleep(1);
	}

	int result = 0;
	for (i = 1; i < nb_fils + 1; i++) {

		printf("shm%d recu %d \n", i, tab_val_random[i]);
		result += tab_val_random[i];
	}
	printf("pere %d : la somme est %d \n", getpid(), result);
	shmdt(adr_att);
	shmctl(shm_id, IPC_RMID, NULL);

	return EXIT_SUCCESS;
}
开发者ID:naaf,项目名称:Master,代码行数:64,代码来源:remonte_partagee.c


示例6: main

int main(){

	  /************DICHIARAZIONE DELLE VARIABILI***************/

     int id_sem,id_shared,k;
     key_t chiave=IPC_PRIVATE;
     key_t c_sem=IPC_PRIVATE;
     int status;
     Buffer *ptr_sh;

     pid_t pid;
     int num_processi=10;

  /************RICHIESTA DEL SEGMENTO DI MEMORIA CONDIVISA***********/


     id_shared=shmget(chiave,sizeof(Buffer),IPC_CREAT|0664);
     printf(" id_shared=%d \n",id_shared);
     ptr_sh=(Buffer*) (shmat(id_shared,0,0));

//   Inzializzazione struttura dati
     ptr_sh->numlettori=0;
     ptr_sh->numscrittori=0;
     ptr_sh->messaggio=0;	


//   richiesta dei semafori
     id_sem=semget(c_sem,4,IPC_CREAT|0664);

//   inizializzazione
     semctl(id_sem,SYNCH,SETVAL,1);
     semctl(id_sem,MUTEXL,SETVAL,1);
     semctl(id_sem,MUTEXS,SETVAL,1);
     semctl(id_sem,MUTEX,SETVAL,1);



  /****GENERAZIONE DEI PROCESSI E OPERAZIONI DI R/W****/


    //generazione di scrittori e lettori
     for (k=0;k<num_processi;k++) {

	pid=fork();

	if (pid==0)  {                //processo figlio
	  if ( (k%2)==0) {
	     printf("sono il figlio scrittore. Il mio pid %d \n",getpid());
	     Scrittore(id_sem,ptr_sh);
          }else {
             printf("sono il figlio lettore. Il mio pid %d \n",getpid());
             Lettore(id_sem,ptr_sh);
          } 
          exit(0);
	} 
     }


     for (k=0; k<num_processi;k++){
       pid=wait(&status);
       if (pid==-1)
		perror("errore");
	else
		 printf ("Figlio n.ro %d e\' morto con status= %d \n ",pid,status);
     }
     shmctl(id_shared,IPC_RMID,0);
     semctl(id_sem,0,IPC_RMID);

     return 1;

}
开发者ID:robertovela,项目名称:sisop,代码行数:71,代码来源:lettore_scrittore.c


示例7: gimp_plug_in_shm_new

GimpPlugInShm *
gimp_plug_in_shm_new (void)
{
  /* allocate a piece of shared memory for use in transporting tiles
   *  to plug-ins. if we can't allocate a piece of shared memory then
   *  we'll fall back on sending the data over the pipe.
   */

  GimpPlugInShm *shm = g_slice_new0 (GimpPlugInShm);

  shm->shm_ID = -1;

#if defined(USE_SYSV_SHM)

  /* Use SysV shared memory mechanisms for transferring tile data. */
  {
    shm->shm_ID = shmget (IPC_PRIVATE, TILE_MAP_SIZE, IPC_CREAT | 0600);

    if (shm->shm_ID != -1)
      {
        shm->shm_addr = (guchar *) shmat (shm->shm_ID, NULL, 0);

        if (shm->shm_addr == (guchar *) -1)
          {
            g_printerr ("shmat() failed: %s\n" ERRMSG_SHM_DISABLE,
                        g_strerror (errno));
            shmctl (shm->shm_ID, IPC_RMID, NULL);
            shm->shm_ID = -1;
          }

#ifdef IPC_RMID_DEFERRED_RELEASE
        if (shm->shm_addr != (guchar *) -1)
          shmctl (shm->shm_ID, IPC_RMID, NULL);
#endif
      }
    else
      {
        g_printerr ("shmget() failed: %s\n" ERRMSG_SHM_DISABLE,
                    g_strerror (errno));
      }
  }

#elif defined(USE_WIN32_SHM)

  /* Use Win32 shared memory mechanisms for transferring tile data. */
  {
    gint  pid;
    gchar fileMapName[MAX_PATH];

    /* Our shared memory id will be our process ID */
    pid = GetCurrentProcessId ();

    /* From the id, derive the file map name */
    g_snprintf (fileMapName, sizeof (fileMapName), "GIMP%d.SHM", pid);

    /* Create the file mapping into paging space */
    shm->shm_handle = CreateFileMapping (INVALID_HANDLE_VALUE, NULL,
                                         PAGE_READWRITE, 0,
                                         TILE_MAP_SIZE,
                                         fileMapName);

    if (shm->shm_handle)
      {
        /* Map the shared memory into our address space for use */
        shm->shm_addr = (guchar *) MapViewOfFile (shm->shm_handle,
                                                  FILE_MAP_ALL_ACCESS,
                                                  0, 0, TILE_MAP_SIZE);

        /* Verify that we mapped our view */
        if (shm->shm_addr)
          {
            shm->shm_ID = pid;
          }
        else
          {
            g_printerr ("MapViewOfFile error: %d... " ERRMSG_SHM_DISABLE,
                        GetLastError ());
          }
      }
    else
      {
        g_printerr ("CreateFileMapping error: %d... " ERRMSG_SHM_DISABLE,
                    GetLastError ());
      }
  }

#elif defined(USE_POSIX_SHM)

  /* Use POSIX shared memory mechanisms for transferring tile data. */
  {
    gint  pid;
    gchar shm_handle[32];
    gint  shm_fd;

    /* Our shared memory id will be our process ID */
    pid = gimp_get_pid ();

    /* From the id, derive the file map name */
    g_snprintf (shm_handle, sizeof (shm_handle), "/gimp-shm-%d", pid);

//.........这里部分代码省略.........
开发者ID:STRNG,项目名称:gimp,代码行数:101,代码来源:gimppluginshm.c


示例8: P_shmctl

int P_shmctl(int shmid, int cmd, struct shmid_ds *buf) { return shmctl(shmid, cmd, buf); }
开发者ID:vishalmistry,项目名称:imitate,代码行数:1,代码来源:osfKludges.C


示例9: LOG

XCODE_IPC_DESCR_T *xcode_ipc_open(unsigned int sz, key_t key) {
  XCODE_IPC_DESCR_T *pIpc = NULL;
  struct shmid_ds buf;
  int shmid;
  //int rc;
#ifdef __linux__
  int flags = 0x180;
#else
  int flags = SEM_R | SEM_A;
#endif // __linux__

  buf.shm_segsz = 0;
  if((shmid = shmget(key, 0, flags)) != -1) {
    // get the segment size of any existing shm
    if(shmctl(shmid, IPC_STAT, &buf) != 0) {
      return NULL;
    }
  }

  if(buf.shm_segsz == 0) {
    LOG(X_ERROR("xcode shared mem not yet created by writer"));
    return NULL;
  }

  if(buf.shm_segsz < sz) {
    LOG(X_ERROR("xcode shared mem size %d below minimum %d"), buf.shm_segsz, sz);
    return NULL;
  }

  pIpc = avc_calloc(1, sizeof(XCODE_IPC_DESCR_T));
  pIpc->key = key;
  pIpc->sz = sz;

  if((pIpc->shmid = shmget(pIpc->key, pIpc->sz, flags)) == -1) {
    LOG(X_ERROR("xcode shmget failed for key: 0x%x flags: 0x%x size: %u"), 
                pIpc->key, flags, pIpc->sz);
    free(pIpc);
    return NULL;
  }

  if(shmctl(pIpc->shmid, IPC_STAT, &buf) != 0) {
    LOG(X_ERROR("xcode shmctl failed for shmid %d"), pIpc->shmid);
    free(pIpc);
    return NULL;
  }

  if(buf.shm_nattch > 1) {
    LOG(X_ERROR("xcode shared mem already has %d attached"), buf.shm_nattch);
    free(pIpc);
    return NULL;
  }

  if((pIpc->pmem = shmat(pIpc->shmid, 0, 0)) == (void *) -1) {
    LOG(X_ERROR("xcode shmat failed"));
    free(pIpc);
    return NULL;
  }

  if((pIpc->sem_srv = sem_open(XCODE_IPC_SEM_NAME_SRV, 0,
                        S_IRWXU | S_IRWXG | S_IRWXO , 0)) == SEM_FAILED) {
    LOG(X_ERROR("xcode sem_open server failed with "ERRNO_FMT_STR), ERRNO_FMT_ARGS);
    pIpc->sem_srv = NULL;
    return NULL;
  }

  if((pIpc->sem_cli = sem_open(XCODE_IPC_SEM_NAME_CLI, 0,
                        S_IRWXU | S_IRWXG | S_IRWXO , 0)) == SEM_FAILED) {
    LOG(X_ERROR("xcode sem_open client failed with "ERRNO_FMT_STR), ERRNO_FMT_ARGS);
    pIpc->sem_cli = NULL;
    return NULL;
  }

  pIpc->pmem->hdr.cmd = XCODE_IPC_CMD_NONE;

  //fprintf(stderr, "init ipc ok\n");

  return pIpc;
}
开发者ID:amirsdream,项目名称:openvcx,代码行数:78,代码来源:xcode_ipc.c


示例10: Close

void * CBUShm::Open(const char *name, int shmsize)
{
   char buf[256];
   Close();
   sprintf(buf,"%s.shm",name);
   m_fileno = sh_open(buf,O_CREAT|O_RDWR,SH_DENYNO);
   if (m_fileno==-1)
   {
      sprintf(m_szmsg,"Cannot open SHM File for '%s' size=%d",name,shmsize);
      return(NULL);
   }
#ifdef WIN32
   int i;
   for (i=0;i<sizeof(buf);i++)
   {
      if ('\\'==buf[i])
         buf[i]='_';
      else if (0==buf[i])
         break;
   }
   if (shmsize>0)
   {
      m_shmid = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,shmsize,(LPCSTR)buf);
      if (m_shmid==NULL)
      {
         // 共享内存创建错误:
         sprintf(m_szmsg,"Cannot open share memory for SHM(%s) size=%d -errno:%u!",buf,shmsize,GetLastError());
         Close();
         return(NULL);
      }
   }
   else
   {
      m_shmid = OpenFileMapping(FILE_MAP_WRITE|FILE_MAP_READ,FALSE,(LPCSTR)buf);
      if (m_shmid==NULL)
      {
         sprintf(m_szmsg,"Cannot open share memory for SHM(%s) - errno:%u!",buf,GetLastError());
         Close();
         return(NULL);
      }
   }
   m_address = MapViewOfFile(m_shmid,FILE_MAP_WRITE|FILE_MAP_READ,0,0,0);
   if (m_address==NULL)
   {
      sprintf(m_szmsg,"MapViewOfFile fail [SHM(%s) size=%d] - errno:%u!",buf,shmsize,GetLastError());
      Close();
      return(NULL);
   }
   BY_HANDLE_FILE_INFORMATION finfo;
   GetFileInformationByHandle(m_shmid,&finfo);
   m_size = finfo.nFileSizeLow;
   return(m_address);
#else
   bool bcreated = true;
   key_t shmkey = myftok(buf,BUSHM_KID);
   if (shmsize>0)
   {
      m_shmid = shmget(shmkey,shmsize,SHM_RIGHT|IPC_CREAT);
   }
   else
      m_shmid = -1;
   if (m_shmid==-1)
   {
      m_shmid = shmget(shmkey,0,BUSHM_FLAG);
      bcreated = false;
   }
   if (m_shmid==-1)
   {
      // 共享内存创建错误:
      sprintf(m_szmsg,"Cannot open share memory for key:0x%X-errno:%d!",shmkey,errno);
      Close();
      return(NULL);
   }
   m_address = shmat(m_shmid,0,0);
#ifdef SHM_FAILED //11:50 2006-7-4 for HP_UX64 
   if (m_address==SHM_FAILED)
#else
   if (m_address==(void *)-1L)
#endif
   {
      m_address = NULL;
      // 则表示挂接的地址出现问题,可能属于flag不正确的缘故
      sprintf(m_szmsg,"shmkey=%x:shmat(%d,0,0) 返回错误errno=%d!",shmkey,m_shmid,errno);
      Close();
      if (bcreated)
         Remove(name);
   }
   struct shmid_ds info;
   shmctl(m_shmid,IPC_STAT,&info);
   m_size = info.shm_segsz;
   return(m_address);
#endif
}
开发者ID:nykma,项目名称:ykt4sungard,代码行数:93,代码来源:BUShm.cpp


示例11: dfb_x11_open_window


//.........这里部分代码省略.........
          }

          xw->ximage=XShmCreateImage(xw->display, xw->visual, xw->depth, ZPixmap,
                                     NULL,xw->shmseginfo, xw->width, xw->height * 2);
          XSync( xw->display, False );
          if (!xw->ximage || error_code) {
               D_ERROR("X11: Error creating shared image (XShmCreateImage) \n");
               x11->use_shm = false;
               D_FREE(xw->shmseginfo);
               error_code = 0;
               goto no_shm;
          }

          xw->bpp = (xw->ximage->bits_per_pixel + 7) / 8;

          /* we firstly create our shared memory segment with the size we need, and
          correct permissions for the owner, the group and the world --> 0777 */
          xw->shmseginfo->shmid=shmget(IPC_PRIVATE,
                                       xw->ximage->bytes_per_line * xw->ximage->height * 2,
                                       IPC_CREAT|0777);

          if (xw->shmseginfo->shmid<0) {
               x11->use_shm = false;
               XDestroyImage(xw->ximage);
               D_FREE(xw->shmseginfo);
               goto no_shm;
          }

          /* Then, we have to attach the segment to our process, and we let the
          function search the correct memory place --> NULL. It's safest ! */
          xw->shmseginfo->shmaddr = shmat( xw->shmseginfo->shmid, NULL, 0 );
          if (!xw->shmseginfo->shmaddr) {
               x11->use_shm = false;
               shmctl(xw->shmseginfo->shmid,IPC_RMID,NULL);
               XDestroyImage(xw->ximage);
               D_FREE(xw->shmseginfo);
               goto no_shm;
          }

          /* We set the buffer in Read and Write mode */
          xw->shmseginfo->readOnly=False;

          xw->virtualscreen= xw->ximage->data = xw->shmseginfo->shmaddr;


          XSetErrorHandler( error_handler_shm );

          XShmAttach(x11->display,xw->shmseginfo);

          XShmPutImage(x11->display, xw->window, xw->gc, xw->ximage,
                       0, 0, 0, 0, 1, 1, False);

          XSync(x11->display, False);

          XSetErrorHandler( error_handler );

          if (!x11->use_shm) {
               shmdt(xw->shmseginfo->shmaddr);
               shmctl(xw->shmseginfo->shmid,IPC_RMID,NULL);
               XDestroyImage(xw->ximage);
               D_FREE(xw->shmseginfo);
          }
     }

no_shm:
     if (!x11->use_shm) {
开发者ID:lamproae,项目名称:DirectFB,代码行数:67,代码来源:xwindow.c


示例12: program

/*---------------------------------------------------------------------+
|                               main                                   |
| ==================================================================== |
|                                                                      |
|                                                                      |
| Function:  Main program  (see prolog for more details)               |
|                                                                      |
| Returns:   (0)  Successful completion                                |
|            (-1) Error occurred                                       |
|                                                                      |
+---------------------------------------------------------------------*/
int main(int argc, char **argv)
{
	int i;

	int shmid[MAX_SHMEM_NUMBER];	/* (Unique) Shared memory identifier */

	char *shmptr[MAX_SHMEM_NUMBER];	/* Shared memory segment address */
	char *ptr;		/* Index into shared memory segment */

	int value = 0;		/* Value written into shared memory segment */

	key_t mykey[MAX_SHMEM_NUMBER];
	char *null_file = "/dev/null";
	char proj[MAX_SHMEM_NUMBER] = {
		'3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'E'
	};

	/*
	 * Parse command line arguments and print out program header
	 */
	parse_args(argc, argv);
	printf("%s: IPC Shared Memory TestSuite program\n", *argv);

	for (i = 0; i < offsets_cnt; i++) {
		char tmpstr[256];
		/*
		 * Create a key to uniquely identify the shared segment
		 */

		mykey[i] = ftok(null_file, proj[i]);

		printf
		    ("\n\tmykey to uniquely identify the shared memory segment 0x%x\n",
		     mykey[i]);

		printf("\n\tGet shared memory segment (%d bytes)\n",
		       shmem_size);
		/*
		 * Obtain a unique shared memory identifier with shmget ().
		 */

		if ((long)
		    (shmid[i] =
		     shmget(mykey[i], shmem_size, IPC_CREAT | 0666)) < 0)
			sys_error("shmget failed", __LINE__);

		printf("\n\tAttach shared memory segment to process\n");

		if ((long)(shmptr[i] = shmat(shmid[i], NULL, 0)) == -1) {
			/* If shmat(2) failed, we need the currect process address
			   space layout to debug. The failure can be random. */
			sprintf(tmpstr, "cat /proc/%d/maps >&2", (int)getpid());
			fprintf(stderr, "heap %p\n", sbrk(0));
			system(tmpstr);

			sprintf(tmpstr, "shmat failed - return: %ld",
				(long)shmptr[i]);
			sys_error(tmpstr, __LINE__);
		}

		printf("\n\tShared memory segment address : %p \n", shmptr[i]);

		printf("\n\tIndex through shared memory segment ...\n");

		/*
		 * Index through the shared memory segment
		 */

		for (ptr = shmptr[i]; ptr < (shmptr[i] + shmem_size); ptr++)
			*ptr = value++;

	}			// for 1..11

	printf("\n\tDetach from the segment using the shmdt subroutine\n");

	printf("\n\tRelease shared memory\n");

	for (i = 0; i < offsets_cnt; i++) {
		// Detach from the segment

		shmdt(shmptr[i]);

		// Release shared memory

		if (shmctl(shmid[i], IPC_RMID, 0) < 0)
			sys_error("shmctl failed", __LINE__);

	}			// 2nd for 1..11
	/*
//.........这里部分代码省略.........
开发者ID:AbhiramiP,项目名称:ltp,代码行数:101,代码来源:shmem_test_06.c


示例13: main


//.........这里部分代码省略.........
			
		}
	}

	//the child	 
	if (pid == 0)
	{
		printf("\t\t\tchildpid = %d\n", getpid());
		long long tmp1 = 0;
		long long tmp2 = 0;
		long long tmp3 = 0;
		
		sem_wait(&(aa->childlock));
			aa->childcounter = aa->childcounter + 1;
		sem_post(&(aa->childlock));
		
		int akir = aa->AllKidsReady;
		while(akir == 0){
			//printf("%d: kids not all ready\n", getpid());
			sched_yield();
			akir = aa->AllKidsReady;
		}
		
		//bool bl = true;
		while(1)
		{
			while ( sem_trywait(&(aa->sema)) < 0 ) {
				if( errno == EAGAIN)
					continue;
				else perror("trywait error");
			}
				printf("pid = %d: ENGAGED\n", getpid());
			
				tmp1 = aa->firstnum;
				tmp2 = aa->secondnum;
				if( (tmp1 > MaxVal) || (tmp2 > MaxVal) )
				{
					printf("first if, tmp1 = %lld, tmp2 = %lld\n", tmp1, tmp2);
					sem_post(&(aa->sema));
					exit(0);
				}

				tmp3 = tmp1 + tmp2;

				if(tmp1 <= tmp2)
				{
					printf("second if, tmp1 = %lld, tmp2 = %lld\n", tmp1, tmp2);
					aa->firstnum = tmp3;
				}
				else //tmp1 > tmp2
				{
					printf("third if, tmp1 = %lld, tmp2 = %lld\n", tmp1, tmp2);
					aa->secondnum = tmp3;
				}


				if(tmp3 > MaxVal)
				{
					printf("fourth if, tmp1 = %lld, tmp2 = %lld\n", tmp1, tmp2);
					aa->winnerpid = getpid();
				}
			
			sem_post(&(aa->sema));
			//printf("\n");
			sched_yield();
			
			
			
		}
	}

	//the Parent
	else {
		while(numchildren != aa->childcounter){
			continue;
		}
	
		printf("\n\nPARENT UNLOCKS THE WORLD\n\n");
		aa->AllKidsReady = 1;
		
		for(i=0; i<numchildren; i++){
			wait(NULL);
		}
		
		sem_wait(&(aa->sema));
			printf("\n\nThe winner has pid: %d\n", aa->winnerpid);
		sem_post(&(aa->sema));
		sem_destroy(&(aa->sema));
		
		int d = shmctl(shmid, IPC_RMID, NULL);
		if(d<0){
			perror("shmctl");
			return -1;
		}
		
		return 0;
	}

	return 0;
}
开发者ID:JosephSchechter,项目名称:SchoolStuff,代码行数:101,代码来源:partB1.c


示例14: parse_testcase

static int parse_testcase()
{
    int i, j, mainkey_cnt;
    struct testcase_base_info *info;
    char mainkey_name[32], display_name[64], binary[16];
    int activated, category, run_type;
    int len;

    mainkey_cnt = script_mainkey_cnt();
    info = malloc(sizeof(struct testcase_base_info) * mainkey_cnt);
    if (info == NULL) {
        db_error("core: allocate memory for temporary test case basic "
                "information failed(%s)\n", strerror(errno));
        return -1;
    }
    memset(info, 0, sizeof(struct testcase_base_info) * mainkey_cnt);

    for (i = 0, j = 0; i < mainkey_cnt; i++) {
        memset(mainkey_name, 0, 32);
        script_mainkey_name(i, mainkey_name);

        if (script_fetch(mainkey_name, "display_name", (int *)display_name, 16))
            continue;

        if (script_fetch(mainkey_name, "activated", &activated, 1))
            continue;

        if (display_name[0] && activated == 1) {
            strncpy(info[j].name, mainkey_name, 32);
            strncpy(info[j].display_name, display_name, 64);
            info[j].activated = activated;

            if (script_fetch(mainkey_name, "program", (int *)binary, 4) == 0) {
                strncpy(info[j].binary, binary, 16);
            }

            info[j].id = j;

            if (script_fetch(mainkey_name, "category", &category, 1) == 0) {
                info[j].category = category;
            }

            if (script_fetch(mainkey_name, "run_type", &run_type, 1) == 0) {
                info[j].run_type = run_type;
            }

            j++;
        }
    }
    total_testcases = j;

    db_msg("core: total test cases #%d\n", total_testcases);
    if (total_testcases == 0) {
        return 0;
    }

    len = sizeof(struct testcase_base_info) * total_testcases;
    base_info_shmid = shmget(IPC_PRIVATE, len, IPC_CREAT | 0666);
    if (base_info_shmid == -1) {
        db_error("core: allocate share memory segment for test case basic "
                "information failed(%s)\n", strerror(errno));
        return -1;
    }

    base_info = shmat(base_info_shmid, 0, 0);
    if (base_info == (void *)-1) {
        db_error("core: attach the share memory for test case basic "
                "information failed(%s)\n", strerror(errno));
        shmctl(base_info_shmid, IPC_RMID, 0);
        return -1;
    }
    memcpy(base_info, info, sizeof(struct testcase_base_info) * 
            total_testcases);

    return total_testcases;
}
开发者ID:543872407,项目名称:Linux_SourceCode,代码行数:76,代码来源:core.c


示例15: xf_process_tsmf_video_frame_event

static void xf_process_tsmf_video_frame_event(xfInfo* xfi, RDP_VIDEO_FRAME_EVENT* vevent)
{
	int i;
	BYTE* data1;
	BYTE* data2;
	UINT32 pixfmt;
	UINT32 xvpixfmt;
	BOOL converti420yv12 = FALSE;
	XvImage * image;
	int colorkey = 0;
	XShmSegmentInfo shminfo;
	xfXvContext* xv = (xfXvContext*) xfi->xv_context;

	if (xv->xv_port == 0)
		return;

	/* In case the player is minimized */
	if (vevent->x < -2048 || vevent->y < -2048 || vevent->num_visible_rects <= 0)
		return;

	if (xv->xv_colorkey_atom != None)
	{
		XvGetPortAttribute(xfi->display, xv->xv_port, xv->xv_colorkey_atom, &colorkey);
		XSetFunction(xfi->display, xfi->gc, GXcopy);
		XSetFillStyle(xfi->display, xfi->gc, FillSolid);
		XSetForeground(xfi->display, xfi->gc, colorkey);
		for (i = 0; i < vevent->num_visible_rects; i++)
		{
			XFillRectangle(xfi->display, xfi->window->handle, xfi->gc,
				vevent->x + vevent->visible_rects[i].x,
				vevent->y + vevent->visible_rects[i].y,
				vevent->visible_rects[i].width,
				vevent->visible_rects[i].height);
		}
	}
	else
	{
		XSetClipRectangles(xfi->display, xfi->gc, vevent->x, vevent->y,
			(XRectangle*) vevent->visible_rects, vevent->num_visible_rects, YXBanded);
	}

	pixfmt = vevent->frame_pixfmt;

	if (xf_tsmf_is_format_supported(xv, pixfmt))
	{
		xvpixfmt = pixfmt;
	}
	else if (pixfmt == RDP_PIXFMT_I420 && xf_tsmf_is_format_supported(xv, RDP_PIXFMT_YV12))
	{
		xvpixfmt = RDP_PIXFMT_YV12;
		converti420yv12 = TRUE;
	}
	else if (pixfmt == RDP_PIXFMT_YV12 && xf_tsmf_is_format_supported(xv, RDP_PIXFMT_I420))
	{
		xvpixfmt = RDP_PIXFMT_I420;
		converti420yv12 = TRUE;
	}
	else
	{
		DEBUG_XV("pixel format 0x%X not supported by hardware.", pixfmt);
		return;
	}

	image = XvShmCreateImage(xfi->display, xv->xv_port,
		xvpixfmt, 0, vevent->frame_width, vevent->frame_height, &shminfo);

	if (xv->xv_image_size != image->data_size)
	{
		if (xv->xv_image_size > 0)
		{
			shmdt(xv->xv_shmaddr);
			shmctl(xv->xv_shmid, IPC_RMID, NULL);
		}
		xv->xv_image_size = image->data_size;
		xv->xv_shmid = shmget(IPC_PRIVATE, image->data_size, IPC_CREAT | 0777);
		xv->xv_shmaddr = shmat(xv->xv_shmid, 0, 0);
	}
	shminfo.shmid = xv->xv_shmid;
	shminfo.shmaddr = image->data = xv->xv_shmaddr;
	shminfo.readOnly = FALSE;

	if (!XShmAttach(xfi->display, &shminfo))
	{
		XFree(image);
		DEBUG_XV("XShmAttach failed.");
		return;
	}

	/* The video driver may align each line to a different size
	   and we need to convert our original image data. */
	switch (pixfmt)
	{
		case RDP_PIXFMT_I420:
		case RDP_PIXFMT_YV12:
			/* Y */
			if (image->pitches[0] == vevent->frame_width)
			{
				memcpy(image->data + image->offsets[0],
					vevent->frame_data,
					vevent->frame_width * vevent->frame_height);
//.........这里部分代码省略.........
开发者ID:KimDongChun,项目名称:FreeRDP,代码行数:101,代码来源:xf_tsmf.c


示例16: main

int
main (void)
{
  const int flags = IPC_CREAT | 0666;
  int shmid, semid, msqid;
  FILE *fd;
  pthread_t thread;
  struct sockaddr_in sock_addr;
  int sock;
  unsigned short port;
  socklen_t size;
  int status;

  if ((shmid = shmget (3925, 4096, flags | IPC_EXCL)) < 0)
    {
      /* Attempt to delete the existing shared-memory region, then
	 recreate it.  */
      shmctl (shmget (3925, 4096, flags), IPC_RMID, NULL);
      if ((shmid = shmget (3925, 4096, flags | IPC_EXCL)) < 0)
	{
	  printf ("Cannot create shared-memory region.\n");
	  return 1;
	}	  
    }

  semid = semget (7428, 1, flags);
  msqid = msgget (5294, flags);
  fd = fopen ("/dev/null", "r");

  /* Lock the mutex to prevent the new thread from finishing immediately.  */
  pthread_mutex_lock (&mutex);
  pthread_create (&thread, NULL, thread_proc, 0);
 
  sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sock < 0)
    {
      printf ("Cannot create socket.\n");
      return 1;
    }
 
  sock_addr.sin_family = AF_INET;
  sock_addr.sin_port = 0; /* Bind to a free port.  */
  sock_addr.sin_addr.s_addr = htonl (INADDR_ANY);

  status = bind (sock, (struct sockaddr *) &sock_addr, sizeof (sock_addr));
  if (status < 0)
    {
      printf ("Cannot bind socket.\n");
      return 1;
    }

  /* Find the assigned port number of the socket.  */
  size = sizeof (sock_addr);
  status = getsockname (sock, (struct sockaddr *) &sock_addr, &size);
  if (status < 0)
    {
      printf ("Cannot find name of socket.\n");
      return 1;
    }
  port = ntohs (sock_addr.sin_port);

  status = listen (sock, 1);
  if (status < 0)
    {
      printf ("Cannot listen on socket.\n");
      return 1;
    }

  /* Set breakpoint here.  */

  shmctl (shmid, IPC_RMID, NULL);
  semctl (semid, 0, IPC_RMID, NULL);
  msgctl (msqid, IPC_RMID, NULL);
  fclose (fd);
  close (sock);

  pthread_mutex_unlock (&mutex);
  pthread_join (thread, NULL);

  return 0;
}
开发者ID:kaushiks,项目名称:GDB,代码行数:81,代码来源:info-os.c


示例17: alloc_back_shm_ximage

/**
 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
 * Return:  GL_TRUE if success, GL_FALSE if error
 */
static GLboolean
alloc_back_shm_ximage(XMesaBuffer b, GLuint width, GLuint height)
{
   /*
    * We have to do a _lot_ of error checking here to be sure we can
    * really use the XSHM extension.  It seems different servers trigger
    * errors at different points if the extension won't work.  Therefore
    * we have to be very careful...
    */
   GC gc;
   int (*old_handler)(XMesaDisplay *, XErrorEvent *);

   if (width == 0 || height == 0) {
      /* this will be true the first time we're called on 'b' */
      return GL_FALSE;
   }

   b->backxrb->ximage = XShmCreateImage(b->xm_visual->display,
                                        b->xm_visual->visinfo->visual,
                                        b->xm_visual->visinfo->depth,
                                        ZPixmap, NULL, &b->shminfo,
                                        width, height);
   if (b->backxrb->ximage == NULL) {
      _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (XShmCreateImage), disabling.\n");
      b->shm = 0;
      return GL_FALSE;
   }

   b->shminfo.shmid = shmget(IPC_PRIVATE, b->backxrb->ximage->bytes_per_line
			     * b->backxrb->ximage->height, IPC_CREAT|0777);
   if (b->shminfo.shmid < 0) {
      _mesa_warning(NULL, "shmget failed while allocating back buffer.\n");
      XDestroyImage(b->backxrb->ximage);
      b->backxrb->ximage = NULL;
      _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (shmget), disabling.\n");
      b->shm = 0;
      return GL_FALSE;
   }

   b->shminfo.shmaddr = b->backxrb->ximage->data
                      = (char*)shmat(b->shminfo.shmid, 0, 0);
   if (b->shminfo.shmaddr == (char *) -1) {
      _mesa_warning(NULL, "shmat() failed while allocating back buffer.\n");
      XDestroyImage(b->backxrb->ximage);
      shmctl(b->shminfo.shmid, IPC_RMID, 0);
      b->backxrb->ximage = NULL;
      _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (shmat), disabling.\n");
      b->shm = 0;
      return GL_FALSE;
   }

   b->shminfo.readOnly = False;
   mesaXErrorFlag = 0;
   old_handler = XSetErrorHandler(mesaHandleXError);
   /* This may trigger the X protocol error we're ready to catch: */
   XShmAttach(b->xm_visual->display, &b->shminfo);
   XSync(b->xm_visual->display, False);

   if (mesaXErrorFlag) {
      /* we are on a remote display, this error is normal, don't print it */
      XFlush(b->xm_visual->display);
      mesaXErrorFlag = 0;
      XDestroyImage(b->backxrb->ximage);
      shmdt(b->shminfo.shmaddr);
      shmctl(b->shminfo.shmid, IPC_RMID, 0);
      b->backxrb->ximage = NULL;
      b->shm = 0;
      (void) XSetErrorHandler(old_handler);
      return GL_FALSE;
   }

   shmctl(b->shminfo.shmid, IPC_RMID, 0); /* nobody else needs it */

   /* Finally, try an XShmPutImage to be really sure the extension works */
   gc = XCreateGC(b->xm_visual->display, b->frontxrb->drawable, 0, NULL);
   XShmPutImage(b->xm_visual->display, b->frontxrb->drawable, gc,
		 b->backxrb->ximage, 0, 0, 0, 0, 1, 1 /*one pixel*/, False);
   XSync(b->xm_visual->display, False);
   XFreeGC(b->xm_visual->display, gc);
   (void) XSetErrorHandler(old_handler);
   if (mesaXErrorFlag) {
      XFlush(b->xm_visual->display);
      mesaXErrorFlag = 0;
      XDestroyImage(b->backxrb->ximage);
      shmdt(b->shminfo.shmaddr);
      shmctl(b->shminfo.shmid, IPC_RMID, 0);
      b->backxrb->ximage = NULL;
      b->shm = 0;
      return GL_FALSE;
   }

   return GL_TRUE;
}
开发者ID:ChillyWillyGuru,项目名称:RSXGL,代码行数:97,代码来源:xm_buffer.c


示例18: main


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ shmdt函数代码示例发布时间:2022-05-30
下一篇:
C++ shmat函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap