Protocol implementation: How to implement one of the simplest communication protocols (threading simulation)

Source: Internet
Author: User

/* *author  : davidlin      *date    :  2014-12-20pm      *email   : [email protected]  or [email protected]      *world   : the  city of SZ, in China      *Ver      : 000.000.001      *for     : threads  for rxtx!   *history :     editor       time            do   &NBSP;&NBSP;&NBSP;&NBSP;*&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;1) LinPeng        2014-12-20      created this file!       *&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;2)       */    #include <stdio.h> #include <stdlib.h> #include <pthread.h>struct msg_packet_t { Char    head;char    address;char    opcode;char     length;char    tail;};/ * who is 9527, i see you say :)  */#define      Is_msg (msg)   (((msg->head)  == 95)  &&  ((msg->tail)  == 27)) # define    is_42   do {         printf ("The answer to the ultimate question of life,"       "the universe and everything is 42\n");     }while (0) #define     init_msg_packet (MSG)    &nbsP {        .head    =  95,         .address =  00,         .opcode  =  01,        .length  =   00,        .tail    =  27, &NBSP;&NBSP;&NBSP;&NBSP,} #define &NBSP;MSG_INFO (msg)     do    {         printf ("\t %s.           \n "   " \t head      :%d\n "   " \ T  address   :%d\n "   " \t opcode    :%d\n "     "\t length    :%d\n"     "\t tail       :%d\n "    ,        __function__,    (struct  msg_packet_t*) msg)->head   ,    ((struct msg_packet_t*) msg) address,    ((struct msg_packet_t*) msg)->opcode ,    ((struct  msg_packet_t*) msg)->length ,    ((struct msg_packet_t*) msg)->tail       );     }while (0) Struct msg_packet_t    g_ Pool  =  init_msg_packet (g_pool);p thread_cond_t          had_tx  =  PTHREAD_COND_INITIALIZER;pthread_mutex_t         lock    =  pthreas_mutex_initializer;char msg_pack ( Char opcode); Char msg_unpack (struct msg_packet *msg); Void phy_tx (Void* buff); VOID&NBSP;PHY_RX (void* bUFF);void *thread_rx  (void* buff) {struct msg_packet_t msg;pthread_mutex_lock (lock); printf ("\n\t wait msg\n");p thread_cond_wait (&had_tx, &lock); Msg_unpack (&msg);   msg_info (&msg);p Thread_mutex_unlock (&lock), Sleep (rand ()%1); void *thread_tx  (Void *buff) {struct msg_packet_t msg;while (1)  {msg.opcode     =  rand ()%43;pthread_mutex_lock (&lock), Msg_pack (msg.opcode);p thread_mutex_ Unlock (&lock);p thread_cond_signal (&AMP;HAD_TX);p rintf (msg tx !\n); Sleep (rand ()%10);}} Int main (int argc, char *argv[]) {pthread_t  tid_tx;pthread_t  tid_rx; Srand (Time (NULL));p thread_create (&tid_tx, null, thread_tx, null);p thread_create (&tid_rx ,  null, thread_rx, null);p thread_join (tid_tx, null);p thread_join (Tid_rx, NULL); exit (0 );} Char msg_unpack (struct msg_packet_t* msg) {Phy_rX (msg); if (!is_msg (msg))  {return;} Switch (msg->opcode) {case 42:    is_42;    break;default:     break;}} Char msg_pack (Char opcode)  {struct msg_packet_t* ptr = &g_pool;ptr-> Opcode = opcode;phy_tx (&g_pool);} Void phy_tx (Void* buff) {g_pool = * (struct msg_packet_t*) buff;//phy tx}void  Phy_rx (Void* buff) {//phy rx* (struct msg_packet_t*) Buff = g_pool;}

Protocol implementation: How to implement one of the simplest communication protocols (threading emulation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.