晚上在FC4下調試出了第一個linux程序,成功地運行,有點成就感,雖然這個程序是抄過來.
本程序的功能是利用系統V(Sys V)消息隊列來實現進程間的通信(IPC).
簡單地介紹程序:
用到的一個數據結構-------消息,用一個結構體來表示:
struct msgbuf
{
long
mtype;
//
消息的類型
char
mtext[
1
];
//
消息的內容,并不是表示只存一個字符
}
;
幾個API:
key_t ftok (char*pathname, char proj);
它返回與路徑pathname相對應的一個鍵值。該函數不直接對消息隊列操作,但在調用msgget()來獲得消息隊列描述字前,往往要調用該函數。典型用法 key=ftok('.', 'a');
int msgget(key_t key, int msgflg)
參數key是一個鍵值,由ftok獲得;msgflg參數是一些標志位。該調用返回與健值key相對應的消息隊列描述字。
在以下兩種情況下,該調用將創建一個新的消息隊列:
- 如果沒有消息隊列與健值key相對應,并且msgflg中包含了IPC_CREAT標志位;
- key參數為IPC_PRIVATE;
參數msgflg可以為以下:IPC_CREAT、IPC_EXCL、IPC_NOWAIT或三者的或結果。
調用返回:成功返回消息隊列描述字,否則返回-1。
int msgrcv(int msqid, struct msgbuf *msgp, int msgsz, long msgtyp, int msgflg);
該系統調用從msgid代表的消息隊列中讀取一個消息,并把消息存儲在msgp指向的msgbuf結構中。
msqid為消息隊列描述字;消息返回后存儲在msgp指向的地址,msgsz指定msgbuf的mtext成員的長度(即消息內容的長度),msgtyp為請求讀取的消息類型;讀消息標志msgflg可以為以下幾個常值的或:
- IPC_NOWAIT 如果沒有滿足條件的消息,調用立即返回,此時,errno=ENOMSG
- IPC_EXCEPT 與msgtyp>0配合使用,返回隊列中第一個類型不為msgtyp的消息
- IPC_NOERROR 如果隊列中滿足條件的消息內容大于所請求的msgsz字節,則把該消息截斷,截斷部分將丟失。
msgrcv手冊中詳細給出了消息類型取不同值時(>0; <0; =0),調用將返回消息隊列中的哪個消息。
msgrcv()解除阻塞的條件有三個:
- 消息隊列中有了滿足條件的消息;
- msqid代表的消息隊列被刪除;
- 調用msgrcv()的進程被信號中斷;
調用返回:成功返回讀出消息的實際字節數,否則返回-1。
int msgsnd(int msqid, struct msgbuf *msgp, int msgsz, int msgflg);
向msgid代表的消息隊列發送一個消息,即將發送的消息存儲在msgp指向的msgbuf結構中,消息的大小由msgze指定。
對發送消息來說,有意義的msgflg標志為IPC_NOWAIT,指明在消息隊列沒有足夠空間容納要發送的消息時,msgsnd是否等待。造成msgsnd()等待的條件有兩種:
- 當前消息的大小與當前消息隊列中的字節數之和超過了消息隊列的總容量;
- 當前消息隊列的消息數(單位"個")不小于消息隊列的總容量(單位"字節數"),此時,雖然消息隊列中的消息數目很多,但基本上都只有一個字節。
msgsnd()解除阻塞的條件有三個:
- 不滿足上述兩個條件,即消息隊列中有容納該消息的空間;
- msqid代表的消息隊列被刪除;
- 調用msgsnd()的進程被信號中斷;
調用返回:成功返回0,否則返回-1。
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
該系統調用對由msqid標識的消息隊列執行cmd操作,共有三種cmd操作:IPC_STAT、IPC_SET 、IPC_RMID。
- IPC_STAT:該命令用來獲取消息隊列信息,返回的信息存貯在buf指向的msqid結構中;
- IPC_SET:該命令用來設置消息隊列的屬性,要設置的屬性存儲在buf指向的msqid結構中;可設置屬性包括:msg_perm.uid、msg_perm.gid、msg_perm.mode以及msg_qbytes,同時,也影響msg_ctime成員。
- IPC_RMID:刪除msqid標識的消息隊列;
調用返回:成功返回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
);
}
以上程序編譯能通過,只是出現了兩個warning
warning:內建函數strcpy不兼容的隱式聲明
warning:內建函數strlen不兼容的隱式聲明
不知是什么原因?是不是gcc版本問題?
還有在寫Makefile后,make時出現一個錯誤提示:
commands commence before first target
解決方法:去掉target的空格(tab鍵)
程序來是http://tldp.org/LDP/lpg/node45.html#SECTION00742640000000000000
API解釋來自http://www.52wg.org/computer/chengxu/200511/computer_98386.html