void WINFD::Arc( /*draw a arc */ float x_radius, /*radii of arc */ float y_radius, /*radii of arc */ float x_center, /*centre of arc */ float y_center, /*centre of arc */ float start, /*ends of arc */ float stop, /*ends of arc */ float rotation, /*rotation of arc */ INT16 close_type /*type of closure */ ) { EIGHTOP *newop; /*message structure */ /*get some space */ newop = (EIGHTOP *) getshm (sizeof (EIGHTOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = ARC; /*send the operator */ /*set parameters */ newop->param.p[0].f = x_radius; newop->param.p[1].f = y_radius; newop->param.p[2].f = x_center; newop->param.p[3].f = y_center; newop->param.p[4].f = start; newop->param.p[5].f = stop; newop->param.p[6].f = rotation; newop->param.p[7].i = close_type; } }
void WINFD::Text2d( /*draw a text */ float x, /*coords of text */ float y, const char *string, /*text to draw */ INT16 xform, /*transform */ INT16 more /*more text? */ ) { TEXTOP *newop; /*message structure */ INT16 length; /*length of string */ length = strlen (string) + 1; /*include null */ length += 3; length &= ~3; /*round up to words */ /*get some space */ newop = (TEXTOP *) getshm (sizeof (TEXTOP) + length - 4); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = TEXT2D; /*send the operator */ newop->param.x = x; /*copy parameters */ newop->param.y = y; newop->param.string = newop->chars; newop->param.xform = xform; newop->param.more = more; /*copy the string */ strcpy (newop->chars, string); } }
int main() { int shmid=getshm(); char* buf=(char*)atshm(shmid); printf("%s\n",buf); dtshm(buf); return 0; }
void WINFD::Clear_view_surface() { /*clear window */ ONEOP *newop; /*message structure */ /*get some space */ newop = (ONEOP *) getshm (sizeof (ONEOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = CLEAR; /*send the operator */ } }
int main() { int mutex=creat_init_semph(0x20,1); //synchronises shared memory between readers(readcount) int writer=creat_init_semph(0x40,1); //synchronises writer and reader(mutual exclusion between writer and reader) int readcount=0; //common varible to be stored in shared memory shmpointer=attach_shmemory(creat_shmemory(shmkey)); //will point to shared memory area //setshm(0); while(1) { printf("waiting for mutex control\n"); wait(mutex); printf("mutex under my control\n"); readcount=getshm()+1; setshm(readcount); //increment readcount by 1 printf("readcount: %d\n",readcount); if(readcount==1) {printf("waiting for writer\n");wait(writer);printf("got writer control\n");} //if first reader then wait for writer printf("writer is %d\n",semctl(writer,0,GETVAL,0)); printf("leaving mutex control..\n"); sleep(10); signal(mutex); printf("reader %d is now reading\n",getpid()); sleep(20); printf("reading over....."); printf("waiting for mutex again\n"); wait(mutex); printf("mutex control again\n"); readcount=getshm()-1; setshm(readcount); printf("readcount now is%d\n",getshm()); if(readcount==0) {printf("releasing writer control//////////////\n");signal(writer);} printf("releasing mutex again\n"); signal(mutex); printf("mutex released\n"); } return 0; }
void WINFD::Line_type( /*set line type */ INT16 style /*style to use */ ) { ONEOP *newop; /*message structure */ /*get some space */ newop = (ONEOP *) getshm (sizeof (ONEOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = LINETYPE; /*send the operator */ newop->param.p.i = style; /*set parameter */ } }
void WINFD::Character_height( /*set text height */ float height /*height to use */ ) { ONEOP *newop; /*message structure */ /*get some space */ newop = (ONEOP *) getshm (sizeof (ONEOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = CHARHEIGHT; /*send the operator */ newop->param.p.f = height; /*set parameter */ } }
void WINFD::Synchronize_windows( /*set line colour */ WINDOW fd2 //other window ) { ONEOP *newop; /*message structure */ /*get some space */ newop = (ONEOP *) getshm (sizeof (ONEOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = SYNCWIN; /*send the operator */ newop->param.p.i = fd2->fd; /*set parameter */ } }
void WINFD::Fill_color_index( /*set fill colour */ COLOUR index /*index to use */ ) { ONEOP *newop; /*message structure */ /*get some space */ newop = (ONEOP *) getshm (sizeof (ONEOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = FILLCOLOUR; /*send the operator */ newop->param.p.i = index; /*set parameter */ } }
void WINFD::Text_font_index( /*set text font */ INT16 index /*index to use */ ) { ONEOP *newop; /*message structure */ /*get some space */ newop = (ONEOP *) getshm (sizeof (ONEOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = TEXTFONT; /*send the operator */ newop->param.p.i = index; /*set parameter */ } }
void WINFD::Marker_type( /*set marker type */ INT16 type /*type to use */ ) { ONEOP *newop; /*message structure */ /*get some space */ newop = (ONEOP *) getshm (sizeof (ONEOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = MARKERTYPE; /*send the operator */ newop->param.p.i = type; /*set parameter */ } }
void WINFD::Marker_size( /*set marker size */ float size /*size to use */ ) { TWOOP *newop; /*message structure */ /*get some space */ newop = (TWOOP *) getshm (sizeof (TWOOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = MARKERSIZE; /*send the operator */ newop->param.p[0].f = size; /*set parameter */ newop->param.p[1].i = FALSE; } }
void WINFD::Interior_style( /*set polygon style */ INT16 style, /*style to use */ INT16 edged /*draw edge or not */ ) { TWOOP *newop; /*message structure */ /*get some space */ newop = (TWOOP *) getshm (sizeof (TWOOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = INTERIORSTYLE; /*send the operator */ newop->param.p[0].i = style; /*set parameter */ newop->param.p[1].i = edged; } }
void WINFD::Draw2d( /*draw the pen */ float x, /*coords to draw to */ float y /*coords to draw to */ ) { TWOOP *newop; /*message structure */ /*get some space */ newop = (TWOOP *) getshm (sizeof (TWOOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = DRAW2D; /*send the operator */ newop->param.p[0].f = x; /*set parameters */ newop->param.p[1].f = y; } }
void WINFD::Make_picture_current() { /*update window */ ONEOP *newop; /*message structure */ if (this == NULL || fd <= 0) { overlap_picture_ops(TRUE); } else { /*get some space */ newop = (ONEOP *) getshm (sizeof (ONEOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = MAKECURRENT; /*send the operator */ kick_daemon(FLUSH_IN); /*empty shm */ } } }
void WINFD::Re_compute_colourmap() { /*Mark need to recalc */ ONEOP *newop; /*message structure */ /*get some space */ newop = (ONEOP *) getshm (sizeof (ONEOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = RE_COMP_COLMAP;/*send the operator */ } /* ONE DAY THE PC VERSION WILL SUPPORT COLOUR - BUT NOT TODAY Among the things that will need doing is to change the size of.. PCSTUBSPEC stubspecs[SYNCWIN+1]; in pcsbdg.[ch] to RE_COMP_COLMAP+1 */ }
void WINFD::Fill_color( /*set fill colour */ UINT8 red, UINT8 green, UINT8 blue) { ONEOP *newop; /*message structure */ UINT32 packed_colour; packed_colour = (blue << 24) + (green << 16) + (red << 8); /*get some space */ newop = (ONEOP *) getshm (sizeof (ONEOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = FILLCOLOUR; /*send the operator */ /*set parameter */ newop->param.p.i = (INT32) packed_colour; } }
void WINFD::Set_echo( /*set window echo */ ECHO_TYPE echo_type, //type of echo float xorig, /*min values */ float yorig /*min values */ ) { FOUROP *newop; /*message structure */ /*get some space */ newop = (FOUROP *) getshm (sizeof (FOUROP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = SETECHO; /*send the operator */ newop->param.p[0].i = echo_type; newop->param.p[1].f = xorig; newop->param.p[2].f = yorig; } }
void WINFD::Destroy_window() { /*destroy a window */ ONEOP *newop; /*destroy structure */ if (fd < 1 || fd > maxsbfd || sbfds[fd].used == FALSE) { return; } else { Clear_event_queue(); sbfds[fd].used = FALSE; /*it is not in use */ sbfds[fd].click_handler = NULL; newop = (ONEOP *) getshm (sizeof (ONEOP)); if (newop != NULL) { newop->header.fd = fd; /*file descriptor */ newop->type = DESTROY; /*set operator type */ } } }
void WINFD::Text_alignment( /*draw a rectangle */ INT32 h_select, //horizontal INT32 v_select, //vertical float horiz, /*coords to draw to */ float vert /*coords to draw to */ ) { FOUROP *newop; /*message structure */ /*get some space */ newop = (FOUROP *) getshm (sizeof (FOUROP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = TEXT_ALIGNMENT;/*send the operator */ newop->param.p[0].i = h_select; newop->param.p[1].i = v_select; newop->param.p[2].f = horiz; newop->param.p[3].f = vert; } }
void WINFD::Rectangle( /*draw a rectangle */ float x1, /*coords to draw to */ float y1, /*coords to draw to */ float x2, /*coords to draw to */ float y2 /*coords to draw to */ ) { FOUROP *newop; /*message structure */ /*get some space */ newop = (FOUROP *) getshm (sizeof (FOUROP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = RECTANGLE; /*send the operator */ newop->param.p[0].f = x1; /*set parameters */ newop->param.p[1].f = y1; newop->param.p[2].f = x2; newop->param.p[3].f = y2; } }
DLLSYM void def_overlap_picture_ops( /*flush output */ BOOL8 update /*send make_ */ ) { ONEOP *newop; /*message structure */ INT16 fd; /*file descriptor */ if (update) { for (fd = 1; fd < maxsbfd; fd++) { if (sbfds[fd].used) { /*get some space */ newop = (ONEOP *) getshm (sizeof (ONEOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ /*send the operator */ newop->type = MAKECURRENT; } } } } kick_daemon(FLUSH_OUT); /*empty shm */ }
void WINFD::Vdc_extent( /*set window focus */ float Xmin, /*min values */ float Ymin, /*min values */ float Xmax, /*max values */ float Ymax /*max values */ ) { EIGHTOP *newop; /*message structure */ /*get some space */ newop = (EIGHTOP *) getshm (sizeof (EIGHTOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = VDCEXTENT; /*send the operator */ newop->param.p[0].f = Xmin; /*set parameters */ newop->param.p[1].f = Ymin; newop->param.p[2].f = 0.0f; newop->param.p[3].f = Xmax; newop->param.p[4].f = Ymax; newop->param.p[5].f = 0.0f; } }
void WINFD::Ellipse( /*draw a ellipse */ float x_radius, /*radii of ellipse */ float y_radius, /*radii of ellipse */ float x_center, /*centre of ellipse */ float y_center, /*centre of ellipse */ float rotation /*rotation of ellipse */ ) { EIGHTOP *newop; /*message structure */ /*get some space */ newop = (EIGHTOP *) getshm (sizeof (EIGHTOP)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = ELLIPSE; /*send the operator */ /*set parameters */ newop->param.p[0].f = x_radius; newop->param.p[1].f = y_radius; newop->param.p[2].f = x_center; newop->param.p[3].f = y_center; newop->param.p[4].f = rotation; } }
void WINFD::Polymarker2d ( /*draw a polymarker */ float clist[], /*coordinate list */ INT16 numpts, /*number of coords */ INT16 flags /*does it have move/draws */ ) { POLYOP *newop; /*message structure */ INT32 floatcount; /*no of floats */ floatcount = flags ? numpts * 3/*move/draw flags in */ : numpts * 2; /*no move/draw flags */ /*get some space */ newop = (POLYOP *) getshm (sizeof (POLYOP) + sizeof (float) * (floatcount - 1)); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = POLYMARKER2D; /*send the operator */ /*pointer to array */ newop->param.clist = newop->clist; newop->param.numpts = numpts;/*other params */ newop->param.flags = flags; memcpy (newop->clist, clist, (UINT32) floatcount * sizeof (float)); } }
void WINFD::Append_text( /*draw a text */ const char *string, /*text to draw */ INT16 xform, /*transform */ INT16 more /*more text? */ ) { APPENDOP *newop; /*message structure */ INT16 length; /*length of string */ length = strlen (string) + 1; /*include null */ length += 3; length &= ~3; /*round up to words */ /*get some space */ newop = (APPENDOP *) getshm (sizeof (APPENDOP) + length - 4); if (newop != NULL) { newop->header.fd = fd; /*send the fd */ newop->type = APPENDTEXT; /*send the operator */ newop->param.string = newop->chars; newop->param.xform = xform; newop->param.more = more; /*copy the string */ strcpy (newop->chars, string); } }
/* * main thread/process (parent) * @param structure with parsed arguments * @return 0 if OK, otherwise some error */ int general(const t_arg *args) { t_err err = ERR_OK; FILE *fw = NULL; pid_t *pid = NULL; /* array with PIDs */ struct st_shmid shmid; /* struct with shared memory segments */ /* get mem for array of PIDs */ if ((pid = calloc((args->n + 1), sizeof(pid_t))) == NULL) { printErr(ERR_OUT_OF_MEM, ""); return ERR_OUT_OF_MEM; } /* get shared memory and save IDs to the structure */ if ((err = getshm(&shmid, args)) != ERR_OK) { free(pid); return err; } /* we use NULL as idicator of stdout */ if (args->file != NULL) { /* open file for writing */ if ((fw = fopen(args->file, "w")) == NULL) { ungetshm(&shmid); free(pid); printErr(ERR_FILE_WRITE, args->file); return ERR_FILE_WRITE; } } /* reset errno */ errno = 0; /* mount shm segment */ struct st_queue *record = (struct st_queue *)shmat(shmid.record, NULL, 0); if (errno) { if (fw != NULL) fclose(fw); shmdt((void *)record); ungetshm(&shmid); free(pid); printErr(ERR_SHM_ATTACH, "shmid.record"); return ERR_SHM_ATTACH; } record->max = args->q; /* chairs in waiting room */ record->pos_st = -1; /* reserved value */ record->pos_ret = -1; /* reserved value */ /* unmount shm segment */ shmdt((void *)record); /* create 1 semaphore with rw rights for the creator */ if ((semid = semget(ftok(args->path, 'z'), 1, IPC_CREAT | IPC_EXCL | 0600)) == -1) { if (fw != NULL) fclose(fw); shmdt((void *)record); ungetshm(&shmid); free(pid); printErr(ERR_SEM_INIT, ""); return ERR_SEM_INIT; } /* semaphore initialization */ unsigned short int semset[1]; /* use 1 semaphore in set */ semset[0] = 1; /* semaphore "index" number (not ID) */ union semuni arg = { .array = semset, }; /* set semaphore options in the whole semset */ semctl(semid, 0, SETALL, arg); /* reset user signals (childs "inherit") */ sigset_t block_mask; /* create new mask */ sigfillset(&block_mask); /* fulfill with every signal */ struct sigaction sigact = { .sa_handler = void_fn, /* do nothing */ .sa_mask = block_mask, .sa_flags = 0, }; sigaction(SIGUSR1, &sigact, NULL); /* apply the new structure */ sigaction(SIGUSR2, &sigact, NULL); /* apply the new structure */ /* generate pseudo-random sequence */ srand(getpid()); int i = 0; int j = args->n; /* give birth to the barber and all customers */ for (i = 0; i <= args->n; i++) { /* sleep for a few miliseconds */ if (i != 0 && args->genc > 0) msleep(rand()%args->genc); /* child */ if ((pid[i] = fork()) == 0) { if (i == 0) process_barber(fw, &shmid, args); else process_customer(fw, &shmid, pid[0], i); } /* something bad happened */ else if (pid[i] == -1) { err = ERR_FORK; for (j = 1; j < i; j++) kill(pid[j], SIGKILL); /* no more interest in the return value */ break; } } /* for(;;) */ /* wait until all customers are gone */ for (i = 1; i <= j; i++) { if (pid[i] != 0) waitpid(pid[i], NULL, 0); } /* close the barbershop forever and kill the barber */ kill(pid[0], SIGKILL); /* wait until barber definitely dies */ waitpid(pid[0], NULL, 0); /* remove semaphore */ semctl(semid, 1, IPC_RMID, arg); /* deallocate shm segments */ ungetshm(&shmid); /* deallocate pid array */ free(pid); /* close file if any */ if (fw != NULL) if (fclose(fw) == EOF) printErr(err = ERR_FILE_CLOSE, args->file); return err; /* exit parent */ } int main(int argc, char *argv[]) { t_err errnum = ERR_OK; /* init the argument structure */ t_arg args = { .q = 0, .genc = 0, .genb = 0, .n = 0, .path = NULL, .file = NULL, /* NULL means output to stdout (instead of file) */ }; /* transform arguments into structure in memory and check them for compatibility */ if ((errnum = parseArgs(&args, argc, argv)) != ERR_OK) return errnum; if ((errnum = general(&args)) != ERR_OK) return errnum; return EXIT_SUCCESS; } // main()
int main(int argc,char *argv[]) { /** 初始化全局共享内存前,先获取ups根路径 **/ if(setupshome()==-1) { printf("设置全局变量upshome错误,请检查UPSHOME环境变量是否设置\n"); return -1; } size_t shmsize; FILE *fp; char tmpbuf[100]; char chnlcfgpath[100]; memset(tmpbuf,0,sizeof(tmpbuf)); memset(chnlcfgpath,0,sizeof(chnlcfgpath)); /** init the sysparam shm cfg **/ shmsize = 3*sizeof(_sys_param); if(getshm(3,shmsize)==-1) { printf("获取系统公用参数共享内存失败\n"); return -1; } shmsize = HASHCNT*BUCKETSCNT*sizeof(_tran); /** init tran shm **/ if(getshm(10,shmsize)==-1) { printf("获取交易hash桶共享内存ID失败\n"); return -1; } /** init commmsg **/ shmsize=MAXCOMMMSG*sizeof(_commmsg); if(getshm(9,shmsize)==-1) { printf("获取渠道间通信共享内存ID失败\n"); return -1; } /** init flow **/ shmsize=MAXFLOW*sizeof(_flow); if(getshm(8,shmsize)==-1) { printf("获取流程配置区共享内存ID失败\n"); return -1; } /** init server reg **/ shmsize = MAXSERVREG*sizeof(_servreg); if(getshm(7,shmsize)==-1) { printf("获取交易系统服务登记共享内存ID失败\n"); return -1; } /** init xml cfg **/ shmsize = MAXXMLCFG*sizeof(_xmlcfg); if(getshm(6,shmsize)==-1) { printf("获取XML配置共享内存失败\n"); return -1; } /** init tran map cfg **/ shmsize = MAXTRANMAP*sizeof(_tranmap); if(getshm(5,shmsize)==-1) { printf("获取交易码映射配置共享内存失败\n"); return -1; } /** init the vardef shm cfg **/ shmsize = MAXVARDEF*sizeof(_vardef); if(getshm(4,shmsize)==-1) { printf("获取变量定义映射配置共享内存失败\n"); return -1; } /** 初始化系统所有渠道的队列区 **/ sprintf(chnlcfgpath,"%s%s",upshome,"/cfg/chnl.cfg"); fp = fopen(chnlcfgpath,"r"); if(fp == NULL) { printf("打开渠道初始化配置文件失败:[%s]\n",strerror(errno)); return -1; } while(fgets(tmpbuf,sizeof(tmpbuf),fp)!=NULL) { tmpbuf[strlen(tmpbuf)-1]='\0'; if(tmpbuf[0]=='*') continue; else if(tmpbuf[0]=='@') { if(initmsg(tmpbuf+1)!=0) { printf("初始化:[%s]渠道队列区失败\n",tmpbuf+1); return -1; } printf("初始化:[%s]渠道队列区成功\n",tmpbuf+1); }else { continue; } } fclose(fp); /** init sem **/ if(initservregsem()!=0) { printf("获取服务登记区信号灯失败\n"); return -1; }else { printf("获取服务登记区信号灯成功\n"); } /** system v sem if(init_sem(1)==0) { SysLog(LOG_SYS_ERR,"init sem ok\n"); }else { SysLog(LOG_SYS_ERR,"init sem error\n"); return -1; } **/ return 0; }
WINDOW WINFD::create( /*create a window */ const char *name, /*name/title of window */ INT8 window_type, /*type of window */ INT16 xpos, /*coords of window */ INT16 ypos, /*coords of window */ INT16 xsize, /*size of window */ INT16 ysize, /*size of window */ float xmin, /*scrolling limits */ float xmax, /*to stop users */ float ymin, /*getting lost in */ float ymax, /*empty space */ BOOL8 downon, /*Events wanted */ BOOL8 moveon, BOOL8 upon, BOOL8 keyon) { INT16 fd; //integer index CREATEOP *newop; /*create structure */ WINDOW win; //output if (xmin == xmax || ymin == ymax) return NO_WINDOW; if (maxsbfd == 0) { maxsbfd = 1; /*don't use 0 */ start_sbdaemon(); /*startup daemon */ } /*find a free one */ for (fd = 1; fd < maxsbfd && sbfds[fd].used; fd++); if (fd == maxsbfd) { /*need a new one */ if (maxsbfd == MAXWINDOWS) return NO_WINDOW; maxsbfd++; } win = &sbfds[fd]; //this win->fd = fd; win->used = TRUE; /*it is in use */ win->downevent = downon; win->moveevent = moveon; win->upevent = upon; win->keyevent = keyon; win->click_handler = NULL; win->selection_handler = NULL; win->key_handler = NULL; win->destroy_handler = NULL; win->events = NULL; win->lastevent = NULL; newop = (CREATEOP *) getshm (sizeof (CREATEOP)); if (newop != NULL) { newop->header.fd = fd; /*file descriptor */ newop->type = CREATE; /*set operator type */ newop->window_type = window_type; /*copy window name */ strncpy (newop->name, name, MAXWINDOWNAME - 1); newop->name[MAXWINDOWNAME - 1] = '\0'; newop->xpos = xpos; newop->ypos = ypos; newop->xsize = xsize; newop->ysize = ysize; newop->xmin = xmin; newop->xmax = xmax; newop->ymin = ymin; newop->ymax = ymax; newop->downon = downon; newop->moveon = moveon; newop->upon = upon; newop->keyon = keyon; } return win; /*file descriptor */ }