// function for text editor (mode 2) calculation int cal_texteditor(void){ printf("DEBUG: text editor function entered\n"); typing_count(0); // initialize counter on FND while(*mode_shm == '2'){ if(input_shm[10] != '*'){ // change mode to 3, if btn2 & btn3 are pressed if(input_shm[1] == 1 && input_shm[2] == 1){ *mode_shm = '3'; init_shared(); } // clear lcd screen, if btn4 & btn5 are pressed else if(input_shm[3] == 1 && input_shm[4] == 1){ init_shared(); } // change typing mode, if btn5 & btn6 are pressed else if(input_shm[4] == 1 && input_shm[5] == 1){ typing_mode(); typing_count(2); } // terminate program, if btn8 & btn9 are pressed else if(input_shm[7] == 1 && input_shm[8] == 1) *mode_shm = '0'; // calculate input to print on lcd else{ if(output_shm[0] == 'A'){ // calculation for alphabet mode typing_alphabet(); } else{ // calculation for numeric mode typing_numeric(); } typing_count(1); } // flag down to wait new input input_shm[10] = '*'; } } printf("DEBUG: text editor function ended\n"); return 0; }
// create and initialize shared memory to use static int shared_memory(void){ // naming shared memory segments mode_key = 1111; input_key = 2222; output_key = 3333; // create the segments if((mode_shmid = shmget(mode_key, 1, IPC_CREAT|0600)) < 0) die("mode shmget"); if((input_shmid = shmget(input_key, 32, IPC_CREAT|0660)) < 0) die("input shmget"); if((output_shmid = shmget(output_key, 64, IPC_CREAT|0666)) < 0) die("output shmget"); // attach the segments to memory space if((mode_shm = shmat(mode_shmid, NULL, 0)) == (char *)-1) die("mode shmat"); if((input_shm = shmat(input_shmid, NULL, 0)) == (char *)-1) die("input shmat"); if((output_shm = shmat(output_shmid, NULL, 0)) == (char *)-1) die("output shmat"); // initialize data to default *mode_shm = '1'; init_shared(); return 0; }
int main(int argc, char* argv[], char* envp[]) { int status, child; strcpy(proc_name, "parent"); init_shared(argv); fprintf(stderr, "Map address is: %p\n", g_shared); if (child = fork()) { parentWork(); waitpid(child, &status, 0); cleanup_shared(); fprintf(stderr, "Parent finished!\n"); } else { /* child executes shmem2 */ execvpe("./mult2Vecs", argv + 2, envp); } }
int main(int argc, char **argv) { int fd; init_semaphore(); semSet(); init_shared(); if( (fd = open("debug.txt", O_CREAT | O_TRUNC, 0666)) < 0) { fprintf(stderr, "Cannot create a file\n"); exit(1); } close(fd); return 0; }
void allocOutputMatrixAndSharedMem(int Arows, int Bcols) { int row, col; for (row = 0; row < Arows; row++) for (col = 0; col < Bcols; col++) { cmdArgc[argNumc] = (char *) calloc(1, sizeof(int)); argNumc++; } printf("size of output matrix cmdArgc = %d\n", argNumc * sizeof(cmdArgc[0])); //allocate shared memory strcpy(proc_name, "parent"); init_shared(); fprintf(stderr, "Map address is: %p\n", g_shared); numChildren = Arows * Bcols; printf("number of children processes needed to get output matrix, each multiplying one row of matrix A by one column of matrix B = %d\n", numChildren); }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); pthread_t tid_producer; pthread_t tid_consumer; /* 初始化 shared 类型 */ init_shared(&buf); /* 创建生产者消费者线程 */ pthread_create(&tid_producer, NULL, producer, (void *)&buf); pthread_create(&tid_consumer, NULL, consumer, (void *)&buf); /* 等待线程结束 */ pthread_join(tid_producer, NULL); pthread_join(tid_consumer, NULL); return a.exec(); }
// get all event keys and pass it to main process static int eventkey_process(void){ struct input_event ev[BUFF_SIZE]; int fd, rd, value, size = sizeof(struct input_event); // open device driver if((fd = open("/dev/input/event1", O_RDONLY)) < 0) die("/dev/input/event1 open error"); printf("DEBUG: event key process entered\n"); while(*mode_shm != '0'){ // get event key if((rd = read(fd, ev, size*BUFF_SIZE)) < size) die("read()"); if(ev[0].value == KEY_PRESS){ // stop watch button input if(*mode_shm == '1'){ if(ev[0].code == SW2) *input_shm = '2'; if(ev[0].code == SW3) *input_shm = '3'; if(ev[0].code == SW4) *input_shm = '4'; } // custom mode button input if(*mode_shm == '3'){ if(ev[0].code == SW1) *input_shm = '1'; if(ev[0].code == SW2) *input_shm = '2'; if(ev[0].code == SW3) *input_shm = '3'; if(ev[0].code == SW4) *input_shm = '4'; } // mode change upward if(ev[0].code == SW_UP){ if(*mode_shm == '1') *mode_shm = '2'; else if(*mode_shm == '2') *mode_shm = '3'; else *mode_shm = '1'; init_shared(); } // mode change downward if(ev[0].code == SW_DOWN){ if(*mode_shm == '1') *mode_shm = '3'; else if(*mode_shm == '2') *mode_shm = '1'; else *mode_shm = '2'; init_shared(); } // terminate program if(ev[0].code == SW_QUIT) *mode_shm = '0'; } } // close device driver close(fd); printf("DEBUG: event key process ended\n"); return 0; }
int main (int ac, char **av){ char l[1024]; /* Creates threads as detached in order to save * some resources when quiting */ assert(!pthread_attr_init (&thread_attr)); assert(!pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED)); L = lua_open(); /* opens Lua */ luaL_openlibs(L); /* and it's libraries */ atexit(clean_lua); luaL_openlib(L,"Selene", seleneLib, 0); /* Declare Selene's functions */ init_shared(L); init_SelTimer(L); init_SelCollection(L); init_SelTimedCollection(L); init_SelTimedWindowCollection(L); init_SelFIFO(L); init_log(L); init_SelEvent(L); #ifdef USE_MQTT init_mqtt(L); #endif lua_pushnumber(L, VERSION); /* Expose version to lua side */ lua_setglobal(L, "SELENE_VERSION"); if(ac > 1){ if(ac > 2){ /* Handle script's arguments */ luaL_checkstack(L, ac-1, "too many arguments to script"); /* Place for args (ac-2) + the table itself */ lua_createtable(L, ac-2, 0); for(int i=2; i<ac; i++){ lua_pushstring(L, av[i]); lua_rawseti(L, -2, i-1); } lua_setglobal(L, "arg"); } char *t = strdup( av[1] ); /* Launching script */ assert(t); lua_pushstring(L, dirname(t) ); lua_setglobal(L, "SELENE_SCRIPT_DIR"); strcpy(t, av[1]); lua_pushstring(L, basename(t) ); lua_setglobal(L, "SELENE_SCRIPT_NAME"); int err = luaL_loadfile(L, av[1]) || lua_pcall(L, 0, 0, 0); if(err){ fprintf(stderr, "%s", lua_tostring(L, -1)); lua_pop(L, 1); /* pop error message from the stack */ exit(EXIT_FAILURE); } } else while(fgets(l, sizeof(l), stdin) != NULL){ int err = luaL_loadbuffer(L, l, strlen(l), "line") || lua_pcall(L, 0, 0, 0); if(err){ fprintf(stderr, "%s\n", lua_tostring(L, -1)); lua_pop(L, 1); /* pop error message from the stack */ } } exit(EXIT_SUCCESS); }