void * philosopher (void *num) { int id; int i, left_chopstick, right_chopstick, f; id = (intptr_t)num; printf ("Philosopher %d is done thinking and now ready to eat.\n", id); right_chopstick = id; left_chopstick = id + 1; /* 箸が一巡した */ if (left_chopstick == PHILOS) left_chopstick = 0; while (f = food_on_table ()) { get_token (); grab_chopstick (id, right_chopstick, "right "); grab_chopstick (id, left_chopstick, "left"); printf ("Philosopher %d: eating.\n", id); usleep (DELAY * (FOOD - f + 1)); down_chopsticks (left_chopstick, right_chopstick); return_token (); } printf ("Philosopher %d is done eating.\n", id); return (NULL); }
/************************************************************************* * *N get_token * *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Purpose: *P * This function gets the first token, token type, and token value of * the expression string, and then advances the expression string * past the token. *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Parameters: *A * expression <input>==(char *) selection expression string. * token <output>==(char *) first token in the string. * token_type <output>==(int *) token type. * token_value <output>==(int *) token_value. * return <output>==(char *) new selection expression. *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * History: *H * Barry Michaels May 1991 DOS Turbo C *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * External Variables: *X * None *E *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Functions Called: *F * static void return_token( char *expr, char *token ) VPFQUERY.C *E *************************************************************************/ static char *get_token( char *expression, char *token, int *token_type, int *token_value ) { register int i, stopflag; *token_type = 0; if (*expression == '\0') { *token_type = FINISHED; *token_value = 0; return expression; } if (*expression=='\r') { /* crlf */ ++expression; ++expression; *token = '\r'; token[1] = '\n'; token[2] = 0; *token_type = DELIMETER; } stopflag = 0; while ((expression[0] == '\"') || (expression[0] == ' ')) { for (i=0;i<ndelim;i++) if (rspf_strncasecmp(expression,delimstr[i],(unsigned int)strlen(delimstr[i]))==0) { stopflag=1; break; } if (stopflag) break; expression++; } return_token( expression, token ); expression += strlen(token); if (*token == '\0') { *token_type = FINISHED; *expression = '\0'; return expression; } leftjust(token); rightjust(token); if (rspf_strcasecmp(token,"AND")==0) { strupr(token); *token_type = JOIN; *token_value = AND; while ((expression[0] == '\"') || (expression[0] == ' ')) expression++; return expression; } if (rspf_strcasecmp(token,"OR")==0) { strupr(token); *token_type = JOIN; *token_value = OR; while ((expression[0] == '\"') || (expression[0] == ' ')) expression++; return expression; } if (token[0] == '"') { /* quoted string */ if (*expression) expression++; i = 0; while (*expression != '"') { token[i] = *expression; i++; expression++; if (*expression == '\0') { *token_type = ERROR; *token_value = ERROR; return expression; } } while ((expression[0] == '\"') || (expression[0] == ' ')) expression++; token[i] = '\0'; *token_type = STRING; *token_value = (int)strlen(token); return expression; } for (i=0;i<ndelim;i++) { if (rspf_strcasecmp(token,delimstr[i])==0) { *token_type = LOP; *token_value = i; return expression; } } for (i=0;i<nfields;i++) { if (rspf_strcasecmp(token,fieldname[i])==0) { strupr(token); *token_type = FIELD; *token_value = i; return expression; } } *token_type = VALUE; *token_value = 0; return expression; }
/* * sniffer_next * * Fill a structure with a copy of the next packet and its metadata. * Return number of packets read. * */ static int sniffer_next(source_t * src, void *out_buf, size_t out_buf_size) { struct _snifferinfo * info = (struct _snifferinfo *) src->ptr; uint npkts; /* processed pkts */ uint out_buf_used; /* bytes in output buffer */ npkts = out_buf_used = 0; mb(); while (info->m->k2u_cons != info->m->k2u_prod) { uint ind; uint token; ushort iface; struct timeval tv; int len, pktofs; pkt_t *pkt; char * base; ind = info->m->k2u_cons % RING_SIZE; token = info->m->k2u_pipe[ind].token; iface = info->m->k2u_pipe[ind].interface; if (iface == (ushort)-1) { /* Kernel decided not to use this token. Return it. */ return_token(info->m, token); info->m->k2u_cons++; continue; } if (iface >= info->retimer_size || info->clock_retimers[iface] == NULL){ /* Clock calibration was incomplete. Uh oh. */ return_token(info->m, token); info->m->k2u_cons++; continue; } /* we have a good incoming packet; deal with it. */ base = info->packet_pool[token].payload; len = info->m->k2u_pipe[ind].len; getTime(info->clock_retimers[iface], info->m->k2u_pipe[ind].tstamp, &tv, NULL); /* check if we have enough space in output buffer */ if (sizeof(pkt_t) + len > out_buf_size - out_buf_used) break; /* * Now we have a packet: start filling a new pkt_t struct * (beware that it could be discarded later on) */ pkt = (pkt_t *) ((char *)out_buf + out_buf_used); pkt->ts = TIME2TS(tv.tv_sec, tv.tv_usec); pkt->len = len; pkt->type = COMO_L2_ETH; pkt->flags = 0; pkt->caplen = 0; /* NOTE: we update caplen as we go given * that we may not store all fields that * exists in the actual bpf packet (e.g., * IP options) */ bcopy(base, &pkt->layer2.eth, 14); if (H16(pkt->layer2.eth.type) != 0x0800) { /* * this is not an IP packet. discard and * go to next packet. */ logmsg(LOGSNIFFER, "non-IP packet received (%04x)\n", H16(pkt->layer2.eth.type)); continue; } pktofs = 14; base += 14; /* copy IP header */ pkt->ih = *(struct _como_iphdr *) base; pkt->caplen += sizeof(struct _como_iphdr); /* skip the IP header * * XXX we are losing IP options if any in the packets. * need to find a place to put them in the como packet * data structure... */ base += (IP(vhl) & 0x0f) << 2; pktofs += (IP(vhl) & 0x0f) << 2; /* copy layer 4 header and payload */ bcopy(base, &pkt->layer4, len - pktofs); pkt->caplen += (len - pktofs); /* done with this packet. return the token */ info->m->k2u_cons++; return_token(info->m, token); /* increment the number of processed packets */ npkts++; out_buf_used += STDPKT_LEN(pkt); } return npkts; }