1 #include <stdio.h> 2 #include <pthread.h> 3 #include <semaphore.h> 4 5 typedef unsigned char uint8; 6 typedef unsigned short unit16; 7 typedef unsigned int uint32; 8 9 #define QUEUE_SIZE 1000 10 11 typedef struct Msg_Hdr_s 12 { 13 uint32 msg_type; 14 uint32 msg_len; 15 uint32 msg_src; 16 uint32 msg_dst; 17 }Msg_Hdr_t; 18 19 typedef struct Msg_s 20 { 21 Msg_Hdr_t hdr; 22 uint8 data[100]; 23 } Msg_t; 24 25 typedef struct Queue_s 26 { 27 int head; 28 int rear; 29 sem_t sem; 30 Msg_t data[QUEUE_SIZE]; 31 }Queue_t; 32 33 typedef struct Queue_s QueueNode; 34 35 enum MSG_TYPE 36 { 37 GO_HOME, 38 GO_TO_BED, 39 GO_TO_LUNCH, 40 GO_TO_CINAMA, 41 GO_TO_SCHOOL, 42 GO_DATEING, 43 GO_TO_WORK,//6 44 }; 45 46 enum SRC_ADDR 47 { 48 THREAD1, 49 THREAD2, 50 THREAD3, 51 HANDLER, 52 }; 53 54 55 int MsgQueueInit(Queue_t* Q); 56 int MsgDeQueue(Queue_t* Q, Msg_t* msg); 57 int MsgEnQueue(Queue_t* Q, Msg_t* msg); 58 void msg_handler(); 59 void msg_sender1(); 60 void msg_sender2(); 61 void msg_sender3(); 62 void msg_printer(Msg_t* msg); 63 int msg_send();
看看跑起来的现象:
Finish!