晚上在FC4下調(diào)試出了第一個(gè)linux程序,成功地運(yùn)行,有點(diǎn)成就感,雖然這個(gè)程序是抄過來(lái).
本程序的功能是利用系統(tǒng)V(Sys V)消息隊(duì)列來(lái)實(shí)現(xiàn)進(jìn)程間的通信(IPC).
簡(jiǎn)單地介紹程序:
用到的一個(gè)數(shù)據(jù)結(jié)構(gòu)-------消息,用一個(gè)結(jié)構(gòu)體來(lái)表示:
struct msgbuf
{
long
mtype;
//
消息的類型
char
mtext[
1
];
//
消息的內(nèi)容,并不是表示只存一個(gè)字符
}
;
幾個(gè)API:
key_t ftok (char*pathname, char proj);
它返回與路徑pathname相對(duì)應(yīng)的一個(gè)鍵值。該函數(shù)不直接對(duì)消息隊(duì)列操作,但在調(diào)用msgget()來(lái)獲得消息隊(duì)列描述字前,往往要調(diào)用該函數(shù)。典型用法 key=ftok('.', 'a');
int msgget(key_t key, int msgflg)
參數(shù)key是一個(gè)鍵值,由ftok獲得;msgflg參數(shù)是一些標(biāo)志位。該調(diào)用返回與健值key相對(duì)應(yīng)的消息隊(duì)列描述字。
在以下兩種情況下,該調(diào)用將創(chuàng)建一個(gè)新的消息隊(duì)列:
- 如果沒有消息隊(duì)列與健值key相對(duì)應(yīng),并且msgflg中包含了IPC_CREAT標(biāo)志位;
- key參數(shù)為IPC_PRIVATE;
參數(shù)msgflg可以為以下:IPC_CREAT、IPC_EXCL、IPC_NOWAIT或三者的或結(jié)果。
調(diào)用返回:成功返回消息隊(duì)列描述字,否則返回-1。
int msgrcv(int msqid, struct msgbuf *msgp, int msgsz, long msgtyp, int msgflg);
該系統(tǒng)調(diào)用從msgid代表的消息隊(duì)列中讀取一個(gè)消息,并把消息存儲(chǔ)在msgp指向的msgbuf結(jié)構(gòu)中。
msqid為消息隊(duì)列描述字;消息返回后存儲(chǔ)在msgp指向的地址,msgsz指定msgbuf的mtext成員的長(zhǎng)度(即消息內(nèi)容的長(zhǎng)度),msgtyp為請(qǐng)求讀取的消息類型;讀消息標(biāo)志msgflg可以為以下幾個(gè)常值的或:
- IPC_NOWAIT 如果沒有滿足條件的消息,調(diào)用立即返回,此時(shí),errno=ENOMSG
- IPC_EXCEPT 與msgtyp>0配合使用,返回隊(duì)列中第一個(gè)類型不為msgtyp的消息
- IPC_NOERROR 如果隊(duì)列中滿足條件的消息內(nèi)容大于所請(qǐng)求的msgsz字節(jié),則把該消息截?cái)啵財(cái)嗖糠謱G失。
msgrcv手冊(cè)中詳細(xì)給出了消息類型取不同值時(shí)(>0; <0; =0),調(diào)用將返回消息隊(duì)列中的哪個(gè)消息。
msgrcv()解除阻塞的條件有三個(gè):
- 消息隊(duì)列中有了滿足條件的消息;
- msqid代表的消息隊(duì)列被刪除;
- 調(diào)用msgrcv()的進(jìn)程被信號(hào)中斷;
調(diào)用返回:成功返回讀出消息的實(shí)際字節(jié)數(shù),否則返回-1。
int msgsnd(int msqid, struct msgbuf *msgp, int msgsz, int msgflg);
向msgid代表的消息隊(duì)列發(fā)送一個(gè)消息,即將發(fā)送的消息存儲(chǔ)在msgp指向的msgbuf結(jié)構(gòu)中,消息的大小由msgze指定。
對(duì)發(fā)送消息來(lái)說(shuō),有意義的msgflg標(biāo)志為IPC_NOWAIT,指明在消息隊(duì)列沒有足夠空間容納要發(fā)送的消息時(shí),msgsnd是否等待。造成msgsnd()等待的條件有兩種:
- 當(dāng)前消息的大小與當(dāng)前消息隊(duì)列中的字節(jié)數(shù)之和超過了消息隊(duì)列的總?cè)萘浚?
- 當(dāng)前消息隊(duì)列的消息數(shù)(單位"個(gè)")不小于消息隊(duì)列的總?cè)萘浚▎挝?字節(jié)數(shù)"),此時(shí),雖然消息隊(duì)列中的消息數(shù)目很多,但基本上都只有一個(gè)字節(jié)。
msgsnd()解除阻塞的條件有三個(gè):
- 不滿足上述兩個(gè)條件,即消息隊(duì)列中有容納該消息的空間;
- msqid代表的消息隊(duì)列被刪除;
- 調(diào)用msgsnd()的進(jìn)程被信號(hào)中斷;
調(diào)用返回:成功返回0,否則返回-1。
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
該系統(tǒng)調(diào)用對(duì)由msqid標(biāo)識(shí)的消息隊(duì)列執(zhí)行cmd操作,共有三種cmd操作:IPC_STAT、IPC_SET 、IPC_RMID。
- IPC_STAT:該命令用來(lái)獲取消息隊(duì)列信息,返回的信息存貯在buf指向的msqid結(jié)構(gòu)中;
- IPC_SET:該命令用來(lái)設(shè)置消息隊(duì)列的屬性,要設(shè)置的屬性存儲(chǔ)在buf指向的msqid結(jié)構(gòu)中;可設(shè)置屬性包括:msg_perm.uid、msg_perm.gid、msg_perm.mode以及msg_qbytes,同時(shí),也影響msg_ctime成員。
- IPC_RMID:刪除msqid標(biāo)識(shí)的消息隊(duì)列;
調(diào)用返回:成功返回0,否則返回-1。
#include
<
stdio.h
>
#include
<
stdlib.h
>
#include
<
ctype.h
>
#include
<
sys
/
ipc.h
>
#include
<
sys
/
types.h
>
#include
<
sys
/
msg.h
>
#define
MAX_SEND_SIZE 80
struct
mymsgbuf
{
long
mtype;
char
mtext[MAX_SEND_SIZE];
}
;
void
send_message(
int
qid,
struct
mymsgbuf
*
qbuf,
long
type,
char
*
text);
void
read_message(
int
qid,
struct
mymsgbuf
*
qbuf,
long
type);
void
remove_queue(
int
qid);
void
change_queue_mode(
int
qid,
char
*
mode);
void
usage(
void
);

int
main(
int
argc,
char
*
argv[])

{
key_t key;
int
msgqueue_id;
struct
mymsgbuf qbuf;
if
(
1
==
argc)
usage();
key
=
ftok(
"
.
"
,
'
m
'
);

if
((msgqueue_id
=
msgget(key,IPC_CREAT
|
0660
))
==-
1
)
{
perror(
"
msgget
"
);
exit(
1
);
}
switch
(tolower(argv[
1
][
0
]))

{
case
'
s
'
:send_message(msgqueue_id,(
struct
mymsgbuf
*
)
&
qbuf,
atol(argv[
2
]),argv[
3
]);
break
;
case
'
r
'
:read_message(msgqueue_id,
&
qbuf,atol(argv[
2
]));
break
;
case
'
d
'
:remove_queue(msgqueue_id);
break
;
case
'
m
'
:change_queue_mode(msgqueue_id,argv[
2
]);
break
;
default
:usage();
}
return
0
;
}
void
send_message(
int
qid,
struct
mymsgbuf
*
qbuf,
long
type,
char
*
text)

{
printf(
"
send a message
\n
"
);
qbuf
->
mtype
=
type;
strcpy(qbuf
->
mtext,text);

if
((msgsnd(qid,(
struct
msgbuf
*
)qbuf,strlen(qbuf
->
mtext)
+
1
,
0
))
==-
1
)
{
perror(
"
msgsnd
"
);
exit(
1
);
}
}
void
read_message(
int
qid,
struct
mymsgbuf
*
qbuf,
long
type)

{
printf(
"
reading a message
.\n
"
);
qbuf
->
mtype
=
type;
msgrcv(qid,(
struct
msgbuf
*
)qbuf,MAX_SEND_SIZE,type,
0
);
printf(
"
Type: %1d Text: %s\n
"
,qbuf
->
mtype,qbuf
->
mtext);
}
void
remove_queue(
int
qid)

{
msgctl(qid,IPC_RMID,
0
);
}
void
change_queue_mode(
int
qid,
char
*
mode)

{
struct
msqid_ds myqueue_ds;
msgctl(qid,IPC_STAT,
&
myqueue_ds);
sscanf(mode,
"
%ho
"
,
&
myqueue_ds.msg_perm.mode);
msgctl(qid,IPC_SET,
&
myqueue_ds);
}
void
usage(
void
)

{
fprintf(stderr,
"
msgtool -A utility for tinkering with msg queue\n
"
);
fprintf(stderr,
"
\nUSAGE:msgtool (s)end <type> <messagetext>\n
"
);
fprintf(stderr,
"
(r)ecv<type>\n
"
);
fprintf(stderr,
"
(d)elete\n
"
);
fprintf(stderr,
"
(m)ode<octal mode>\n
"
);
exit(
1
);
}
以上程序編譯能通過,只是出現(xiàn)了兩個(gè)warning
warning:內(nèi)建函數(shù)strcpy不兼容的隱式聲明
warning:內(nèi)建函數(shù)strlen不兼容的隱式聲明
不知是什么原因?是不是gcc版本問題?
還有在寫Makefile后,make時(shí)出現(xiàn)一個(gè)錯(cuò)誤提示:
commands commence before first target
解決方法:去掉target的空格(tab鍵)
程序來(lái)是http://tldp.org/LDP/lpg/node45.html#SECTION00742640000000000000
API解釋來(lái)自http://www.52wg.org/computer/chengxu/200511/computer_98386.html