本文整理汇总了C++中sem_v函数的典型用法代码示例。如果您正苦于以下问题:C++ sem_v函数的具体用法?C++ sem_v怎么用?C++ sem_v使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sem_v函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: doTerminateInstance
static int
doTerminateInstance( struct nc_state_t *nc,
ncMetadata *meta,
char *instanceId,
int *shutdownState,
int *previousState)
{
ncInstance *instance, *vninstance;
virConnectPtr *conn;
int err;
sem_p (inst_sem);
instance = find_instance(&global_instances, instanceId);
sem_v (inst_sem);
if (instance == NULL)
return NOT_FOUND;
/* try stopping the KVM domain */
conn = check_hypervisor_conn();
if (conn) {
sem_p(hyp_sem);
virDomainPtr dom = virDomainLookupByName(*conn, instanceId);
sem_v(hyp_sem);
if (dom) {
/* also protect 'destroy' commands, just in case */
sem_p (hyp_sem);
err = virDomainDestroy (dom);
sem_v (hyp_sem);
if (err==0) {
logprintfl (EUCAINFO, "destroyed domain for instance %s\n", instanceId);
}
sem_p(hyp_sem);
virDomainFree(dom); /* necessary? */
sem_v(hyp_sem);
} else {
if (instance->state != BOOTING)
logprintfl (EUCAWARN, "warning: domain %s to be terminated not running on hypervisor\n", instanceId);
}
}
/* change the state and let the monitoring_thread clean up state */
sem_p (inst_sem);
if (instance->state==BOOTING) {
change_state (instance, CANCELED);
} else {
change_state (instance, SHUTOFF);
}
sem_v (inst_sem);
*previousState = instance->stateCode;
*shutdownState = instance->stateCode;
return OK;
}
示例2: main
int main(int argc, char *argv[])
{
//前面一大块4个人物其实都是一样的,就是看看semget是不是分配成功,共享区是不是分享成功
int i,item,shmid;
semaphore mutex,pan,orange;
union semun sem_union;
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
if ( (mutex=semget((key_t)KEY_MUTEX,1,0666|IPC_CREAT)) == -1 ) {
fprintf(stderr,"Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if ( (pan = semget((key_t)KEY_PAN,1,0666|IPC_CREAT)) == -1 ) {
fprintf(stderr,"Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if ( (orange = semget((key_t)KEY_ORANGE,1,0666|IPC_CREAT)) == -1 ) {
fprintf(stderr,"Failed to create semaphore!");
exit(EXIT_FAILURE);
}
if ( (shmid = shmget((key_t)KEY_SHM,sizeof(struct shared_use_st),0666|IPC_CREAT)) == -1 ) {
fprintf(stderr,"Failed to create shared memory!");
exit(EXIT_FAILURE);
}
if ( (shared_memory = shmat(shmid,(void *)0,0) ) == (void *)-1) {
fprintf(stderr,"shmat failed\n");
exit(EXIT_FAILURE);
}
shared_stuff = (struct shared_use_st *)shared_memory;
//这个是模仿老师的30个,其实通过sleep的时间调整,能让dad和mother经常拿到不同盘子,比较接近的话,会一直拿到一样的盘子的
for(i=0;i<30;i++)
{
sleep(0.9);
sem_p(pan);
sem_p(mutex);
(shared_stuff->hi) = ((shared_stuff->hi)+1) % BUFFER_SIZE;
item = ((int)shared_stuff->hi)+1;
printf("Mother use pan %d put the orange\n",item);
sem_v(mutex);
sem_v(orange);
}
if (shmdt(shared_memory) == -1) {
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
printf("Finish!\n");
getchar();
exit(EXIT_SUCCESS);
}
示例3: diskutil_loop
int diskutil_loop (const char * path, const long long offset, char * lodev, int lodev_size)
{
int found = 0;
int done = 0;
int ret = OK;
char * output;
// we retry because we cannot atomically obtain a free loopback
// device on all distros (some versions of 'losetup' allow a file
// argument with '-f' options, but some do not)
for (int i=0; i<LOOP_RETRIES; i++) {
sem_p (loop_sem);
output = pruntf (TRUE, "%s %s -f", helpers_path[ROOTWRAP], helpers_path[LOSETUP]);
sem_v (loop_sem);
if (output==NULL) // there was a problem
break;
if (strstr (output, "/dev/loop")) {
strncpy (lodev, output, lodev_size);
char * ptr = strrchr (lodev, '\n');
if (ptr) {
*ptr = '\0';
found = 1;
}
}
free (output);
if (found) {
boolean do_log = ((i+1)==LOOP_RETRIES); // log error on last try only
logprintfl (EUCADEBUG, "{%u} attaching file %s\n", (unsigned int)pthread_self(), path);
logprintfl (EUCADEBUG, "{%u} to %s at offset %lld\n", (unsigned int)pthread_self(), lodev, offset);
sem_p (loop_sem);
output = pruntf (do_log, "%s %s -o %lld %s %s", helpers_path[ROOTWRAP], helpers_path[LOSETUP], offset, lodev, path);
sem_v (loop_sem);
if (output==NULL) {
logprintfl (EUCADEBUG, "{%u} cannot attach to loop device %s (will retry)\n", (unsigned int)pthread_self(), lodev);
} else {
free (output);
done = 1;
break;
}
}
sleep (1);
found = 0;
}
if (!done) {
logprintfl (EUCAINFO, "{%u} error: cannot find free loop device or attach to one\n", (unsigned int)pthread_self());
ret = ERROR;
}
return ret;
}
示例4: doDescribeInstances
static int
doDescribeInstances( struct nc_state_t *nc,
ncMetadata *meta,
char **instIds,
int instIdsLen,
ncInstance ***outInsts,
int *outInstsLen)
{
ncInstance *instance;
int total, i, j, k;
*outInstsLen = 0;
*outInsts = NULL;
sem_p (inst_sem);
if (instIdsLen == 0) /* describe all instances */
total = total_instances (&global_instances);
else
total = instIdsLen;
*outInsts = malloc(sizeof(ncInstance *)*total);
if ((*outInsts) == NULL) {
sem_v (inst_sem);
return OUT_OF_MEMORY;
}
k = 0;
for (i=0; (instance = get_instance(&global_instances)) != NULL; i++) {
/* only pick ones the user (or admin) is allowed to see */
if (strcmp(meta->userId, nc->admin_user_id)
&& strcmp(meta->userId, instance->userId))
continue;
if (instIdsLen > 0) {
for (j=0; j < instIdsLen; j++)
if (!strcmp(instance->instanceId, instIds[j]))
break;
if (j >= instIdsLen)
/* instance of not relavance right now */
continue;
}
(* outInsts)[k++] = instance;
}
*outInstsLen = k;
sem_v (inst_sem);
return OK;
}
示例5: doAttachSystemDisk
static int
doAttachSystemDisk ( struct nc_state_t *nc,
ncMetadata *meta,
char *instanceId,
char *isoPath)
{
ncInstance *instance;
virConnectPtr *conn;
virDomainPtr dom = NULL;
char xml[MAX_PATH]={'\0'};
int err;
snprintf (xml, sizeof(xml), "<disk type='block' device='cdrom'><source dev='%s'/><target dev='hdc' bus='ide'/><readonly/></disk>", isoPath);
//logprintfl(EUCAINFO, "doAttachSystemDisk(): founded %s [%s---%s---%d]\n", instanceId,__FILE__, __FUNCTION__, __LINE__);
sem_p(inst_sem);
instance = find_instance(&global_instances, instanceId);
sem_v(inst_sem);
if (instance == NULL || (instance->state != RUNNING&& instance->state != BLOCKED&& instance->state !=PAUSED)) {
logprintfl(EUCAINFO, "doAttachSystemDisk(): found %s failed or instance->state!=RUNNING||BLOCKED||PAUSED [file:%s---line:%d]\n", instanceId, __FILE__, __LINE__);
return NOT_FOUND;
}
conn = check_hypervisor_conn();
if (conn) {
/*
snprintf(cmd, sizeof(cmd), "virsh attach-disk %s %s hdc --type cdrom --mode readonly",instanceId, isoPath);
logprintfl(EUCAINFO, "xiongm: cmd=%s [%s---%s---%d]\n",cmd, __FILE__, __FUNCTION__, __LINE__);
system(cmd);
*/
dom = NULL;
sem_p(hyp_sem);
dom = virDomainLookupByName(*conn, instanceId);
sem_v(hyp_sem);
if (dom) {
sem_p (hyp_sem);
err = virDomainAttachDevice (dom, xml);
sem_v (hyp_sem);
if(err != 0) {
logprintfl(EUCAINFO,"virDomainAttachDevice failed. err=%d\n",err);
return 1;
}
}
logprintfl(EUCAINFO,"doAttachSystemDisk success.\n");
return 0;
}
else {
logprintfl(EUCAINFO, "doAttachSystemDisk(): no connect hypervisior [file:%s---line:%d]\n",__FILE__, __LINE__);
return 1;
}
}
示例6: disconnect_iscsi_target
int disconnect_iscsi_target (const char *dev_string)
{
int pid, retval, status;
assert (strlen (home));
logprintfl (EUCAINFO, "disconnect_iscsi_target invoked (dev_string=%s)\n", dev_string);
sem_p (iscsi_sem);
pid = fork();
if (!pid) {
if ( dev_string && strlen(dev_string) ) logprintfl(EUCADEBUG, "disconnect_iscsi_target(): running command: %s %s,%s\n", disconnect_storage_cmd_path, home, dev_string);
if (vrun("%s %s,%s", disconnect_storage_cmd_path, home, dev_string) != 0) {
logprintfl (EUCAERROR, "ERROR: disconnect_iscsi_target failed\n");
exit(1);
}
exit(0);
} else {
retval = timewait(pid, &status, 90);
if (retval) {
retval = WEXITSTATUS(status);
} else {
kill(pid, SIGKILL);
retval = -1;
}
}
sem_v (iscsi_sem);
return retval;
}
示例7: disconnect_ebs_volume
//!
//! Detach a local device that is iSCSI and disconnect the session.
//!
//! @param[in] sc_url - The URL to reach the cluster's SC at.
//! @param[in] use_ws_sec - boolean to determine use of WS-SEC.
//! @param[in] ws_sec_policy_file - Policy file path for WS-SEC
//! @param[in] attachment_token - The volume/token string received in the request that will be used
//! @param[in] connect_string - The connect string used for attachment, to be re-used on disconnect
//! @param[in] local_ip - The local host's external IP
//! @param[in] local_iqn - The local host's IQN
//!
//! @return
//!
//! @pre
//!
//! @post
//!
//! @note should only be invoked after detachment from the guest
//!
int disconnect_ebs_volume(char *sc_url, int use_ws_sec, char *ws_sec_policy_file, char *attachment_token, char *connect_string, char *local_ip, char *local_iqn)
{
int ret = EUCA_ERROR;
int norescan = 0; //send a 0 to indicate no rescan requested
ebs_volume_data *vol_data = NULL;
if (attachment_token == NULL || connect_string == NULL || local_ip == NULL || local_iqn == NULL) {
LOGERROR("Cannont disconnect ebs volume. Got NULL input parameters.\n");
return EUCA_ERROR;
}
LOGTRACE("Disconnecting an EBS volume\n");
if (deserialize_volume(attachment_token, &vol_data) != EUCA_OK) {
LOGERROR("Could not deserialize attachment token string %s\n", attachment_token);
return EUCA_ERROR;
}
LOGTRACE("Requesting volume lock\n");
sem_p(vol_sem);
{
LOGTRACE("Got volume lock\n");
ret = cleanup_volume_attachment(sc_url, use_ws_sec, ws_sec_policy_file, vol_data, connect_string, local_ip, local_iqn, norescan);
LOGTRACE("cleanup_volume_attachment returned: %d\n", ret);
LOGTRACE("Releasing volume lock\n");
}
sem_v(vol_sem);
LOGTRACE("Released volume lock\n");
EUCA_FREE(vol_data);
return ret;
}
示例8: diskutil_unloop
int diskutil_unloop (const char * lodev)
{
int ret = OK;
char * output;
int retried = 0;
logprintfl (EUCADEBUG, "{%u} detaching from loop device %s\n", (unsigned int)pthread_self(), lodev);
// we retry because we have seen spurious errors from 'losetup -d' on Xen:
// ioctl: LOOP_CLR_FD: Device or resource bus
for (int i=0; i<LOOP_RETRIES; i++) {
boolean do_log = ((i+1)==LOOP_RETRIES); // log error on last try only
sem_p (loop_sem);
output = pruntf (do_log, "%s %s -d %s", helpers_path[ROOTWRAP], helpers_path[LOSETUP], lodev);
sem_v (loop_sem);
if (!output) {
ret = ERROR;
} else {
ret = OK;
free (output);
break;
}
logprintfl (EUCADEBUG, "{%u} cannot detach loop device %s (will retry)\n", (unsigned int)pthread_self(), lodev);
retried++;
sleep (1);
}
if (ret == ERROR) {
logprintfl (EUCAWARN, "{%u} error: cannot detach loop device\n", (unsigned int)pthread_self());
} else if (retried) {
logprintfl (EUCAINFO, "{%u} succeeded to detach %s after %d retries\n", (unsigned int)pthread_self(), lodev, retried);
}
return ret;
}
示例9: doRebootInstance
static int
doRebootInstance( struct nc_state_t *nc,
ncMetadata *meta,
char *instanceId)
{
sem_p (inst_sem);
ncInstance *instance = find_instance (&global_instances, instanceId);
sem_v (inst_sem);
if ( instance == NULL ) {
logprintfl (EUCAERROR, "cannot find instance %s\n", instanceId);
return ERROR;
}
pthread_t tcb;
// since shutdown/restart may take a while, we do them in a thread
if ( pthread_create (&tcb, NULL, rebooting_thread, (void *)instance) ) {
logprintfl (EUCAERROR, "failed to spawn a reboot thread\n");
return ERROR_FATAL;
}
if (pthread_detach(tcb)) {
logprintfl (EUCAERROR, "failed to detach the rebooting thread\n");
return ERROR_FATAL;
}
return OK;
}
示例10: main
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操作 */
}
}
示例11: wait_for_file
/* wait for file 'appear' to appear or for file 'disappear' to disappear */
static int wait_for_file (const char * appear, const char * disappear, const int iterations, const char * name)
{
int done, i;
if (!appear && !disappear) return 1;
for ( i=0, done=0; i<iterations && !done; i++ ) {
struct stat mystat;
sem_p (sc_sem);
int check = ( (appear && (stat (appear, &mystat)==0)) ||
(disappear && (stat (disappear, &mystat)!=0)) );
sem_v (sc_sem);
if (check) {
done++;
} else {
if (i==0) {
logprintfl (EUCAINFO, "waiting for %s to become ready...\n", name);
}
sleep (10);
}
}
if (!done) {
logprintfl (EUCAERROR, "ERROR: timed out waiting for %s to become ready\n", name);
return 1;
}
return 0;
}
示例12: doRebootInstance
//!
//! Handles the reboot request of an instance.
//!
//! @param[in] nc a pointer to the NC state structure to initialize
//! @param[in] pMeta a pointer to the node controller (NC) metadata structure
//! @param[in] instanceId the instance identifier string (i-XXXXXXXX)
//!
//! @return EUCA_OK on success or proper error code. Known error code returned include:
//! EUCA_ERROR, EUCA_NOT_FOUND_ERROR, and EUCA_FATAL_ERROR.
//!
static int doRebootInstance(struct nc_state_t *nc, ncMetadata * pMeta, char *instanceId)
{
pthread_t tcb = { 0 };
ncInstance *instance = NULL;
rebooting_thread_params *params = NULL;
sem_p(inst_sem);
{
instance = find_instance(&global_instances, instanceId);
}
sem_v(inst_sem);
if (instance == NULL) {
LOGERROR("[%s] cannot find instance\n", instanceId);
return (EUCA_NOT_FOUND_ERROR);
}
params = EUCA_ZALLOC(1, sizeof(rebooting_thread_params));
memcpy(&(params->instance), instance, sizeof(ncInstance));
memcpy(&(params->nc), nc, sizeof(struct nc_state_t));
// since shutdown/restart may take a while, we do them in a thread
if (pthread_create(&tcb, NULL, rebooting_thread, params)) {
LOGERROR("[%s] failed to spawn a reboot thread\n", instanceId);
return (EUCA_FATAL_ERROR);
}
set_corrid_pthread(get_corrid() != NULL ? get_corrid()->correlation_id : NULL, tcb);
if (pthread_detach(tcb)) {
LOGERROR("[%s] failed to detach the rebooting thread\n", instanceId);
return (EUCA_FATAL_ERROR);
}
return (EUCA_OK);
}
示例13: main
int main()
{
void *shared_memory = NULL;
struct shm_buff *shm_buff_inst;
char buffer[BUFSIZ];
int shmid,semid;
ignore_signal();
semid = semget(ftok(".",'a'),1,0666|IPC_CREAT);
init_sem(semid,1);
shmid = shmget(ftok(".",'b'),sizeof(struct shm_buff),0666|IPC_CREAT);
if(shmid == -1)
{
perror("shmget failed");
del_sem(semid);
exit(1);
}
shared_memory=shmat(shmid,(void*)0,0);
if(shared_memory==(void*)-1)
{
perror("shmat");
del_sem(semid);
exit(1);
}
printf("memory attached at %X\n",(int)shared_memory);
shm_buff_inst = (struct shm_buff*)shared_memory;
do
{
sem_p(semid);
printf("enter some text to the shared memory(enter'quit'to exit):");
if(fgets(shm_buff_inst->buffer,SHM_BUFF_SZ,stdin)==NULL)
{
perror("fgets");
sem_v(semid);
break;
}
shm_buff_inst->pid=getpid();
sem_v(semid);
} while(strncmp(shm_buff_inst->buffer,"quit",4)!=0);
del_sem(semid);
if(shmdt(shared_memory)==1)
{
perror("shmdt");
exit(1);
}
exit(0);
}
示例14: staebchen_nehmen
int staebchen_nehmen(int my_id, int pos) {
sem_p(sem[pos]);
int n=staebchen[pos];
if (n==1) {
printf("%i nimmt %i\n", my_id, pos);
staebchen[pos]--; // ergibt 0, gibt aber chance zur fehlererkennung
sem_v(sem[pos]);
return 1;
} else if (n==0) {
sem_v(sem[pos]);
return 0;
} else {
printf("Fehler: staebchen[%i]=%i\n", pos, n);
sem_v(sem[pos]);
exit(1);
}
}
示例15: create_instance_backing
int create_instance_backing (ncInstance * instance)
{
int ret = ERROR;
virtualMachine * vm = &(instance->params);
artifact * sentinel = NULL;
// ensure instance directory exists
set_path (instance->instancePath, sizeof (instance->instancePath), instance, NULL);
if (ensure_directories_exist (instance->instancePath, 0, NULL, "root", BACKING_DIRECTORY_PERM) == -1)
goto out;
// set various instance-directory-relative paths in the instance struct
set_path (instance->xmlFilePath, sizeof (instance->xmlFilePath), instance, "instance.xml");
set_path (instance->libvirtFilePath, sizeof (instance->libvirtFilePath), instance, "libvirt.xml");
set_path (instance->consoleFilePath, sizeof (instance->consoleFilePath), instance, "console.log");
if (strstr (instance->platform, "windows")) {
// generate the floppy file for windows instances
if (makeWindowsFloppy (nc_state.home, instance->instancePath, instance->keyName, instance->instanceId)) {
logprintfl (EUCAERROR, "[%s] error: could not create windows bootup script floppy\n", instance->instanceId);
goto out;
} else {
set_path (instance->floppyFilePath, sizeof (instance->floppyFilePath), instance, "floppy");
}
}
char work_prefix [1024]; // {userId}/{instanceId}
set_id (instance, NULL, work_prefix, sizeof (work_prefix));
// compute tree of dependencies
sentinel = vbr_alloc_tree (vm, // the struct containing the VBR
FALSE, // for Xen and KVM we do not need to make disk bootable
TRUE, // make working copy of runtime-modifiable files
(instance->do_inject_key)?(instance->keyName):(NULL), // the SSH key
instance->instanceId); // ID is for logging
if (sentinel == NULL) {
logprintfl (EUCAERROR, "[%s] error: failed to prepare backing for instance\n", instance->instanceId);
goto out;
}
sem_p (disk_sem);
// download/create/combine the dependencies
int rc = art_implement_tree (sentinel, work_bs, cache_bs, work_prefix, INSTANCE_PREP_TIMEOUT_USEC);
sem_v (disk_sem);
if (rc != OK) {
logprintfl (EUCAERROR, "[%s] error: failed to implement backing for instance\n", instance->instanceId);
goto out;
}
if (save_instance_struct (instance)) // update instance checkpoint now that the struct got updated
goto out;
ret = OK;
out:
if (sentinel)
art_free (sentinel);
return ret;
}