#include <stdio.h>
#include <stdlib.h>
#include <="" span="">ipc.h>
#include <="" span="">types.h>
#include <unistd.h>
int main(){
struct msgbuf{
long
mtype;
char
mtext
[256];
};
char
line [256];
int msqid;
key_t msgkey;
struct msgbuf msgdata, *p;
p=&msgdata;
printf
(“Enter message: “);
fflush (stdout);
fgets
(line, sizeof
(line), stdin);
if
(sscanf
(line, “%[^\n]”,
p->mtext)
>0){
// プロセスIDをmtypeに入れる
p->mtype=getpid ();
msgkey=ftok (“mrecv”,
‘a’);
// メッセージキュー識別子を得る
msqid=msgget (msgkey,
IPC_CREAT | 0666);
// メッセージを送信する
msgsnd (msqid, p, sizeof
(p->mtext),
0);
}
return
EXIT_SUCCESS;
}
[mrecv.c]
#include <stdio.h>
#include <stdlib.h>
#include <="" span="">ipc.h>
#include <="" span="">msg.h>
#include <="" span="">types.h>
int main(){
struct msgbuf{
long
mtype;
char
mtext
[256];
};
int msqid;
key_t msgkey;
struct msgbuf msgdata, *p;
p=&msgdata;
msgkey=ftok (“mrecv”,
‘a’);
// メッセージキュー識別子を得る
msqid=msgget (msgkey,
IPC_CREAT | 0666);
while
(1){ //無限ループ
// メッセージを受信する
msgrcv (msqid, p, sizeof
(p->mtext), 0,
0);
printf
(“message received from %ld\n%s\n”
,p->mtype,
p->mtext);
}
return
EXIT_SUCCESS;
}
atmark@atde3:~/Desktop/work$ gcc -o mrecv mrecv.c
atmark@atde3:~/Desktop/work$ gcc -o msend msend.c
atmark@atde3:~/Desktop/work$ ./msend
Enter message: Hello
atmark@atde3:~/Desktop/work$ ./msend
Enter message: Good
atmark@atde3:~/Desktop/work$ ./mrecv
message received from 10658
Hello
message received from 10659
Good
0 件のコメント:
コメントを投稿