msgget() 시스템 호출을 사용하여 msg를 작성한 후, cout 또는 printf를 사용할 수 없습니다.msgget() 후, cout 또는 printf가 작동하지 않습니다.
다음은 메시지 큐에서 수신을 시도하는 코드입니다. 28 행의 "printf"는 아무 것도 인쇄하지 않습니다. 하지만 하나의 "cout"또는 "printf"문을 29 행에 추가하면 제대로 작동합니다. 내 코드의 결함을 알려 주시기 바랍니다.
1 #include<sys/errno.h>
2 #include<sys/wait.h>
3 #include<sys/ipc.h>
4 #include<stdio.h>
5 #include<iostream.h>
6 #include<sys/msg.h>
7 #include<sys/types.h>
8
9 struct messgQ
10 {
11 char text[1024];
12 long int mtype;
13 };
14
15
16 int main()
17 {
18 struct messgQ R;
19
20 R.mtype=1;
21
22 int qid=0;
23 qid=msgget((key_t)1234,0766|IPC_CREAT);
24 cout<<"\n 1 MsgQ created with id ="<<qid;
25
26 if(qid > 0)
27 {
28 printf("\n MsgQ created with id =%d",qid);
29 }
30
31 int rc=0,run=1;
32 while(run)
33 {
34 memset(R.text,0x00,sizeof(R.text));
35 msgrcv(qid,&R,sizeof(R.text),1,0);
36 cout<<"\n Recvd:"<<R.text;
37 if(!strncmp(R.text,"bye",3))
38 {
39 cout<<"\n Exiting";
40 run=0;
41 }
42 }
43 rc=msgctl(qid,IPC_RMID,NULL);
44 if (rc < 0)
45 perror(strerror(errno));
46 }
로 세척,
를 수행하여 세척을 강제 또는 것은'qid' 당신 ' 실제로 0보다 큰'msgget()'에서 다시 얻는가? 왜냐하면 그렇지 않다면'printf()'는 호출되지 않을 것이고'msgget()'이 실패한 이유를 보려면'errno'를 봐야 할 것입니다. – TripeHound