/Author: DriverMonkey
// Mobile: 13410905075
// Mail: bookworepeng@Hotmail.com
// Qq: 196568501
# Include <pthread. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/ipc. h>
# Include <sys/msg. h>
# Include <string. h>
# Include <iostream>
# Define MAX_SEND_SIZE 80
# Define RETURN_MSG_TYPE 0XAA
# Define SEND_MSG_TYPE 0X55
Using namespace std;
Struct mymsgbuf {
Long mtype;
Char mtext [MAX_SEND_SIZE];
};
Static void * thread_GUI (void * arg );
Static void * thread_logic (void * arg );
Static int init_message (unsigned char key );
Static void send_message (int qid,
Struct mymsgbuf * qbuf,
Long type,
Const void * text,
Int size );
Static int read_message (int qid, struct mymsgbuf * qbuf, long type );
Static void remove_queue (int qid );
Static int message_id = 0;
Int main ()
{
Pthread_t thread_GUI_id = 0;
Pthread_t thread_logic_id = 0;
Message_id = init_message ('G ');
Pthread_create (& thread_GUI_id, NULL, & thread_GUI, NULL );
Pthread_create (& thread_logic_id, NULL, & thread_logic, NULL );
Pthread_join (thread_GUI_id, NULL );
Pthread_join (thread_logic_id, NULL );
Return 0;
}
Static void * thread_GUI (void * arg)
{
Int sleep_count = 0;
Mymsgbuf send_buf;
Sleep_count = 10;
Char send_v = 0;
While (sleep_count --)
{
Send_v ++;
Send_message (message_id, & send_buf, SEND_MSG_TYPE, & send_v, sizeof (send_v ));
// Cout <"thead_GUI: sleep_count =" <sleep_count <endl;
// Sleep (1 );
}
}
Static void * thread_logic (void * arg)
{
Int sleep_count = 0;
Mymsgbuf recive_buf;
Sleep_count = 10;
While (sleep_count --)
{
// Cout <"thread_logic: sleep_count =" <sleep_count <endl;
Read_message (message_id, & recive_buf, send_msg_type );
// Sleep (1 );
}
}
Int init_message (unsigned char key)
{
Int id = 0;
Key = ftok (".", key );
Id = msgget (Key, ipc_creat | 0777 );
If (ID = (-1 ))
While (1); // shoshould never in
Return ID;
}
Void send_message (INT qid,
Struct mymsgbuf * qbuf,
Long type,
Const void * text,
Int size)
{
Qbuf-> mtype = type;
Memcpy (qbuf-> mtext, text, size );
Cout <"send =" <(INT) qbuf-> mtext [0] <Endl;
If (msgsnd (qid, (struct msgbuf *) qbuf, size, null) =-1 ))
While (1); // shoud never in
Qbuf-> mtype = type;
Msgrcv (qid, (struct msgbuf *) qbuf, max_send_size, return_msg_type, 0 );
Cout <"Send return =" <(INT) qbuf-> mtext [0] <Endl;
Cout <qbuf-> mtext <Endl;
}
Int read_message (INT qid, struct mymsgbuf * qbuf, long type)
{
Int read_size = 0;
Static int temp = 100;
Qbuf-> mtype = type;
Temp ++;
Read_size = msgrcv (qid, (struct msgbuf *) qbuf, max_send_size, type, 0 );
Cout <"read =" <(INT) qbuf-> mtext [0] <Endl;
Char const * return_message = "message_ OK ";
Strcpy (qbuf-> mtext, return_message );
Qbuf-> mtext [0] = temp ++;
Qbuf-> mtype = RETURN_MSG_TYPE;
Msgsnd (qid, (struct msgbuf *) qbuf, strlen (return_message) + 1, NULL );
Cout <"read return =" <(int) qbuf-> mtext [0] <endl;
}
Void remove_queue (int qid)
{
}