forked from selfboot/CS_Offer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
760 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* @Author: [email protected] | ||
* @Last Modified time: 2016-08-31 21:35:25 | ||
*/ | ||
|
||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <errno.h> | ||
#include <sys/msg.h> | ||
|
||
struct msg_st | ||
{ | ||
long int msg_type; | ||
char text[BUFSIZ]; | ||
}; | ||
|
||
int main() | ||
{ | ||
int running = 1; | ||
int msgid = -1; | ||
struct msg_st data; | ||
long int msgtype = 2; | ||
|
||
//建立消息队列 | ||
msgid = msgget((key_t)1234, 0666 | IPC_CREAT); | ||
if(msgid == -1) | ||
{ | ||
fprintf(stderr, "Msgget failed with error: %d\n", errno); | ||
exit(EXIT_FAILURE); | ||
} | ||
//从队列中获取消息,直到遇到end消息为止 | ||
while(running) | ||
{ | ||
if(msgrcv(msgid, (void*)&data, BUFSIZ, msgtype, 0) == -1) | ||
{ | ||
fprintf(stderr, "Msgrcv failed with errno: %d\n", errno); | ||
exit(EXIT_FAILURE); | ||
} | ||
printf("You wrote: %s\n",data.text); | ||
//遇到end结束 | ||
if(strncmp(data.text, "end", 3) == 0) | ||
running = 0; | ||
} | ||
//删除消息队列 | ||
if(msgctl(msgid, IPC_RMID, 0) == -1) | ||
{ | ||
fprintf(stderr, "Msgctl(IPC_RMID) failed\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
exit(EXIT_SUCCESS); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* @Author: [email protected] | ||
* @Last Modified time: 2016-08-31 21:34:31 | ||
*/ | ||
|
||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <sys/msg.h> | ||
#include <errno.h> | ||
|
||
#define MAX_TEXT 512 | ||
struct msg_st | ||
{ | ||
long int msg_type; | ||
char text[MAX_TEXT]; | ||
}; | ||
|
||
int main() | ||
{ | ||
int running = 1; | ||
struct msg_st data; | ||
char buffer[BUFSIZ]; | ||
int msgid = -1; | ||
|
||
//建立消息队列 | ||
msgid = msgget((key_t)1234, 0666 | IPC_CREAT); | ||
if(msgid == -1) | ||
{ | ||
fprintf(stderr, "Msgget failed with error: %d\n", errno); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
//向消息队列中写消息,直到写入end | ||
while(running) | ||
{ | ||
//输入数据 | ||
printf("Enter some text: "); | ||
fgets(buffer, BUFSIZ, stdin); | ||
// 消息类型 | ||
data.msg_type = 1; | ||
strcpy(data.text, buffer); | ||
//向队列发送数据 | ||
if(msgsnd(msgid, (void*)&data, MAX_TEXT, 0) == -1) | ||
{ | ||
fprintf(stderr, "Msgsnd failed\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
//输入end结束输入 | ||
if(strncmp(buffer, "end", 3) == 0) | ||
running = 0; | ||
sleep(1); | ||
} | ||
exit(EXIT_SUCCESS); | ||
} |
Oops, something went wrong.