void init() { int i, j, k, pid; int string_length; int to_exec_length; char * to_exec; char * ct_bin = "bin/"; ipc_destroy(server_params); ipc_create(server_params); ipc_open(server_params, O_RDONLY); ipc_destroy(server_receive_params); ipc_create(server_receive_params); ipc_open(server_receive_params, O_RDONLY|O_NONBLOCK); //sem_init(&sem,0,0); for(i = 0; i < CANT_INSTRUCTIONS; i++) { switch(pid = fork()) { case -1: perror("Fork exception"); exit(1); break; case 0: /* Hijo */ (*(process_list[i]))->pid = getpid(); to_exec_length = strlen(process_name_list[i]) + BIN_LENGTH + 1; to_exec = calloc(1, to_exec_length); strcpy(to_exec, ct_bin); strcat(to_exec, process_name_list[i]); to_exec[to_exec_length - 1] = 0; execvp(to_exec, NULL); /* No deberia llegar aca */ perror("Process error"); exit(1); break; } (*(process_list[i]))->params->unique_id = pid; ipc_destroy((*process_list[i])->params); ipc_create((*process_list[i])->params); ipc_open((*(process_list[i]))->params, O_WRONLY); } }
void server_close(){ int i; printf("Closing server...\n"); for(i = 0; i < CANT_INSTRUCTIONS; i++) ipc_destroy((*(process_list[i]))->params); semctl(server_params->semid, 293845, IPC_RMID); ipc_destroy(server_params); ipc_destroy(server_receive_params); free_processes(); printf("Server closed.\n"); exit(0); }
static PLI_INT32 tick_calltf(PLI_BYTE8 *user) { struct migensim_softc *sc = (struct migensim_softc *)user; int r; if(!ipc_tick(sc->ipc)) { perror("ipc_tick"); vpi_control(vpiFinish, 1); ipc_destroy(sc->ipc); sc->ipc = NULL; return 0; } r = process_until_go(sc); if(r != 1) { vpi_control(vpiFinish, r == 2 ? 0 : 1); ipc_destroy(sc->ipc); sc->ipc = NULL; return 0; } return 0; }
int main(int argc, char ** argv){ char * program_name; struct status cl_program; int i; ipc_params_t client_params; init_processes(); if(argc > 1){ program_name = argv[1]; }else{ printf("Entrada incorrecta\n"); return 0; } client_params = get_params_from_pid(getpid(), PROGRAM_STATUS, sizeof(struct status)); client_header_t header = calloc(1, sizeof(struct client_header)); header->program_size = strlen(program_name); header->client_id = getpid(); ipc_open(server_params, O_WRONLY); server_params->msg_type = PRE_HEADER; ipc_send(server_params, header, sizeof(struct client_header)); server_params->msg_type = PROGRAM_STRING; ipc_send(server_params, program_name, header->program_size); ipc_close(server_params); ipc_create(client_params); ipc_open(client_params, O_RDONLY); while (!ipc_receive(client_params, &cl_program, sizeof(struct status))); ipc_close(client_params); ipc_destroy(client_params); for (i = 0; i < MEM_SIZE; i++){ printf("%d ", cl_program.mem[i]); } printf("\n"); return 0; }
int foo() { struct calc_args calc; calc.left = 123; calc.right = 321; calc.opcode = '+'; int ret; logi("calc string=\"%d %c %d\"\n", calc.left, calc.opcode, calc.right); struct ipc *ipc = ipc_create(IPC_CLIENT, 5555); logi("before ipc_call\n"); if (0 != ipc_call(ipc, IPC_CALC, &calc, sizeof(calc), &ret, sizeof(ret))) { loge("ipc_call %d failed!\n", IPC_CALC); return -1; } logi("ipc_call IPC_CALC success!\n"); logi("return value = %d\n", ret); ipc_destroy(ipc); return 0; }
int shell_test() { char buf[1024]; char cmd[512]; int loop = 1; struct ipc *ipc = ipc_create(IPC_CLIENT, 5555); if (!ipc) { loge("ipc_create failed!\n"); return -1; } while (loop) { memset(buf, 0, sizeof(buf)); printf("hack shell$ "); scanf("%s", cmd); printf("cmd = %s\n", cmd); ipc_call(ipc, IPC_SHELL_HELP, cmd, sizeof(cmd), buf, sizeof(buf)); printf("ret = %s\n", buf); } ipc_destroy(ipc); return 0; }
int main(int argc,char const **argv) { stor_t *d; char* name=malloc(128); int return_val=0; ipc_t ctx; fprintf(stderr,"-----------------\n"); fprintf(stderr,"simpleblocksd - v%s\n",VERSION_STRING); fprintf(stderr,"-----------------\n"); /* Setup program globals */ init_signals(); init_globals(); fprintf(stderr,"Parse args...\n"); parse_args(argc,(const char **)argv); /* Create IPC communication context */ fprintf(stderr,"Setup IPC...\n"); ipc_init(&ctx,"simpleblocksd"); /* Check to see if file exists */ // fprintf(stderr,"Create disk...\n"); // if(access(g.disk_dir,F_OK)==-1) // { // if(g.create==true) // { // // alocate_file(g.disk_dir,g.size); // } // else // { // fprintf(stderr,"File not found: %s\n",g.disk_dir); // fprintf(stderr,"-c --create flag not set.\n"); // return_val=1; // } // } // // d=load_disk_path(g.disk_dir,g.create); // if(d==NULL) // { // fprintf(stderr,"Can't open directory!\n"); // return_val=2; // } // errno=0; shmb_t *block=ctx.block; uint32_t sleep_ticks=kSLEEP_TICKS_DEFAULT; int result=0; #ifdef kATUOEXIT_TIME time_t start_t,end_t; time(&start_t); #endif fprintf(stderr,"-----------------\n"); fprintf(stderr,"Ready...\n"); while(!g.done) { errno=0; sched_yield(); #ifdef kATUOEXIT_TIME time(&end_t); g.done=!(difftime(end_t,start_t)<kATUOEXIT_TIME); #endif result=sem_trywait(ctx.block_sem); if(result==0) { if(ctx.block_epoc!=block->epoc) { if(block->head.type==eEmpty) { fprintf(stderr,"Empty header\n"); sleep_ticks=kSLEEP_TICKS_SLOW; } else if(block->head.type==eStatus) { sleep_ticks=kSLEEP_TICKS_DEFAULT; // fprintf(stderr,"block->type=%d\n",block->head.type); fprintf(stderr,"eStatus\n"); } else if(block->head.type==eDiskStatus) { sleep_ticks=kSLEEP_TICKS_DEFAULT; // fprintf(stderr,"block->type=%d\n",block->head.type); fprintf(stderr,"eDiskStatus\n"); } else if(block->head.type==eBlockRead) { sleep_ticks=kSLEEP_TICKS_DEFAULT; // fprintf(stderr,"block->type=%d\n",block->head.type); fprintf(stderr,"eBlockRead\n"); } else if(block->head.type==eBlockWrite) { sleep_ticks=kSLEEP_TICKS_DEFAULT; // fprintf(stderr,"block->type=%d\n",block->head.type); fprintf(stderr,"eBlockWrite\n"); } else if(block->head.type==eBlockStatus) { sleep_ticks=kSLEEP_TICKS_DEFAULT; // fprintf(stderr,"block->type=%d\n",block->head.type); fprintf(stderr,"eBlockStatus\n"); } else if(block->head.type==eAdmin) { // fprintf(stderr,"block->head.type = !valid\n"); fprintf(stderr,"eAdmin\n"); g.done=true; } else printf("Block epoc(%08x)-> Message type: %02x\n",ctx.block_epoc,block->head.type); ctx.block_epoc=block->epoc; } sem_post(ctx.block_sem); } else { fprintf(stderr,"result: %d Errno=%d \"%s\"\n",result,errno,strerror(errno)); sleep_ticks=kSLEEP_TICKS_SLOW; } usleep(sleep_ticks); } /* Cleanup */ fprintf(stderr,"\n"); fprintf(stderr,"Cleanup...\n"); fprintf(stderr,"Freeing cache...\n"); fprintf(stderr,"Closing disk...\n"); // stor_free(d); fprintf(stderr,"Closing IPC...\n"); ipc_destroy(&ctx); fprintf(stderr,"Exiting...\n"); free(name); return return_val; }
void ipc_close(ipc_t conn) { ipc_destroy(conn, 0); }
void ipc_end(void) { ipc_destroy(me, 1); }