[問題] Linux ipcs msgsnd用法
開發平台(Platform): (Ex: VC++, Gcc, Linux, ...)
gcc
額外使用到的庫(Library Used) (Ex: OpenGL, ...)
no
問題(Question):
需要一個功能,smgsnd後,由smgcrv取出
(將來會fork成兩個process,一支傳值,一支接值)
餵入的資料(Input):
hello
預期的正確結果(Expected Output):
hello
錯誤結果(Wrong Output):
Segmentation fault
程式碼(Code): (請善用置底文標色功能)
傳送部分
struct msgbuf{
long mtype;
char mtext[50];
};
int main(){
int msgid;
msgid=msgget(0x333445,IPC_CREAT|0666);
struct msgbuf buf;
memset(buf.mtext,'\0',sizeof(buf.mtext));
strcat(buf.mtext,"hello");
buf.mtype=2;
msgsnd(msgid, (void *) &buf, sizeof(buf)-sizeof(long), 0);
printf("%s",&buf);
}
接收部分
struct msgbuf{
long mtype;
char mtext[50];
};
int main(){
int msgid;
msgid=msgget(0x333445,IPC_CREAT|0666);
struct msgbuf buf;
printf("msg is = ",buf);
msgrcv(msgid, (void *) &buf, sizeof(buf)-sizeof(long),2,0);
printf("%s",buf);
}
補充說明(Supplement):
試了很多次後,還是對msgsnd/msgrcv第二個參數與msg queue間的關係一知半解
1.msgget建立msg queue後
2.將data寫入buffer array,再用msgsnd把buffer 位址傳給msg queue
3.msgrcv到msg queue得到buffer位址後,再printf bufffer array中的data
不知道我這樣想到底錯在那裡,亂試一通但又不曉得該從何問起 嘆
--
生涯
場次 先發 時間 FG% 3P% FT% OFF DEF RPG APG SPG BPG TO PF PPG
497 403 24.8 41.2 21.4 65.9 1.6 3.0 4.6 1.1 0.6 0.6 1.00 3.20 4.5
年薪$6,200,000美元.Stanford畢業.隊上每位球員都說不能沒有他.你知道他是誰嗎?
-I Am Legend-
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 180.218.31.75
※ 編輯: MarcusWill 來自: 180.218.31.75 (01/02 16:41)
※ MarcusWill:轉錄至看板 Linux 01/02 18:13
→
01/02 19:50, , 1F
01/02 19:50, 1F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章
11
38