int main(int n_args, char * ll_args[]) { int i,n,t,t_total,n_proc,n_veg; int *p_lletres,id_lletres; int id_win,n_fil,n_col,mida_camp; char a1[20],a3[20],a4[20],a5[10],a6[10], fmis[80]; void *p_win; if (n_args != 4) { fprintf(stderr,"comanda: multiproc2 num_procs n_vegades n_lletres\n"); exit(1); } n_proc = atoi(ll_args[1]); /* convertir arguments a num. enter */ n_veg = atoi(ll_args[2]); if (n_proc < 1) n_proc = 1; /* i filtrar el seu valor */ if (n_proc > MAX_PROCS) n_proc = MAX_PROCS; if (n_veg < 1) n_veg = 1; if (n_veg > MAX_VEGADES) n_veg = MAX_VEGADES; id_lletres = ini_mem(sizeof(int)); /* crear zona mem. compartida */ p_lletres = map_mem(id_lletres); /* obtenir adres. de mem. compartida */ *p_lletres = atoi(ll_args[3]); /* inicialitza variable compartida */ if (*p_lletres < 1) *p_lletres = 1; if (*p_lletres > n_proc*n_veg) *p_lletres = n_proc*n_veg; sprintf(a3,"%i",id_lletres); /* convertir identificador mem. en string */ n_fil = n_proc+3; n_col = n_veg+2; mida_camp = win_ini(&n_fil,&n_col,'+',INVERS); if (mida_camp < 0) { /* si no pot crear l'entorn de joc amb les curses */ switch (mida_camp) { case -1: fprintf(stderr,"camp de joc ja creat!\n"); break; case -2: fprintf(stderr,"no s'ha pogut inicialitzar l'entorn de curses!\n"); break; case -3: fprintf(stderr,"les mides del camp demanades son massa grans!\n"); break; case -4: fprintf(stderr,"no s'ha pogut crear la finestra!\n"); break; } elim_mem(id_lletres); /* elimina zona de memoria compartida */ exit(2); } id_win = ini_mem(mida_camp); /* crear zona mem. compartida */ p_win = map_mem(id_win); /* obtenir adres. de mem. compartida */ sprintf(a4,"%i",id_win); sprintf(a5,"%i",n_fil); /* convertir mides camp en string */ sprintf(a6,"%i",n_col); win_set(p_win,n_fil,n_col); /* crea acces a finestra oberta */ n = 0; for ( i = 0; i < n_proc; i++) { tpid[n] = fork(); /* crea un nou proces */ if (tpid[n] == (pid_t) 0) /* branca del fill */ { sprintf(a1,"%i",(i+1)); execlp("./mp_car2", "mp_car2", a1, ll_args[2], a3, a4, a5, a6, (char *)0); fprintf(stderr,"error: no puc executar el process fill \'mp_car2\'\n"); exit(0); } else if (tpid[n] > 0) n++; /* branca del pare */ } /* fprintf(stderr,"Pare: he creat %d processos fill, espero que acabin!\n\n",n);*/ do { sprintf(fmis,"lletres = %d",*p_lletres); win_escristr(fmis); win_update(); /* actualitza visualitzacio CURSES */ win_retard(20); } while (*p_lletres != 0); t_total = 0; for (i = 0; i < n; i++) { waitpid(tpid[i],&t,0); /* espera finalitzacio d'un fill */ t = t >> 8; /* normalitza resultat */ /* fprintf(stderr,"el proces (%d) ha escrit %d lletres\n",tpid[i],t); */ t_total += t; } win_fi(); printf("\nJa han acabat tots els processos creats!\n"); printf("Entre tots els processos han escrit %d lletres.\n",t_total); elim_mem(id_win); /* elimina zones de memoria compartida */ elim_mem(id_lletres); return(0); }
main() { register char c; register int fd, lgt; char buf[512+1]; ini_mem(); if( ini_screen() < 0 ) exit( 0 ); dpo( увт ); if( (fd=open("/usr/minihelp/dtest",0)) > 0 ) { while( read(fd,buf,512) > 0 ) {buf[lgt]=0; dpc( buf );} close( fd ); } state = 0; radr=0177776; wadr=0177776; dpp( xr, yr-1 ); dpc( " READ" ); dpp( xr, yr ); dpo( '(' ); dpc( conv( radr, 6, 8, '0' ) ); dpc( ")=" ); dpc( conv( get(radr), 6, 8, '0' ) ); dpp( xw, yw-1 ); dpc( " WRITE" ); dpp( xw, yw ); dpo( '(' ); dpc( conv( wadr, 6, 8, '0' ) ); dpc( ")=" ); dpc( conv( wvalue, 6, 8, '0' ) ); dpp( x=xr+1, y=yr ); for(;;) { switch( state ) { case 0: c = getnumb( &radr, x-xr-1, 0177777, 0160000 ); break; case 1: c = getnumb( &wadr, x-xw-1, 0177777, 0160000 ); break; case 2: c = getnumb(&wvalue,x-xw-9, 0177777, 0000000 ); break; } rep: switch( c ) { case CTRL(D): end_screen(); exit(0); case 'r': case 'R': state=0; x=xr+1; dpp(x,y); break; case 'w': case 'W': state=1; x=xw+1; dpp(x,y); break; case 'v': case 'V': state=2; x=xw+9; dpp(x,y); break; case 'c': case 'C': if( state ) { wcycle = !wcycle; dpp(xw+8,yw-1); dpc( wcycle ? "(CYCLE)" : " " ); } else { rcycle = !rcycle; dpp(xr+8,yr-1); dpc( rcycle ? "(CYCLE)" : " " ); } dpp( x, y ); break; case CR: switch( state ) { case 0: rvalue = get( radr ); dpp( xr+9, yr ); dpc( conv(rvalue,6,8,'0') ); c='r'; goto rep; case 1: c='v'; goto rep; case 2: set( wadr, wvalue ); c='v'; goto rep; } } } }