예제 #1
0
// if create == 0: create new share-memory channel
// else: attach exist share-memory channel and verify data
bus_terminal_channel_t* bus_terminal_channel_init(int32_t shmkey, bus_addr_t from,
                                                  bus_addr_t to, size_t size,
                                                  int32_t create)
{
    struct shm_t* shm;
    bus_terminal_channel_t* btc;
    size_t alloc_size = size + sizeof(bus_channel_t) + rbuffer_head_size();

    // init terminal channel
    btc = (bus_terminal_channel_t*)MALLOC(sizeof(*btc));
    if (!btc) {
        return NULL;
    }

    // share memory
    if (create == 0) {
        shm = shm_create(shmkey, alloc_size, 0);
    } else {
        shm = shm_create(shmkey, alloc_size, 1);
    }
    if (!shm) {
        FREE(btc);
        return NULL;
    }

    // init channel or verify channel data
    btc->bc = shm_mem(shm);
    assert(btc->bc);
    if (create == 0) {
        btc->bc->shmkey = shmkey;
        btc->bc->from = from;
        btc->bc->to = to;
        btc->bc->channel_size = size;
    } else {
        if (btc->bc->shmkey != shmkey || btc->bc->from != from
            || btc->bc->to != to || btc->bc->channel_size != size) {
            FREE(btc);
            return NULL;
        }
    }

    // init ring buffer
    btc->r = rbuffer_init_mem((char*)btc->bc + sizeof(bus_channel_t),
        btc->bc->channel_size + rbuffer_head_size());
    if (!btc->r) {
        FREE(btc);
        return NULL;
    }
    return btc;
}
예제 #2
0
int
td_metrics_nbd_start(stats_t *nbd_stats, int minor)
{
    int err = 0;

    if(!td_metrics.path || nbd_stats->shm.path != NULL)
        goto out;

    shm_init(&nbd_stats->shm);

    err = asprintf(&nbd_stats->shm.path, TAPDISK_METRICS_NBD_PATHF, td_metrics.path, minor);
    if(unlikely(err == -1)){
        err = errno;
        EPRINTF("failed to allocate memory to store NBD metrics path: %s\n",strerror(err));
        nbd_stats->shm.path = NULL;
        goto out;
    }

    nbd_stats->shm.size = PAGE_SIZE;

    err = shm_create(&nbd_stats->shm);
    if (unlikely(err)) {
        err = errno;
        EPRINTF("failed to create NBD shm ring stats file: %s\n", strerror(err));
        goto out;
   }
    nbd_stats->stats = nbd_stats->shm.mem;
out:
    return err;
}
예제 #3
0
int
td_metrics_blktap_start(int minor, stats_t *blktap_stats)
{

    int err = 0;

    if(!td_metrics.path)
        goto out;

    shm_init(&blktap_stats->shm);

    err = asprintf(&blktap_stats->shm.path, TAPDISK_METRICS_BLKTAP_PATHF, td_metrics.path, minor);
    if(unlikely(err == -1)){
        err = errno;
        EPRINTF("failed to allocate memory to store blktap metrics path: %s\n",strerror(err));
        blktap_stats->shm.path = NULL;
        goto out;
    }

    blktap_stats->shm.size = PAGE_SIZE;

    err = shm_create(&blktap_stats->shm);
    if (unlikely(err)) {
        err = errno;
        EPRINTF("failed to create blktap shm ring stats file: %s\n", strerror(err));
        goto out;
    }
    blktap_stats->stats = blktap_stats->shm.mem;
out:
    return err;
}
예제 #4
0
int
td_metrics_vbd_start(int domain, int id, stats_t *vbd_stats)
{
    int err = 0;

    if(!td_metrics.path)
        goto out;

    shm_init(&vbd_stats->shm);

    err = asprintf(&vbd_stats->shm.path, TAPDISK_METRICS_VBD_PATHF,
            td_metrics.path, domain, id);
    if(unlikely(err == -1)){
        err = errno;
        EPRINTF("failed to allocate memory to store vbd metrics path: %s\n",
            strerror(err));
        vbd_stats->shm.path = NULL;
        goto out;
    }

    vbd_stats->shm.size = PAGE_SIZE;

    err = shm_create(&vbd_stats->shm);
    if (unlikely(err)) {
        err = errno;
        EPRINTF("failed to create shm ring stats file: %s\n", strerror(err));
        goto out;
   }
    vbd_stats->stats = vbd_stats->shm.mem;
out:
    return err;

}
void *soc_shm_setup(int core_id, struct sim_arg *multi_sim_arg)
{
	void *ptr;

	ptr = shm_create(multi_sim_arg->boot.soc_shm_name, MAX_SOC_SHM_INST * PAGE_SIZE * DSPNUM, O_RDWR);
	return ptr;
}
예제 #6
0
int main(int argc, char *argv[])
{
	printf("*****************************\nTest shm\n"
	       "*****************************\n");
	(void)argc;
	(void)argv;
	
	const char *c="page_test_shm";
	int fid=pcreate(1);
	int ret;
	int pid=start("test_shm1",10000,getprio(getpid()),(void *)fid);
	void *pagep = shm_create(c);
	assert(pagep!=NULL);
	*((int*)pagep+4)=23;
	psend(fid, 1);
	preceive(fid,&ret);
	printf("[process %i] : %i\n",getpid(),*((int*)pagep+4));
	pdelete(fid);
	shm_release(c);
	waitpid(pid,&ret);
	assert(shm_acquire(c)==NULL);
	printf("\n*****************************\nTest fini\n"
	       "*****************************\n");
	return 0;
}
예제 #7
0
파일: shm.c 프로젝트: jgraef/meinOS
int shmget(key_t key,size_t size,int flags) {
  int ret;
  if ((flags&IPC_CREAT) || key==IPC_PRIVATE) ret = shm_create(key,size,flags);
  if ((!(flags&IPC_CREAT) || ret<0) && !(flags&IPC_EXCL)) ret = shm_get(key);
  errno = ret<0?-ret:0;
  return ret<0?-1:ret;
}
예제 #8
0
int create_shm_index()
{
    void *pindex = NULL;

    unsigned int i = 0;
    struct shm_attr attr = {0};
    attr.entry_size = sizeof(rc_index);
    attr.n_entry = MAX_INDEX;

    p_indexs = (rc_index *)calloc(MAX_INDEX, sizeof(rc_index));
    if (p_indexs == NULL)
    {
        return -1;
    }

    index_handle = shm_attach((char *)SHM_DIRCOUNTER_INDEX);
    if (index_handle == SHM_HANDLE_FAIL)
    {
        index_handle = shm_create((char *)SHM_DIRCOUNTER_INDEX, &attr);
        if (index_handle == NULL)
        {
            free(p_indexs);
            return -1;
        }
    }

    shm_wlock(index_handle);
    for_each_shm_obj(index_handle, pindex, i)
    {
        memset(pindex, 0, sizeof(rc_index));
    }
예제 #9
0
/*** Create shared memory area ***/
void _0x94_shm_create(void) {
	uint8_t key = (uint8_t)current_process->cpu.ebx;
	uint32_t size = (uint32_t)current_process->cpu.ecx;

	current_process->cpu.edx = (uint32_t) shm_create(key, size, current_process); // return value

	current_process->state = READY;
}
void *jtag_shm_setup(int core_id, struct sim_arg *multi_sim_arg)
{
	void *ptr;
	char iss_shm_name[FILE_NAMESIZE];

	sprintf(iss_shm_name, "%s-%d", multi_sim_arg->boot.iss_shm_name, core_id);
	ptr = shm_create(iss_shm_name, PAGE_SIZE, O_CREAT | O_RDWR | O_TRUNC);
	return ptr;
}
예제 #11
0
int
sys_shm_create(void)
{
  int size;

  argint(0,&size);
  return shm_create(size);

}
예제 #12
0
void Queue::queue_write_init(){
	int shmid = shm_create();
	_shmid = shmid;
	_head = (Head *)(shm_attach(shmid));
	_head->front = 0;
	_head->rear = 0;
	_arr = (Text *)(_head+1);
//	cout<<"++"<<endl;
}
예제 #13
0
int main(void *arg)
{
    int i, count, fid, pid;
    struct tst16 *p = NULL;
    int pids[2 * NB_PROCS];

    (void)arg;
    p = (struct tst16*) shm_create("test16_shm");
    assert(p != NULL);

    assert(getprio(getpid()) == 128);
    for (count = 1; count <= 100; count++) {
        fid = pcreate(count);
        assert(fid >= 0);
        p->count = count;
        p->fid = fid;
        pid = start("proc16_1", 2000, 128, 0);
        assert(pid > 0);
        for (i=0; i<=count; i++) {
            assert(psend(fid, i) == 0);
            test_it();
        }
        assert(waitpid(pid, 0) == pid);
        assert(pdelete(fid) == 0);
    }

    p->count = 20000;
    fid = pcreate(50);
    assert(fid >= 0);
    p->fid = fid;
    for (i = 0; i< NB_PROCS; i++) {
        pid = start("proc16_2", 2000, 127, 0);
        assert(pid > 0);
        pids[i] = pid;
    }
    for (i=0; i < NB_PROCS; i++) {
        pid = start("proc16_3", 2000, 127, 0);
        assert(pid > 0);
        pids[NB_PROCS + i] = pid;
    }
    for (i=0; i < 2 * NB_PROCS; i++) {
        assert(waitpid(pids[i], 0) == pids[i]);
    }
    assert(pcount(fid, &count) == 0);
    assert(count == 0);
    assert(pdelete(fid) == 0);

    shm_release("test16_shm");
    printf("ok.\n");
    return 0;
}
예제 #14
0
static int
vbd_stats_create(td_vbd_t *vbd) {

    int err;

    ASSERT(vbd);

    err = mkdir("/dev/shm/metrics", S_IRUSR | S_IWUSR);
    if (likely(err)) {
        err = errno;
        if (unlikely(err != EEXIST))
            goto out;
        else
            err = 0;
    }

    /*
     * FIXME Rename this to something like "vbd3-domid-devid". Consider
     * consolidating this with the io_ring shared memory file. Check if blkback
     * exports the same information in some sysfs file and if so move this to
     * the ring location.
     */
    err = asprintf(&vbd->rrd.shm.path, "/dev/shm/metrics/tap-%d-%d", getpid(),
                   vbd->uuid);
    if (err == -1) {
        err = errno;
        vbd->rrd.shm.path = NULL;
        EPRINTF("failed to create metric file: %s\n", strerror(err));
        goto out;
    }
    err = 0;

    vbd->rrd.shm.size = PAGE_SIZE;
    err = shm_create(&vbd->rrd.shm);
    if (err)
        EPRINTF("failed to create RRD: %s\n", strerror(err));

out:
    if (err) {
        int err2 = vbd_stats_destroy(vbd);
        if (err2)
            EPRINTF("failed to clean up failed RRD shared memory creation: "
                    "%s (error ignored)\n", strerror(-err2));
    }
    return -err;
}
예제 #15
0
파일: tst2.c 프로젝트: koujinogaku/helloos
void tst_mutex(void)
{
  int r,r1;
  char *ctl;
  int *mutex;
  char *m1;

  r1=shm_create(SHRMEMNAME,8*1024);
  display_puts("shm create=");
  sint2dec(r1,s);
  display_puts(s);

  r=shm_map(SHRMEMNAME,SHRMEMHEAP);
  display_puts(" shm map=");
  sint2dec(r,s);
  display_puts(s);
  display_puts("\n");

  mutex = SHRMEMHEAP;
  ctl = (void*)((unsigned long)SHRMEMHEAP+sizeof(int));
  if(r1==0) {
    *mutex=0;
    syscall_mtx_lock(mutex);
    memory_init(ctl,8*1024-sizeof(int));
    display_puts(" init heap\n");
    syscall_mtx_unlock(mutex);
  }

  syscall_mtx_lock(mutex);
  display_puts("lock tst2\n");
  m1 = memory_alloc(10,ctl);
  display_puts("alloc tst2\n");
  syscall_mtx_unlock(mutex);
  display_puts("unlock tst2\n");

  memcpy(m1,"efgh",5);
  display_puts("memcpy tst2\n");
  syscall_wait(1000);
  display_puts("sleeping tst2\n");
  display_puts("dump tst2=");
  display_puts(m1);
  display_puts("\n");

}
예제 #16
0
파일: main_a.c 프로젝트: MaxAlmeida/FSO
int main(int argc, char* argv[]){
  flags(argc,argv);
  int qid_send = create_queue();
  int qid_receive = create_queue();
  int pid = fork();
  Msg msg_check;
  key_t key = KEY;
  int shmid = shm_create(key);
  strcpy(msg_check.text, CODE);
  shm_write_process(msg_check);
  if(pid > 0){ // Parent
    parent_a(qid_send, qid_receive);
  }else if(pid == 0){ // Child
    child_a(qid_send, qid_receive);
  } else if(pid < 0){ // Fail
    printf("Failed to create process!\n", pid);
    exit(-1);
  }

} // fim-main
예제 #17
0
static int cj_create (int nxjlib, int mlibn, const char **mlibs, int jlibn, const char **jlibs)
{
	assert(cj_state == CJS_UNINIT);

	if (socketpair(AF_UNIX, SOCK_STREAM, 0, socks)) {
		fprintf(stderr, "socketpair() failed.\n");
		return 1;
	}

	if (shm_create(mlibn, mlibs, jlibn, jlibs))
		return 1;

	if (clone(child_main, stack_jail + JSTACK_SIZE, CLONE_FILES|CLONE_FS|SIGCHLD, NULL) == -1) {
		fprintf(stderr, "clone() failed.\n");
		return 2;
	}

	// parent
	cj_state = CJS_MAIN;
	// close(socks[1]); // cannot close because CLONE_FILES
	if (nxjlib)
		drop_jlib_exec(jlibn, jlibs);
	{
		unsigned long oldbos = getbos();
		oldbos = ((oldbos - 1) & 0xfffff000) + 4096; // round-up to page bound
		jump_stack(oldbos, (unsigned long)stack_main + MSTACK_SIZE);
	}
	cj_alloc_init();
	refmon_init();
	//pthread_mutex_init(&sock_mutex, PTHREAD_MUTEX_RECURSIVE);
	{
		int i, stub_size;
		callbacks = (struct callback_struct *)calloc(MAXCALLBACKS, sizeof(struct callback_struct));
		assert(cj_memtype(callbacks) == CJMT_PRIVATE);
		stub_size = (char *)cj_callback_stub1 - (char *)cj_callback_stub0;
		for (i = 0; i < MAXCALLBACKS; i ++)
			callbacks[i].stub = (void *)cj_callback_stub0 + i * stub_size;
	}

	return 0;
}
예제 #18
0
파일: mail.c 프로젝트: avkrotov/fdm
int
mail_open(struct mail *m, size_t size)
{
	m->size = 0;
	m->space = IO_ROUND(size);
	m->body = 0;

	if ((m->base = shm_create(&m->shm, m->space)) == NULL)
		return (-1);
	SHM_REGISTER(&m->shm);

	m->off = 0;
	m->data = m->base + m->off;

	strb_create(&m->tags);
	ARRAY_INIT(&m->wrapped);
	m->wrapchar = '\0';
	m->attach = NULL;
	m->attach_built = 0;

	return (0);
}
예제 #19
0
int32_t _bus_terminal_init_create(bus_terminal_t* bt, int16_t key)
{
    int32_t buskey = (key << 16);
    struct shm_t* shm = shm_create(buskey, sizeof(bus_t), 0);
    if (shm) {
        bt->bus = (bus_t*)shm_mem(shm);
        assert(bt->bus);

        bt->bus->key = buskey;
        bt->bus->channel_key = 0;
        bt->bus->size = sizeof(bus_t);

        // terminals local info
        bt->bus->terminal_count = 1;
        bt->bus->terminals[0] = bt->self;
        atom_set(&bt->bus->terminal_version, 1);

        // channels info
        bt->bus->channel_count = 0;
        atom_set(&bt->bus->channel_version, 1);
        return 0;
    }
    return -1;
}
예제 #20
0
int32_t _bus_terminal_init_attach(bus_terminal_t* bt, int16_t key)
{
    int32_t buskey = (key << 16);
    int32_t i;
    struct shm_t* shm;
    int32_t registered = -1;

    // attach exsit shm
    shm = shm_create(buskey, sizeof(bus_t), 1);
    if (!shm) {
        return -1;
    }
    bt->bus = (bus_t*)shm_mem(shm);

    // invalid shm data
    if (bt->bus->key != buskey || bt->bus->size != sizeof(bus_t)) {
        return -1;
    }

    // register terminal info
    for (i = 0; i < bt->bus->terminal_count; ++ i) {
        if (bt->bus->terminals[i] == bt->self) {
            registered = 0;
            break;
        }
    }
    if (registered != 0) {
        if (bt->bus->terminal_count >= BUS_MAX_TERMINAL_COUNT) {
            return -1;
        } else {
            bt->bus->terminals[bt->bus->terminal_count ++] = bt->self;
            atom_inc(&bt->bus->terminal_version);
        }
    }
    return 0;
}
예제 #21
0
//reg user info
key_t initOnlineShmat()
{
    key_t shmid = shm_create();
    return shmid;
}
예제 #22
0
int main(int argc, char *argv[])
{
    int sockfd, clientfd, sin_size, recvbytes;  //定义监听套接字、客户套接字
    pid_t pid, ppid;            //定义父子线程标记变量
    char *buf, *r_addr, *w_addr, *temp, *time_str;      //="\0"; //定义临时存储区
    struct sockaddr_in their_addr;      //定义地址结构
    key_t shmid;
    shmid = shm_create();       //创建共享存储区
    temp = (char *) malloc(255);
    time_str = (char *) malloc(20);
    sockfd = bindPort(MYPORT);  //绑定端口
    while (1)
    {
        if (listen(sockfd, BACKLOG) == -1)      //在指定端口上监听
        {
            perror("listen");
            exit(1);
        }
        printf("listening......\n");
        if ((clientfd = accept(sockfd, (struct sockaddr *) &their_addr, &sin_size)) == -1)      //接收客户端连接
        {
            perror("accept");
            exit(1);
        }
        printf("accept from:%d\n", inet_ntoa(their_addr.sin_addr));
        send(clientfd, WELCOME, strlen(WELCOME), 0);    //发送问候信息
        buf = (char *) malloc(255);
        ppid = fork();          //创建子进程
        if (ppid == 0)
        {
            //printf("ppid=0\n");
            pid = fork();       //创建子进程  
            while (1)
            {
                if (pid > 0)
                {
                    //父进程用于接收信息
                    memset(buf, 0, 255);
                    //printf("recv\n");
                    //sleep(1);
                    if ((recvbytes = recv(clientfd, buf, 255, 0)) <= 0)
                    {
                        perror("recv1");
                        close(clientfd);
                        raise(SIGKILL);
                        exit(1);
                    }
                    //write buf's data to share memory
                    w_addr = shmat(shmid, 0, 0);
                    memset(w_addr, '\0', 1024);
                    strncpy(w_addr, buf, 1024);
                    get_cur_time(time_str);
                    strcat(buf, time_str);
                    printf(" %s\n", buf);
                }
                else if (pid == 0)
                {
                    //子进程用于发送信息
                    //scanf("%s",buf);
                    sleep(1);
                    r_addr = shmat(shmid, 0, 0);
                    //printf("---%s\n",r_addr);
                    //printf("cmp:%d\n",strcmp(temp,r_addr));
                    if (strcmp(temp, r_addr) != 0)
                    {
                        strcpy(temp, r_addr);
                        get_cur_time(time_str);
                        strcat(r_addr, time_str);
                        //printf("discriptor:%d\n",clientfd);
                        //if(send(clientfd,buf,strlen(buf),0) == -1)
                        if (send(clientfd, r_addr, strlen(r_addr), 0) ==
                            -1)
                        {
                            perror("send");
                        }
                        memset(r_addr, '\0', 1024);
                        strcpy(r_addr, temp);
                    }
                }
                else
                    perror("fork");
            }
        }
    }
    printf("------------------------------\n");
    free(buf);
    close(sockfd);
    close(clientfd);
    return 0;
}
예제 #23
0
int main(int argc, char **argv)
{
	int ret = -1;
	int c, optidx = 0;
	static struct option options[] = {
		{ "command", 1, 0, 'c' },
		{ "name", 1, 0, 'n' },
		{ "size", 1, 0, 's' },
		{ "perm", 1, 0, 'p' },
		{ "flag", 1, 0, 'f' },
		{ "help", 0, 0, 'h' },
		{ 0, },
	};

	for( ; ; ) {
		c = getopt_long(argc, argv, "c:n:s:p:f:h", options, &optidx);
		if(c == -1)
			break;

		switch(c) {
		case 'c':
			if(s_command) {
				fprintf(stderr, "command is already setup.\n");
				exit(EXIT_FAILURE);
			}
			
			if(strcasecmp(optarg, "create") == 0) {
				s_command = CMD_CREATE;
			}
			else if(strcasecmp(optarg, "remove") == 0) {
				s_command = CMD_REMOVE;
			}
			else {
				fprintf(stderr, "%s is unknown.\n", optarg);
				exit(EXIT_FAILURE);
			}
			break;

		case 'n':
			if(s_name) {
				fprintf(stderr, "name is already setup.\n");
				exit(EXIT_FAILURE);
			}
			s_name = strdup(optarg);
			ASSERT(s_name);
			break;
		case 's':
			s_size = atoi(optarg);
			break;
		case 'p':
			s_perm = atoi(optarg);
			break;
		case 'f':
			s_flags = atoi(optarg);
			break;
		case 'h':
			help();
			break;
		default:
			fprintf(stderr, "Unknown options.\n");
			exit(EXIT_FAILURE);
			break;
		}
	}

	switch(s_command) {
		case	CMD_CREATE:
			if(!s_name) {
				fprintf(stderr, "name is not setup.\n");
				exit(EXIT_FAILURE);
			}
			if(s_size <= 0) {
				fprintf(stderr, "Size %d is invalid.\n", 
						s_size);
				exit(EXIT_FAILURE);
			}
			ret = shm_create(s_name, s_size, s_perm, s_flags);
			break;
		case	CMD_REMOVE:
			if(!s_name) {
				fprintf(stderr, "name is not setup.\n");
				exit(EXIT_FAILURE);
			}
			ret = shm_remove(s_name);
			break;
		default:
			fprintf(stderr, "Command is not setup.\n");
			exit(EXIT_FAILURE);
			break;
	}

	if(ret < 0) {
		exit(EXIT_FAILURE);
	}

	exit(EXIT_SUCCESS);
}
예제 #24
0
파일: td-blkif.c 프로젝트: andyhhp/blktap
/*
 * TODO provide ring stats in raw format (the same way I/O stats are provided).
 * xen-ringwatch will have to be modified accordingly.
 */
static int
tapdisk_xenblkif_stats_create(struct td_xenblkif *blkif)
{
    int err = 0, len;
    char *_path = NULL;

    len = asprintf(&blkif->xenvbd_stats.root, "/dev/shm/vbd3-%d-%d",
            blkif->domid, blkif->devid);
    if (unlikely(len == -1)) {
        err = errno;
        blkif->xenvbd_stats.root = NULL;
        goto out;
    }

	err = mkdir(blkif->xenvbd_stats.root, S_IRUSR | S_IWUSR);
	if (unlikely(err)) {
        err = errno;
        if (err != EEXIST) {
            EPRINTF("failed to create %s: %s\n",
                    blkif->xenvbd_stats.root, strerror(err));
    		goto out;
        }
        err = 0;
    }

    len = asprintf(&blkif->xenvbd_stats.io_ring.path, "%s/io_ring~",
            blkif->xenvbd_stats.root);
    if (unlikely(len == -1)) {
        err = errno;
        blkif->xenvbd_stats.io_ring.path = NULL;
        goto out;
    }
    blkif->xenvbd_stats.io_ring.size = PAGE_SIZE;
    err = shm_create(&blkif->xenvbd_stats.io_ring);
    if (unlikely(err)) {
        err = errno;
        EPRINTF("failed to create shm ring stats file: %s\n", strerror(err));
        goto out;
   }

    err = asprintf(&blkif->xenvbd_stats.stats.path, "%s/statistics",
            blkif->xenvbd_stats.root);
    if (unlikely(err == -1)) {
        err = errno;
        blkif->xenvbd_stats.stats.path = NULL;
        goto out;
    }
    blkif->xenvbd_stats.stats.size = PAGE_SIZE;
    err = shm_create(&blkif->xenvbd_stats.stats);
    if (unlikely(err))
        goto out;

    blkif->xenvbd_stats.last = 0;

	blkif->stats.xenvbd = blkif->xenvbd_stats.stats.mem;

    if (tapdisk_server_mem_mode()) {
        td_flag_set(blkif->stats.xenvbd->flags, BT3_LOW_MEMORY_MODE);
    }

    err = tapdisk_xenblkif_ring_stats_update(blkif);
    if (unlikely(err)) {
        EPRINTF("failed to generate shared I/O ring stats: %s\n",
                strerror(-err));
        goto out;
    }

    _path = strndup(blkif->xenvbd_stats.io_ring.path, len - 1);
    if (unlikely(!_path)) {
        err = errno;
        goto out;
    }

    err = rename(blkif->xenvbd_stats.io_ring.path, _path);
    if (unlikely(err)) {
        err = errno;
        goto out;
    }

    free(blkif->xenvbd_stats.io_ring.path);
    blkif->xenvbd_stats.io_ring.path = _path;
    _path = NULL;
out:
    free(_path);
    if (err) {
        int err2 = tapdisk_xenblkif_stats_destroy(blkif);
        if (err2)
            EPRINTF("failed to clean up failed stats file: "
                    "%s (error ignored)\n", strerror(-err2));
    }
    return -err;
}
예제 #25
0
파일: shm_test.c 프로젝트: rit-sos/SOS
void main(void) {
	unsigned int pid;
	unsigned int status;
	shared_object *ptr;

	char *local_str1 = "Hello from the parent\n";
	char *local_str2 = "Hello from the child\n";

	status = fork(&pid);

	if (status != SUCCESS) {
		/* failure */
		puts("shm_test: fork: ");
		putx(status);
		puts("\n");
		exit();
	} else if (pid == 0) {
		/* child */
		msleep(2500);
		puts("shm_test: child: good morning\n");

		status = shm_open("test", (void**)&ptr);

		if (status != SUCCESS) {
			puts("shm_test: shm_open: ");
			putx(status);
			puts("\n");
			exit();
		}

		putx(ptr->x); puts(" "); putx(ptr->y); puts(" "); putx(ptr->z); puts("\n");
		puts("shared str1: ");
		puts(ptr->str1);

		ptr->x++; ptr->y++; ptr->z++;

		memcpy(ptr->str2, local_str2, strlen(local_str2) + 1);

		status = shm_close("test");

		ptr->x++;

		exit();
	} else {
		/* parent */
		puts("shm_test: parent: creating shm region \"test\"\n");

		status = shm_create("test", 0x4000, SHM_WRITE, (void**)&ptr);

		if (status != SUCCESS) {
			puts("shm_test: shm_create: ");
			putx(status);
			puts("\n");
			exit();
		}

		ptr->x = 0x10;
		ptr->y = 0x20;
		ptr->z = 0x30;

		memcpy(ptr->str1, local_str1, strlen(local_str1) + 1);

		msleep(5000);

		puts("shm_test: parent: good morning\n");
		putx(ptr->x); puts(" "); putx(ptr->y); puts(" "); putx(ptr->z); puts("\n");
		puts("shared str2: ");
		puts(ptr->str2);

		exit();
	}
}
예제 #26
0
int main(int argc, char *argv[])
{
    int clientfd;
    char *buf;

    //绑定端口
    int sockfd = bindPort(MYPORT);
    
    //创建共享存储区
    key_t shmid, onlineShmid;
    char *r_addr, *w_addr;
    shmid = shm_create();
    w_addr = shmat(shmid, 0, 0);
    r_addr = shmat(shmid, 0, 0);
    
    onlineShmid = initOnlineShmat();
    
    // //定义临时存储区
    char *temp, *time_str;
    temp = (char *)malloc(255);
    time_str=(char *)malloc(20);
    
    
    while(1)
    {
        //在指定端口上监听
        if(listen(sockfd,BACKLOG) == -1)
        {
            perror("listen error");
            exit(1);
        }
        printf("listening......\n");
        
        
        //接收客户端连接
        int sin_size = 0;
        struct sockaddr_in their_addr;
        if((clientfd = accept(sockfd,(struct sockaddr*)&their_addr,&sin_size)) == -1)
        {
            perror("accept error");
            exit(1);
        }
        printf("discriptor:%d\n",clientfd);
        printf("accept from:%d\n",inet_ntoa(their_addr.sin_addr));
        
        //发送问候信息
        send(clientfd, WELCOME, strlen(WELCOME), 0);
        
        //build buff
        buf = (char *)malloc(255);
        
        //创建子进程
        pid_t ppid = fork();
        if(ppid == 0)
        {
            printf("fork ppid=0\n");
            
            //再次创建子进程
            pid_t pid = fork();
            bool bBeRequested = false;
            while(1)
            {
                //父进程用于接收信息
                if(pid > 0)
                {
                    memset(buf,0,255);
                    printf("begin recv\n");
                    //sleep(1);
                    
                    int recvbytes = 0;
                    if((recvbytes = recv(clientfd,buf,255,0)) <= 0)
                    {
                        perror("recv1 error");
                        close(clientfd);
                        raise(SIGKILL);
                        exit(1);
                    }
                    
                    
                    //write buf's data to share memory
                    memset(w_addr, '\0', 1024);
                    strncpy(w_addr, buf, 1024);
                    bBeRequested = true;
                    printf("w_addr->%s\n",w_addr);
                    
                    
                    //strcat time info
                    get_cur_time(time_str);
                    strcat(buf,time_str);
                    printf("buf:%s\n",buf);
                }
                
                //子进程用于发送信息
                else if(pid == 0)
                {
                    sleep(1);
                    printf("r_addr:|%s| temp:|%s|\n",r_addr, temp);
                    
                    //swap shmat buffer
                    if(strcmp(temp,r_addr) != 0)
                    {
                        printf("swap buffer!\n");
                        get_cur_time(time_str);
                        //strcat(r_addr,time_str);
                        strcpy(temp,r_addr);
                        memset(r_addr, '\0', 255);
                        printf("temp:%s\n",temp);

                        char *delims = { "@" };
                        char *cmd = strtok( temp, delims);
                        if( NULL == cmd )
                        {
                            printf("temp:%s parse error,no cmd!\n", temp);
                            memset(temp, '\0', 1024);
                            continue;
                        }
                        
                        
                        char* rbody = strtok( NULL, delims);
                        if( NULL == rbody )
                        {
                            printf("temp:%s parse error,no rbody!\n", temp);
                            memset(temp, '\0', 1024);
                            continue;
                        }
                        printf("cmd:%s rbody:%s\n", cmd, rbody);
                        
                        
                        if( 0 == strcmp(cmd, "reg") )
                        {
                            regOnlineShmat(onlineShmid, rbody);
                        }
                        

                        //send temp buffer
                        //if(send(clientfd,temp,strlen(temp),0) == -1)
                        //{
                        //    perror("send error");
                        //}
                        memset(temp, '\0', 1024);
                    }
                }
                else
                {
                    perror("fork error");
                }
            }
        }
    }
    printf("------------------------------\n");
    free(buf);
    close(sockfd);
    close(clientfd);
    return 0;
}
예제 #27
0
int syscall_shm_create(unsigned int shmname, unsigned long size, unsigned int options)
{
  return shm_create(shmname, size, options);
}
예제 #28
-2
int		SSQ_Init(SS_QUEUE_OBJ_T *pObj, unsigned int sharememory, unsigned int channelid, wchar_t *sharename, unsigned int bufsize, unsigned int prerecordsecs, unsigned int createsharememory)
{
	wchar_t wszHeaderName[36] = {0,};
	wchar_t wszFramelistName[36] = {0,};
	wchar_t wszDataName[36] = {0,};
	if (NULL==pObj)											return -1;
	if (createsharememory==0x01 && bufsize<1)				return -1;
	if ( (sharememory==0x01) && (NULL==sharename || (0==wcscmp(sharename, TEXT("\0")))) )	return -1;

	memset(pObj, 0x00, sizeof(SS_QUEUE_OBJ_T));
	pObj->channelid = channelid;
	pObj->shareinfo.id = channelid;
	wcscpy(pObj->shareinfo.name, sharename);

	wchar_t wszMutexName[36] = {0,};
	wsprintf(wszMutexName, TEXT("%s%d_mutex"), sharename, channelid);
	pObj->hMutex = OpenMutex(NULL, FALSE, wszMutexName);
	if (NULL == pObj->hMutex)
	{
		pObj->hMutex = CreateMutex(NULL, FALSE, wszMutexName);
		if (NULL == pObj->hMutex)							return -1;
	}

	//Create Header map
	
	
#ifdef _WIN32
	if (sharememory == 0x01)
	{
		wsprintf(wszHeaderName, TEXT("%s%d_h"), sharename, channelid);
		pObj->hSSHeader = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, wszHeaderName);
		if (NULL==pObj->hSSHeader && createsharememory==0x01)
		{
			pObj->hSSHeader = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE|SEC_COMMIT, 0, sizeof(SS_HEADER_T), wszHeaderName);
			if (NULL==pObj->hSSHeader || pObj->hSSHeader==INVALID_HANDLE_VALUE)
			{
				return -1;
			}
		}
		pObj->pQueHeader = (SS_HEADER_T*)MapViewOfFile(pObj->hSSHeader, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 0);
		if (createsharememory==0x01)
		{
			if (pObj->pQueHeader->bufsize < 1)
			{
				memset(pObj->pQueHeader, 0x00, sizeof(SS_HEADER_T));
				pObj->pQueHeader->bufsize = bufsize;
			}
		}
		else if (NULL==pObj->pQueHeader)
		{
			return -1;
		}
		else
		{
			bufsize = pObj->pQueHeader->bufsize;
 		}
	}
	else
	{
		pObj->pQueHeader = new SS_HEADER_T;
		memset(pObj->pQueHeader, 0x00, sizeof(SS_HEADER_T));
	}

	//==========================================
	//Create frame list map
	if (prerecordsecs > 0)
	{
		wsprintf(wszFramelistName, TEXT("%s%d_f"), sharename, channelid);
		unsigned int nFramelistNum = prerecordsecs * 30;	//每秒30帧
		unsigned int nFrameQueSize = nFramelistNum*sizeof(FRAMEINFO_LIST_T);

		if (sharememory == 0x01)
		{
			pObj->hSSFrameList = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, wszFramelistName);
			if (NULL==pObj->hSSFrameList && createsharememory==0x01)
			{
				pObj->hSSFrameList = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE|SEC_COMMIT, 0, nFrameQueSize, wszFramelistName);
				if (NULL==pObj->hSSFrameList || pObj->hSSFrameList==INVALID_HANDLE_VALUE)
				{
					return -1;
				}
			}
			pObj->pFrameinfoList = (FRAMEINFO_LIST_T*)MapViewOfFile(pObj->hSSFrameList, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 0);
			if (createsharememory==0x01)
			{
				memset(pObj->pFrameinfoList, 0x00, nFrameQueSize);
				pObj->pQueHeader->framelistNum = nFramelistNum;
			}
			else if (NULL==pObj->hSSFrameList)
			{
				return -1;
			}
		}
		else
		{
			pObj->pFrameinfoList = new FRAMEINFO_LIST_T[nFramelistNum];
			memset(&pObj->pFrameinfoList[0], 0x00, sizeof(FRAMEINFO_LIST_T)*nFramelistNum);
			pObj->pQueHeader->framelistNum = nFramelistNum;
		}
	}

	//Create data map
	
	if (sharememory == 0x01)
	{
		wsprintf(wszDataName, TEXT("%s%d_b"), sharename, channelid);
		pObj->hSSData	= OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, wszDataName);
		if (NULL==pObj->hSSData && createsharememory==0x01)
		{
			pObj->hSSData = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE|SEC_COMMIT, 0, bufsize, wszDataName);
		}
		if (NULL == pObj->hSSData || pObj->hSSData==INVALID_HANDLE_VALUE)
		{
			return -1;
		}
		pObj->pQueData = (char*)MapViewOfFile(pObj->hSSData, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 0);
	}
	else
	{
		pObj->pQueData = new char [bufsize];
		pObj->pQueHeader->bufsize = bufsize;
	}
	if (createsharememory==0x01)
	{
		//memset(pQueHeader, 0x00, sizeof(SS_HEADER_T));
		memset(pObj->pQueData, 0x00, bufsize);
	}
#else
	int ret = shm_create((SYNC_VID_SHM_KEY<<8)|channelid, &pObj->shmHdrid, sizeof(SS_HEADER_T), (char**)&pObj->pQueHeader);
	if (ret < 0)
	{
		return -1;
	}
	SSQ_TRACE("[%d]pQueHeader: %d\n", (SYNC_VID_SHM_KEY<<8)|channelid, pObj->shmHdrid);
	ret = shm_create((SYNC_VID_SHM_KEY<<16)|channelid, &pObj->shmDatid, bufsize, (char**)&pObj->pQueData);
	if (ret < 0)
	{
		shm_delete(&pObj->shmHdrid, (char*)pObj->pQueHeader);
		return -1;
	}
	pObj->pQueHeader->bufsize = bufsize;
	SSQ_TRACE("[%d]pQueData: %d\n", (SYNC_VID_SHM_KEY<<16)|channelid, pObj->shmDatid);
#endif

	return 0;
}