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

C++ shmdt函数代码示例

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

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



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

示例1: main

int main(int argc, char* argv[])
{
	int shmid ;
	char line[128];
	int fd_s, fd_r ;

	//open fifo  12.fifp 21.fifo
	fd_s = open("./12.fifo", O_WRONLY);

	fd_r = open("./21.fifo", O_RDONLY);

	printf("fd_s: %d, fd_r: %d\n", fd_s, fd_r);

	//shm
	shmid = shmget((key_t)1234, sizeof(mem_t), IPC_CREAT|0666);
	if(shmid == -1)
	{
		perror("shmget");
		exit(1);
	}
	pmem_t pm = (pmem_t)shmat(shmid, NULL, 0);

	if((void*)pm == (void*)-1)
	{
		perror("shmat");
		exit(1);
	}
	pm ->m_lock = 1 ;

	pm ->m_unshow = 0 ;


	//select
	fd_set rds ;
	struct timeval tm ;
	int ret ;
	while(1)
	{
		FD_ZERO(&rds);

		FD_SET(0, &rds);

		FD_SET(fd_r, &rds);

		tm.tv_usec = 0 ;
		tm.tv_sec = 5;

		ret = select(1024, &rds, NULL, NULL, &tm);

		if(ret == 0)
		{
			continue ;
		}else if(ret > 0)
		{
			if(FD_ISSET(0, &rds))
			{
				memset(line, 0, 128);
				read(0, line, 127);
				write(fd_s, line, strlen(line));
			}
			if(FD_ISSET(fd_r, &rds ))
			{
				memset(line, 0, 128);
				read(fd_r, line, 127);

				Lock(pm);
				while( pm -> m_unshow == 1)
				{
					unLock(pm);
					sleep(1);

					Lock(pm);
				}

				strcpy(pm ->m_buf, line);

				pm ->m_unshow = 1 ;

				unLock(pm);
			}
		}





	}


	close(fd_s);
	close(fd_r);
	shmdt(pm);

	shmctl(shmid, IPC_RMID, NULL);


	return 0 ;
}
开发者ID:Fransunshine,项目名称:chapter_linux_git,代码行数:98,代码来源:1_w.c


示例2: shmdt

SDL_Surface *PG_SetVideoMode(_THIS, SDL_Surface *current,
				int width, int height, int bpp, Uint32 flags)
{
	if ( this->hidden->bitmap ) {
	  /* Free old bitmap */
	  if (current->pixels) {
	    shmdt(current->pixels);
	    current->pixels = NULL;
	  }
	  pgDelete(this->hidden->bitmap);
	}

	/* Allocate the new pixel format for the screen */
	if ( ! SDL_ReallocFormat(current, bpp, 0, 0, 0, 0) ) {
		SDL_SetError("Couldn't allocate new pixel format for requested mode");
		return(NULL);
	}

	/* Create a new picogui bitmap */
	this->hidden->bitmap = pgCreateBitmap(width,height);
	this->hidden->shm = *pgMakeSHMBitmap(this->hidden->bitmap);
	current->pixels = shmat(shmget(this->hidden->shm.shm_key,
				       this->hidden->shm.shm_length,0),NULL,0);

	/* Reset the canvas, and draw persistent and incremental grops.
	 * Use mapping and offsets to center it.
	 */

	pgWriteCmd(this->hidden->wCanvas, PGCANVAS_NUKE, 0);

	/* 0. Set the source position during incremental rendering
	 */
	pgWriteCmd(this->hidden->wCanvas, PGCANVAS_GROP, 5, PG_GROP_SETSRC,0,0,0,0);
	pgWriteCmd(this->hidden->wCanvas, PGCANVAS_GROPFLAGS, 1, PG_GROPF_INCREMENTAL);

	/* 1. Incremental bitmap rendering
	 */
	pgWriteCmd(this->hidden->wCanvas, PGCANVAS_GROP, 6, PG_GROP_BITMAP,
		   0,0,0,0,this->hidden->bitmap);
	pgWriteCmd(this->hidden->wCanvas, PGCANVAS_GROPFLAGS, 1, PG_GROPF_INCREMENTAL);

	/* 2. Normal bitmap rendering
	 */
	pgWriteCmd(this->hidden->wCanvas, PGCANVAS_GROP, 6, PG_GROP_BITMAP,
		   0,0,this->hidden->shm.width,this->hidden->shm.height,this->hidden->bitmap);
     
	/* Set up the new mode framebuffer */
	current->flags = 0;
	current->w = this->hidden->shm.width;
	current->h = this->hidden->shm.height;
	current->pitch = this->hidden->shm.pitch;

	/* Set up pixel format */
	current->format->BitsPerPixel = this->hidden->shm.bpp;
	current->format->BytesPerPixel = this->hidden->shm.bpp >> 3;
	if (this->hidden->shm.bpp & 7)
	  current->format->BytesPerPixel++;
	current->format->palette = NULL;
	current->format->Rmask = this->hidden->shm.red_mask;
	current->format->Gmask = this->hidden->shm.green_mask;
	current->format->Bmask = this->hidden->shm.blue_mask;
	current->format->Amask = this->hidden->shm.alpha_mask;
	current->format->Rshift = this->hidden->shm.red_shift;
	current->format->Gshift = this->hidden->shm.green_shift;
	current->format->Bshift = this->hidden->shm.blue_shift;
	current->format->Ashift = this->hidden->shm.alpha_shift;
	current->format->Rloss = 8 - this->hidden->shm.red_length;
	current->format->Gloss = 8 - this->hidden->shm.green_length;
	current->format->Bloss = 8 - this->hidden->shm.blue_length;
	current->format->Aloss = 8 - this->hidden->shm.alpha_length;

	/* Draw the app */
	pgUpdate();

	/* We're done */
	return(current);
}
开发者ID:ahpho,项目名称:wowmapviewer,代码行数:77,代码来源:SDL_pgvideo.c


示例3: segment_create


//.........这里部分代码省略.........
    opal_shmem_seg_hdr_t *seg_hdrp = MAP_FAILED;

    /* init the contents of opal_shmem_ds_t */
    shmem_ds_reset(ds_buf);

    /* for sysv shared memory we don't have to worry about the backing store
     * being located on a network file system... so no check is needed here.
     */

    /* create a new shared memory segment and save the shmid. note the use of
     * real_size here
     */
    if (-1 == (ds_buf->seg_id = shmget(IPC_PRIVATE, real_size,
                                       IPC_CREAT | IPC_EXCL | S_IRWXU))) {
        int err = errno;
        char hn[MAXHOSTNAMELEN];
        gethostname(hn, MAXHOSTNAMELEN - 1);
        hn[MAXHOSTNAMELEN - 1] = '\0';
        opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn,
                       "shmget(2)", "", strerror(err), err);
        rc = OPAL_ERROR;
        goto out;
    }
    /* attach to the sement */
    else if ((void *)-1 == (seg_hdrp = shmat(ds_buf->seg_id, NULL, 0))) {
        int err = errno;
        char hn[MAXHOSTNAMELEN];
        gethostname(hn, MAXHOSTNAMELEN - 1);
        hn[MAXHOSTNAMELEN - 1] = '\0';
        opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn,
                       "shmat(2)", "", strerror(err), err);
        shmctl(ds_buf->seg_id, IPC_RMID, NULL);
        rc = OPAL_ERROR;
        goto out;
    }
    /* mark the segment for destruction - if we are here, then the run-time
     * component selection test detected adequate support for this type of
     * thing.
     */
    else if (0 != shmctl(ds_buf->seg_id, IPC_RMID, NULL)) {
        int err = errno;
        char hn[MAXHOSTNAMELEN];
        gethostname(hn, MAXHOSTNAMELEN - 1);
        hn[MAXHOSTNAMELEN - 1] = '\0';
        opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn,
                       "shmctl(2)", "", strerror(err), err);
        rc = OPAL_ERROR;
        goto out;
    }
    /* all is well */
    else {
        /* -- initialize the shared memory segment -- */
        opal_atomic_rmb();

        /* init segment lock */
        opal_atomic_init(&seg_hdrp->lock, OPAL_ATOMIC_UNLOCKED);
        /* i was the creator of this segment, so note that fact */
        seg_hdrp->cpid = my_pid;

        opal_atomic_wmb();

        /* -- initialize the contents of opal_shmem_ds_t -- */
        ds_buf->seg_cpid = my_pid;
        ds_buf->seg_size = real_size;
        ds_buf->seg_base_addr = (unsigned char *)seg_hdrp;

        /* notice that we are not setting ds_buf->name here. sysv doesn't use
         * it, so don't worry about it - shmem_ds_reset took care of
         * initialization, so we aren't passing garbage around.
         */

        /* set "valid" bit because setment creation was successful */
        OPAL_SHMEM_DS_SET_VALID(ds_buf);

        OPAL_OUTPUT_VERBOSE(
            (70, opal_shmem_base_framework.framework_output,
             "%s: %s: create successful "
             "(id: %d, size: %lu, name: %s)\n",
             mca_shmem_sysv_component.super.base_version.mca_type_name,
             mca_shmem_sysv_component.super.base_version.mca_component_name,
             ds_buf->seg_id, (unsigned long)ds_buf->seg_size, ds_buf->seg_name)
        );
    }

out:
    /* an error occured, so invalidate the shmem object and release any
     * allocated resources.
     */
    if (OPAL_SUCCESS != rc) {
        /* best effort to delete the segment. */
        if ((void *)-1 != seg_hdrp) {
            shmdt((char*)seg_hdrp);
        }
        shmctl(ds_buf->seg_id, IPC_RMID, NULL);

        /* always invalidate in this error path */
        shmem_ds_reset(ds_buf);
    }
    return rc;
}
开发者ID:IanYXXL,项目名称:A1,代码行数:101,代码来源:shmem_sysv_module.c


示例4: main


//.........这里部分代码省略.........
    asserts( semop(_semid, &_buf, 1) );
  }
  
  
  /* lock the semaphore */  
  _buf.sem_op = -1;
  asserts( semop(_semid, &_buf, 1) );
  
  /* Critical section: */ 
  
  /* there is no arguments so print shared memory object content */
  if(o_argc == 1) {
  
  
    /* obtain the descriptor of shared memory object */
    asserts( _shmid = shmget(_key, 0, S_IRUSR | S_IWUSR | SHM_RDONLY) );
    
    /* map shared memory object into virtual address space */
    assertv(_data = shmat(_shmid, NULL, 0), cast(void*)-1);

    /* read from shared memory object */
    _off = 0;
    for(size_t i = 0; i < _data->len; i++) {
      print(_data->vals + _off);
      print(" ");
      _off += strlen(_data->vals + _off) + 1;
    }
    
    println("");
  }
  
  /* write arguments in the reveres order into shared memory object */
  else {
  
    #if (defined ALLOW_CLEANUP || defined DYN_SEG_SIZE)
    struct shmid_ds _shmds;
    #endif /* ALLOW_CLEANUP || DYN_SEG_SIZE */
    
    #ifdef ALLOW_CLEANUP
    union semun _semun;
    
    /* if shared memory object already exist obtain its id and destroy, otherwise do nothing */
    if( o_argc == 2 && !strcmp(o_argv[1], "cleanup") ) {
      _shmid = shmget(_key, 0, S_IRUSR | S_IWUSR);  
      if(_shmid == -1) {
        if(errno != ENOENT) asserts(_shmid);
      }
      else asserts( shmctl(_shmid, IPC_RMID, &_shmds) );
      
      /* destroy the semaphore before exit */
      asserts( semctl(_semid, 0, IPC_RMID, _semun) );
      exit(EXIT_SUCCESS);
    } 
    #endif /* ALLOW_CLEANUP */
    
    /* use existing shared memory object or create the new one */
    #ifdef DYN_SEG_SIZE  
    off_t _segSz = sizeof(size_t);
    for(size_t i = 1; i < o_argc; i++) {
      _segSz += strlen(o_argv[i]) + 1;
    }

    /* Try to create a new shared memory object.
     * If such object already exits the destoy it before.
    **/
    _shmid = shmget(_key, _segSz, S_IRUSR | S_IWUSR | IPC_CREAT | IPC_EXCL);
    if(_shmid == -1) {
      if(errno == EEXIST) {
        asserts( _shmid = shmget(_key, 0, S_IRUSR | S_IWUSR) );
        asserts( shmctl(_shmid, IPC_RMID, &_shmds) );
        asserts( _shmid = shmget(_key, _segSz, S_IRUSR | S_IWUSR | IPC_CREAT) );
      }
    }
    #else
    asserts( _shmid = shmget(_key, SEG_SIZE, S_IRUSR | S_IWUSR | IPC_CREAT) );
    #endif /* DYN_SEG_SIZE */

    /* map shared memory object into virtual address space */
    assertv(_data = shmat(_shmid, NULL, 0), cast(void*)-1);
    
    /* write into the shared memory object */
    _data->len = o_argc - 1;
    _off = 0;
    for(size_t i = o_argc - 1; i > 0; i--) {
      /* it is safe to use strcpy, because we got enought memory */
      strcpy(_data->vals + _off, o_argv[i]);
      _off += strlen(o_argv[i]) + 1;
    }
  }
   
  /* unmap shared memory object from virtual address space */
  assertz( shmdt(_data) );
  _data = NULL;
  
  /* unlock the semaphore */
  _buf.sem_op = 1;
  asserts( semop(_semid, &_buf, 1) );

  exit(EXIT_SUCCESS);
}
开发者ID:chrapkowski,项目名称:so-ipc,代码行数:101,代码来源:sys_v_shm.c


示例5: main


//.........这里部分代码省略.........
                // this code is meant to be a stress test that maximizes the 
                // opportunity for shared memory corruption and politeness is 
                // not helpful in stress tests.
                if (!rc)
                    rc = acquire_semaphore(MY_NAME, sem_id, params.live_dangerously);

                if (rc)
                    done = 1;
                else {
                    // I keep checking the shared memory until something new has 
                    // been written.
                    while ( (!rc) && \
                            (!strcmp((char *)address, last_message_i_wrote)) 
                          ) {
                        // Nothing new; give Mrs. Conclusion another change to respond.
                        sprintf(s, "Read %zu characters '%s'", strlen((char *)address), (char *)address);
                        say(MY_NAME, s);
                        rc = release_semaphore(MY_NAME, sem_id, params.live_dangerously);
                        if (!rc) {
                            rc = acquire_semaphore(MY_NAME, sem_id, params.live_dangerously);
                        }
                    }


                    if (rc) 
                        done = 1;
                    else {
                        sprintf(s, "Read %zu characters '%s'", strlen((char *)address), (char *)address);
                        say(MY_NAME, s);

                        // What I read must be the md5 of what I wrote or something's 
                        // gone wrong.
                        md5ify(last_message_i_wrote, md5ified_message);
                    
                        if (strcmp(md5ified_message, (char *)address) == 0) {
                            // Yes, the message is OK
                            i++;
                            if (i == params.iterations)
                                done = 1;

                            // MD5 the reply and write back to Mrs. Conclusion.
                            md5ify(md5ified_message, md5ified_message);
                            
                            sprintf(s, "Writing %zu characters '%s'", strlen(md5ified_message), md5ified_message);
                            say(MY_NAME, s);

                            strcpy((char *)address, md5ified_message);
                            strcpy((char *)last_message_i_wrote, md5ified_message);
                        }
                        else {
                            sprintf(s, "Shared memory corruption after %d iterations.", i);
                            say(MY_NAME, s);                            
                            sprintf(s, "Mismatch; new message is '%s', expected '%s'.", (char *)address, md5ified_message);
                            say(MY_NAME, s);
                            done = 1;
                        }
                    }
                }
            }

            // Announce for one last time that the semaphore is free again so that 
            // Mrs. Conclusion can exit.
            say(MY_NAME, "Final release of the semaphore followed by a 5 second pause");            
            rc = release_semaphore(MY_NAME, sem_id, params.live_dangerously);
            sleep(5);
            // ...before beginning to wait until it is free again. 
            // Technically, this is bad practice. It's possible that on a 
            // heavily loaded machine, Mrs. Conclusion wouldn't get a chance
            // to acquire the semaphore. There really ought to be a loop here
            // that waits for some sort of goodbye message but for purposes of
            // simplicity I'm skipping that.

            say(MY_NAME, "Final wait to acquire the semaphore");
            rc = acquire_semaphore(MY_NAME, sem_id, params.live_dangerously);
            if (!rc) {
                say(MY_NAME, "Destroying the shared memory.");
                
                if (-1 == shmdt(address)) {
                    sprintf(s, "Detaching the memory failed; errno is %d", errno);
                    say(MY_NAME, s);
                }
                address = NULL;
                
            
                if (-1 == shmctl(shm_id, IPC_RMID, &shm_info)) {
                    sprintf(s, "Removing the memory failed; errno is %d", errno);
                    say(MY_NAME, s);
                }
            }
        }

        say(MY_NAME, "Destroying the semaphore.");
        // Clean up the semaphore
        if (-1 == semctl(sem_id, 0, IPC_RMID)) {
            sprintf(s, "Removing the semaphore failed; errno is %d", errno);
            say(MY_NAME, s);
        }
    }
    return 0; 
}
开发者ID:Chertolom,项目名称:sysv_ipc,代码行数:101,代码来源:premise.c


示例6: PGSharedMemoryCreate

/*
 * PGSharedMemoryCreate
 *
 * Create a shared memory segment of the given size and initialize its
 * standard header.  Also, register an on_shmem_exit callback to release
 * the storage.
 *
 * Dead Postgres segments are recycled if found, but we do not fail upon
 * collision with non-Postgres shmem segments.	The idea here is to detect and
 * re-use keys that may have been assigned by a crashed postmaster or backend.
 *
 * makePrivate means to always create a new segment, rather than attach to
 * or recycle any existing segment.
 *
 * The port number is passed for possible use as a key (for SysV, we use
 * it to generate the starting shmem key).	In a standalone backend,
 * zero will be passed.
 */
PGShmemHeader *
PGSharedMemoryCreate(Size size, bool makePrivate, int port)
{
	IpcMemoryKey NextShmemSegID;
	void	   *memAddress;
	PGShmemHeader *hdr;
	IpcMemoryId shmid;
	struct stat statbuf;

	/* Room for a header? */
	Assert(size > MAXALIGN(sizeof(PGShmemHeader)));

	/* Make sure PGSharedMemoryAttach doesn't fail without need */
	UsedShmemSegAddr = NULL;

	/* Loop till we find a free IPC key */
	NextShmemSegID = port * 1000;

	for (NextShmemSegID++;; NextShmemSegID++)
	{
		/* Try to create new segment */
		memAddress = InternalIpcMemoryCreate(NextShmemSegID, size);
		if (memAddress)
			break;				/* successful create and attach */

		/* Check shared memory and possibly remove and recreate */

		if (makePrivate)		/* a standalone backend shouldn't do this */
			continue;

		if ((memAddress = PGSharedMemoryAttach(NextShmemSegID, &shmid)) == NULL)
			continue;			/* can't attach, not one of mine */

		/*
		 * If I am not the creator and it belongs to an extant process,
		 * continue.
		 */
		hdr = (PGShmemHeader *) memAddress;
		if (hdr->creatorPID != getpid())
		{
			if (kill(hdr->creatorPID, 0) == 0 || errno != ESRCH)
			{
				shmdt(memAddress);
				continue;		/* segment belongs to a live process */
			}
		}

		/*
		 * The segment appears to be from a dead Postgres process, or from a
		 * previous cycle of life in this same process.  Zap it, if possible.
		 * This probably shouldn't fail, but if it does, assume the segment
		 * belongs to someone else after all, and continue quietly.
		 */
		shmdt(memAddress);
		if (shmctl(shmid, IPC_RMID, NULL) < 0)
			continue;

		/*
		 * Now try again to create the segment.
		 */
		memAddress = InternalIpcMemoryCreate(NextShmemSegID, size);
		if (memAddress)
			break;				/* successful create and attach */

		/*
		 * Can only get here if some other process managed to create the same
		 * shmem key before we did.  Let him have that one, loop around to try
		 * next key.
		 */
	}

	/*
	 * OK, we created a new segment.  Mark it as created by this process. The
	 * order of assignments here is critical so that another Postgres process
	 * can't see the header as valid but belonging to an invalid PID!
	 */
	hdr = (PGShmemHeader *) memAddress;
	hdr->creatorPID = getpid();
	hdr->magic = PGShmemMagic;

	/* Fill in the data directory ID info, too */
	if (stat(DataDir, &statbuf) < 0)
//.........这里部分代码省略.........
开发者ID:gurjeet,项目名称:postgres,代码行数:101,代码来源:sysv_shmem.c


示例7: cleanUp

/**
 * Performs the cleanup functions
 *
 * @param sharedMemPtr - the pointer to the shared memory
 */
void cleanUp(void** sharedMemPtr) {
    // this one only needs to detach from the shared mem, because recv will
    // handle all the dealloc procedures
    printf("Detaching from shared memory\n");
    shmdt(sharedMemPtr);
}
开发者ID:ciarand,项目名称:operating-systems-send-and-receive,代码行数:11,代码来源:send.c


示例8: sysv_runtime_query

/**
 * this routine performs a test that indicates whether or not sysv shared
 * memory can safely be used during this run.
 * note: that we want to run this test as few times as possible.
 *
 * @return OPAL_SUCCESS when sysv can safely be used.
 */
static int
sysv_runtime_query(mca_base_module_t **module, int *priority, const char *hint)
{
    char c     = 'j';
    int shmid  = -1;
    char *a    = NULL;
    char *addr = NULL;
    struct shmid_ds tmp_buff;

    *priority = 0;
    *module = NULL;

    /* if hint isn't null, then someone else already figured out who is the
     * best runnable component is AND the caller is relaying that info so we
     * don't have to perform a run-time query.
     */
    if (NULL != hint) {
        OPAL_OUTPUT_VERBOSE(
            (70, opal_shmem_base_framework.framework_output,
             "shmem: sysv: runtime_query: "
             "attempting to use runtime hint (%s)\n", hint)
        );
        /* was i selected? if so, then we are done.
         * otherwise, disqualify myself.
         */
        if (0 == strcasecmp(hint,
                            mca_shmem_sysv_component.super.base_version.mca_component_name)) {
            *priority = mca_shmem_sysv_component.priority;
            *module = (mca_base_module_t *)&opal_shmem_sysv_module.super;
            return OPAL_SUCCESS;
        }
        else {
            *priority = 0;
            *module = NULL;
            return OPAL_SUCCESS;
        }
    }

    /* if we are here, then let the run-time test games begin */
    OPAL_OUTPUT_VERBOSE(
        (70, opal_shmem_base_framework.framework_output,
         "shmem: sysv: runtime_query: NO HINT PROVIDED:"
         "starting run-time test...\n")
    );

    if (-1 == (shmid = shmget(IPC_PRIVATE, (size_t)(opal_getpagesize()),
                              IPC_CREAT | IPC_EXCL | S_IRWXU ))) {
        goto out;
    }
    else if ((void *)-1 == (addr = shmat(shmid, NULL, 0))) {
        goto out;
    }

    /* protect against lazy establishment - may not be needed, but can't hurt */
    a = addr;
    *a = c;

    if (-1 == shmctl(shmid, IPC_RMID, NULL)) {
        goto out;
    }
    else if (-1 == shmctl(shmid, IPC_STAT, &tmp_buff)) {
        goto out;
    }
    /* all is well - rainbows and butterflies */
    else {
        *priority = mca_shmem_sysv_component.priority;
        *module = (mca_base_module_t *)&opal_shmem_sysv_module.super;
    }

out:
    if ((char *)-1 != addr) {
        shmdt(addr);
    }
    return OPAL_SUCCESS;
}
开发者ID:jsquyres,项目名称:ompi-netloc-prototype,代码行数:82,代码来源:shmem_sysv_component.c


示例9: nk_DisconnClientStatuslink

void nk_DisconnClientStatuslink(void)
{
	if(shmdt(shared_memory) == -1){
		warn("remove memory link failed for Client Status Info\n");
	}
}
开发者ID:KrisChaplin,项目名称:LRT2x4_v1.0.2.06_GPL_source,代码行数:6,代码来源:nk_pppoe_handle.c


示例10: main

int main(void)
{
	signed int *X;
	pid_t pid1, pid2;
	int shmid, i, status;

	struct sembuf pop, vop ;

	int semid1, semid2;

	semid1 = semget(IPC_PRIVATE, 1, 0777|IPC_CREAT);
	semid2 = semget(IPC_PRIVATE, 1, 0777|IPC_CREAT);

	semctl(semid1, 0, SETVAL, 0);
	semctl(semid2, 0, SETVAL, 1);

	pop.sem_num = vop.sem_num = 0;
	pop.sem_flg = vop.sem_flg = 0;
	pop.sem_op = -1 ; vop.sem_op = 1 ;

	shmid = shmget(IPC_PRIVATE, 1*sizeof(signed int), 0777|IPC_CREAT);
	X = (signed int *)shmat(shmid, 0, 0);
	*X = 0;
	
	pid1 = fork();
	pid2 = fork();

	if(pid1 == 0 && pid2 != 0)
	{
		P(semid1);
		for( i = 0; i < 10; i++)
		{
			*X += 1;
			if( i == 4 )
				sleep(1);
			printf("\nIncrementing");
		}
		shmdt(X);
		V(semid2);
		return 0;
	}
	if(pid2 == 0 && pid1 != 0)
	{
		P(semid2);
		for( i = 0; i < 10; i++)
		{
			*X -= 1;
			printf("\nDecrementing");
		}
		shmdt(X);
		V(semid1);
		return 0;
	}
	if(pid1 == 0 && pid2 == 0)
		return 0;
	
	for( i = 0; i < 3; i++)
		wait(&status);
	
	printf("\nValue of X = %d\n", *X);

	shmdt(X);
	shmctl(shmid, IPC_RMID, 0);	
	
	semctl(semid1, 0, IPC_RMID, 0);
	semctl(semid2, 0, IPC_RMID, 0);

	return 0;
}
开发者ID:anki08,项目名称:codes,代码行数:69,代码来源:6part2.c


示例11: eliminarMemoria

void eliminarMemoria()
{
	shmdt (memoria);
	shmctl (memory_id , IPC_RMID, (struct shmid_ds *)NULL);
}
开发者ID:oTTa,项目名称:PROY-1-SD,代码行数:5,代码来源:servidor.c


示例12: mexFunction

void
mexFunction (int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
{
  key_t key;
  ACQ_MessagePacketType *packet;
  int msgNumber, msgType, msgId, sampleNumber, numSamples, numChannels, *data;
  int shmid, shmsize;
  int numValues = 28160;

  shmsize = sizeof(ACQ_MessagePacketType) * ACQ_MSGQ_SIZE;

  /* make the key
  if ((key = ftok(ACQ_MSGQ_SHMPATH, ACQ_MSGQ_SHMPROJID)) == -1) {
    perror("ftok");
    exit(1);
    }
   */

  /* use the pre-defined key */
  key = ACQ_MSGQ_SHMKEY;

  /* connect to (and possibly create) the segment */
  if ((shmid = shmget(key, shmsize, 0644 | IPC_CREAT )) == -1)
    mexErrMsgTxt("shmget");

  /* attach to the segment to get a pointer to it */
  packet = shmat(shmid, (void *)0, 0);
  if ((char *)packet == (char *)(-1))
    mexErrMsgTxt("shmat");

  if (nrhs<7)
    mexErrMsgTxt("Not enough input arguments");

  msgNumber    = (int)mxGetScalar(prhs[0])-1; /* one offset in Matlab, zero offset in C */
  /*
  msgType      = (int)mxGetScalar(prhs[1]);
  msgId        = (int)mxGetScalar(prhs[2]);
  sampleNumber = (int)mxGetScalar(prhs[3]);
  numSamples   = (int)mxGetScalar(prhs[4]);
  numChannels  = (int)mxGetScalar(prhs[5]);
  */

  if (mxGetClassID(prhs[6]) != mxINT32_CLASS)
    mexErrMsgTxt("Invalid type of data, should be int32");

  if (msgNumber<0)
    mexErrMsgTxt("Cannot write before the first packet");
  if (msgNumber>=ACQ_MSGQ_SIZE)
    mexErrMsgTxt("Cannot write after the last packet");

  numValues = mxGetNumberOfElements(prhs[6]);
  numValues = ( numValues>28160 ? 28160 : numValues );  /* check boundary */
  numValues = ( numValues<0     ? 0     : numValues );  /* check boundary */

   /* write the meta-information to the packet */
  packet[msgNumber].message_type = (int)mxGetScalar(prhs[1]);
  packet[msgNumber].messageId    = (int)mxGetScalar(prhs[2]);
  packet[msgNumber].sampleNumber = (int)mxGetScalar(prhs[3]);
  packet[msgNumber].numSamples   = (int)mxGetScalar(prhs[4]);
  packet[msgNumber].numChannels  = (int)mxGetScalar(prhs[5]);

   /* write the data to the packet */
  memcpy(packet[msgNumber].data, mxGetData(prhs[6]), numValues*sizeof(int));

  /* detach from the segment */
  if (shmdt(packet) == -1)
    mexErrMsgTxt("shmdt");

} /* end of mexFunction */
开发者ID:Ascronia,项目名称:fieldtrip,代码行数:69,代码来源:write_ctf_shm.c


示例13: testaccess_ipc

void
testaccess_ipc (int ipc_id, char opt, int mode, int expected, char *outbuf)
{
  int actual, semval, rc;
  int myerror = 0;
  char *chPtr;
  struct sembuf sop;
  uid_t tmpuid;
  gid_t tmpgid;
  struct msqbuf
  {
    long mtype;
    char mtext[80];
  } s_message, r_message;

  /* If we are root, we expect to succeed event
   * without explicit permission.
   */
  strcat (outbuf, (expected == -1) ? "expected: fail  " : "expected: pass  ");

  switch (opt)
    {
      /* Shared Memory */
    case 'm':
      /* Try to get (mode) access
       * There is no notion of a write-only shared memory
       * segment. We are testing READ ONLY and READWRITE access.
       */
      chPtr = shmat (ipc_id, NULL, (mode == O_RDONLY) ? SHM_RDONLY : 0);
      if (chPtr != (void *) -1)
{
  strcat (outbuf, "actual: pass ");
  actual = 0;
  if (shmdt (chPtr) == -1)
    {
      perror ("Warning: Could not dettach memory segment");
    }
}
      else
{
  myerror = errno;
  strcat (outbuf, "actual: fail ");
  actual = -1;
}
      break;
      /* Semaphores */
    case 's':
      tmpuid = geteuid ();
      tmpgid = getegid ();
      semval = semctl (ipc_id, 0, GETVAL);
      /* Need semaphore value == 0 to execute read permission test */
      if ((mode == O_RDONLY) && (semval > 0))
{
  setids (0, 0);
  if ((semctl (ipc_id, 0, SETVAL, 0)) == -1)
    {
      printf ("Unable to set semaphore value: %d\n", errno);
    }
  setids (tmpuid, tmpgid);
}
      /* Try to get mode access */
      sop.sem_num = 0;
      sop.sem_op = mode;
      sop.sem_flg = SEM_UNDO;
      actual = semop (ipc_id, &sop, 1);
      myerror = errno;
      if (actual != -1)
{
  strcat (outbuf, "actual: pass ");
  /* back to semaphore original value */
  if (mode != O_RDONLY)
    {
      sop.sem_op = -1;/* decrement semaphore */
      rc = semop (ipc_id, &sop, 1);
    }
}
      else
{
  /* Back to semaphore original value */
  if ((mode == O_RDONLY) && (semval > 0))
    {
      setids (0, 0);
      if ((semctl (ipc_id, 0, SETVAL, semval)) == -1)
{
  printf ("Unable to set semaphore " "value: %d\n", errno);
}
      setids (tmpuid, tmpgid);
    }
  strcat (outbuf, "actual: fail ");
}
      break;
      /* Message Queues */
    case 'q':
      tmpuid = geteuid ();
      tmpgid = getegid ();
      if (mode == O_RDONLY)
{
  setids (0, 0);
  /* Send a message to test msgrcv function */
  s_message.mtype = 1;
//.........这里部分代码省略.........
开发者ID:8l,项目名称:rose,代码行数:101,代码来源:syscall_rt.18.c


示例14: Demux


//.........这里部分代码省略.........
    {
        if (sys->es != NULL)
            es_out_Del (demux->out, sys->es);

        /* Update composite pixmap */
        if (sys->window != geo->root)
        {
            xcb_free_pixmap (conn, sys->pixmap); /* no-op first time */
            xcb_composite_name_window_pixmap (conn, sys->window, sys->pixmap);
            xcb_create_pixmap (conn, geo->depth, sys->pixmap,
                               geo->root, geo->width, geo->height);
        }

        sys->es = InitES (demux, w, h, geo->depth, &sys->bpp);
        if (sys->es != NULL)
        {
            sys->cur_w = w;
            sys->cur_h = h;
            sys->bpp /= 8; /* bits -> bytes */
        }
    }

    /* Capture screen */
    xcb_drawable_t drawable =
        (sys->window != geo->root) ? sys->pixmap : sys->window;
    free (geo);

    block_t *block = NULL;
#if HAVE_SYS_SHM_H
    if (sys->shm)
    {   /* Capture screen through shared memory */
        size_t size = w * h * sys->bpp;
        int id = shmget (IPC_PRIVATE, size, IPC_CREAT | 0777);
        if (id == -1) /* XXX: fallback */
        {
            msg_Err (demux, "shared memory allocation error: %m");
            goto noshm;
        }

        /* Attach the segment to X and capture */
        xcb_shm_get_image_reply_t *img;
        xcb_shm_get_image_cookie_t ck;

        xcb_shm_attach (conn, sys->segment, id, 0 /* read/write */);
        ck = xcb_shm_get_image (conn, drawable, x, y, w, h, ~0,
                                XCB_IMAGE_FORMAT_Z_PIXMAP, sys->segment, 0);
        xcb_shm_detach (conn, sys->segment);
        img = xcb_shm_get_image_reply (conn, ck, NULL);
        xcb_flush (conn); /* ensure eventual detach */

        if (img == NULL)
        {
            shmctl (id, IPC_RMID, 0);
            goto noshm;
        }
        free (img);

        /* Attach the segment to VLC */
        void *shm = shmat (id, NULL, 0 /* read/write */);
        shmctl (id, IPC_RMID, 0);
        if (-1 == (intptr_t)shm)
        {
            msg_Err (demux, "shared memory attachment error: %m");
            return;
        }

        block = block_shm_Alloc (shm, size);
        if (unlikely(block == NULL))
            shmdt (shm);
    }
noshm:
#endif
    if (block == NULL)
    {   /* Capture screen through socket (fallback) */
        xcb_get_image_reply_t *img;

        img = xcb_get_image_reply (conn,
            xcb_get_image (conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
                           x, y, w, h, ~0), NULL);
        if (img == NULL)
            return;

        uint8_t *data = xcb_get_image_data (img);
        size_t datalen = xcb_get_image_data_length (img);
        block = block_heap_Alloc (img, data + datalen - (uint8_t *)img);
        if (block == NULL)
            return;
        block->p_buffer = data;
        block->i_buffer = datalen;
    }

    /* Send block - zero copy */
    if (sys->es != NULL)
    {
        block->i_pts = block->i_dts = mdate ();

        es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
        es_out_Send (demux->out, sys->es, block);
    }
}
开发者ID:Aki-Liang,项目名称:vlc-2.1.0.oldlib-2010,代码行数:101,代码来源:xcb.c


示例15: IpcMemoryDetach

static void
IpcMemoryDetach(int status, Datum shmaddr)
{
	if (shmdt(DatumGetPointer(shmaddr)) < 0)
		elog(LOG, "shmdt(%p) failed: %m", DatumGetPointer(shmaddr));
}
开发者ID:gurjeet,项目名称:postgres,代码行数:6,代码来源:sysv_shmem.c


示例16: nk_DisconnIPInfolink

void nk_DisconnIPInfolink(void)
{
    if(shmdt(shared_memory3) == -1){
		warn("remove memory link failed for IP Info\n");
	}
}
开发者ID:KrisChaplin,项目名称:LRT2x4_v1.0.2.06_GPL_source,代码行数:6,代码来源:nk_pppoe_handle.c


示例17: PGSharedMemoryIsInUse

/*
 * PGSharedMemoryIsInUse
 *
 * Is a previously-existing shmem segment still existing and in use?
 *
 * The point of this exercise is to detect the case where a prior postmaster
 * crashed, but it left child backends that are still running.	Therefore
 * we only care about shmem segments that are associated with the intended
 * DataDir.  This is an important consideration since accidental matches of
 * shmem segment IDs are reasonably common.
 */
bool
PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2)
{
	IpcMemoryId shmId = (IpcMemoryId) id2;
	struct shmid_ds shmStat;
	struct stat statbuf;
	PGShmemHeader *hdr;

	/*
	 * We detect whether a shared memory segment is in use by seeing whether
	 * it (a) exists and (b) has any processes attached to it.
	 */
	if (shmctl(shmId, IPC_STAT, &shmStat) < 0)
	{
		/*
		 * EINVAL actually has multiple possible causes documented in the
		 * shmctl man page, but we assume it must mean the segment no longer
		 * exists.
		 */
		if (errno == EINVAL)
			return false;

		/*
		 * EACCES implies that the segment belongs to some other userid, which
		 * means it is not a Postgres shmem segment (or at least, not one that
		 * is relevant to our data directory).
		 */
		if (errno == EACCES)
			return false;

		/*
		 * Some Linux kernel versions (in fact, all of them as of July 2007)
		 * sometimes return EIDRM when EINVAL is correct.  The Linux kernel
		 * actually does not have any internal state that would justify
		 * returning EIDRM, so we can get away with assuming that EIDRM is
		 * equivalent to EINVAL on that platform.
		 */
#ifdef HAVE_LINUX_EIDRM_BUG
		if (errno == EIDRM)
			return false;
#endif

		/*
		 * Otherwise, we had better assume that the segment is in use. The
		 * only likely case is EIDRM, which implies that the segment has been
		 * IPC_RMID'd but there are still processes attached to it.
		 */
		return true;
	}

	/* If it has no attached processes, it's not in use */
	if (shmStat.shm_nattch == 0)
		return false;

	/*
	 * Try to attach to the segment and see if it matches our data directory.
	 * This avoids shmid-conflict problems on machines that are running
	 * several postmasters under the same userid.
	 */
	if (stat(DataDir, &statbuf) < 0)
		return true;			/* if can't stat, be conservative */

	hdr = (PGShmemHeader *) shmat(shmId, NULL, PG_SHMAT_FLAGS);

	if (hdr == (PGShmemHeader *) -1)
		return true;			/* if can't attach, be conservative */

	if (hdr->magic != PGShmemMagic ||
		hdr->device != statbuf.st_dev ||
		hdr->inode != statbuf.st_ino)
	{
		/*
		 * It's either not a Postgres segment, or not one for my data
		 * directory.  In either case it poses no threat.
		 */
		shmdt((void *) hdr);
		return false;
	}

	/* Trouble --- looks a lot like there's still live backends */
	shmdt((void *) hdr);

	return true;
}
开发者ID:gurjeet,项目名称:postgres,代码行数:95,代码来源:sysv_shmem.c


示例18: nk_DisconnServerInfolink

void nk_DisconnServerInfolink(void)
{
    if(shmdt(shared_memory4) == -1){
		warn("remove memory link failed for Server Info Info\n");
	}
}
开发者ID:KrisChaplin,项目名称:LRT2x4_v1.0.2.06_GPL_source,代码行数:6,代码来源:nk_pppoe_handle.c


示例19: sysv_runtime_query

/* ////////////////////////////////////////////////////////////////////////// */
static int
sysv_runtime_query(mca_base_module_t **module,
                   int *priority,
                   const char *hint)
{
    char c     = 'j';
    int shmid  = -1;
    char *a    = NULL;
    char *addr = NULL;
    struct shmid_ds tmp_buff;
    int flags;
    int ret;

    ret = OSHMEM_SUCCESS;

    *priority = 0;
    *module = NULL;

    /* if we are here, then let the run-time test games begin */

#if defined (SHM_HUGETLB)
    if (mca_sshmem_sysv_component.use_hp != 0) {
         flags = IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | SHM_HUGETLB;
        if (-1 == (shmid = shmget(IPC_PRIVATE, sshmem_sysv_gethugepagesize(), flags))) {
            if (mca_sshmem_sysv_component.use_hp == 1) {
                mca_sshmem_sysv_component.use_hp = 0;
                ret = OSHMEM_ERR_NOT_AVAILABLE;
                goto out;
            }
            mca_sshmem_sysv_component.use_hp = 0;
        }
        else if ((void *)-1 == (addr = shmat(shmid, NULL, 0))) {
            shmctl(shmid, IPC_RMID, NULL);
            if (mca_sshmem_sysv_component.use_hp == 1) {
                mca_sshmem_sysv_component.use_hp = 0;
                ret = OSHMEM_ERR_NOT_AVAILABLE;
                goto out;
            }
            mca_sshmem_sysv_component.use_hp = 0;
        }
    }
#else
    if (mca_sshmem_sysv_component.use_hp == 1) {
        mca_sshmem_sysv_component.use_hp = 0;
        ret = OSHMEM_ERR_NOT_AVAILABLE;
        goto out;
    }
    mca_sshmem_sysv_component.use_hp = 0;
#endif

    if (0 == mca_sshmem_sysv_component.use_hp) {
        flags = IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR;
        if (-1 == (shmid = shmget(IPC_PRIVATE, (size_t)(opal_getpagesize()), flags))) {
            ret = OSHMEM_ERR_NOT_AVAILABLE;
            goto out;
        }
        else if ((void *)-1 == (addr = shmat(shmid, NULL, 0))) {
            shmctl(shmid, IPC_RMID, NULL);
            ret = OSHMEM_ERR_NOT_AVAILABLE;
            goto out;
        }
    }

    /* protect against lazy establishment - may not be needed, but can't hurt */
    a = addr;
    *a = c;

    if (-1 == shmctl(shmid, IPC_RMID, NULL)) {
        goto out;
    }
    else if (-1 == shmctl(shmid, IPC_STAT, &tmp_buff)) {
        goto out;
    }
    /* all is well - rainbows and butterflies */
    else {
        *priority = mca_sshmem_sysv_component.priority;
        *module = (mca_base_module_t *)&mca_sshmem_sysv_module.super;
    }

  out:
    if ((char *)-1 != addr) {
        shmdt(addr);
    }
    return ret;
}
开发者ID:Cai900205,项目名称:test,代码行数:86,代码来源:sshmem_sysv_component.c


示例20: main


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
C++ shmem_barrier_all函数代码示例发布时间:2022-05-30
下一篇:
C++ shmctl函数代码示例发布时间: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