1. 用ftok产生一个key。
2. 调用msgget(使用key作为参数)产生一个队列
3. 进程可以用msgsnd发送消息到这个队列,相应的别的进程用msgrcv读取。
这里需要注意msgsnd可能会失败的两个情况:
a) 可能被中断打断(包括msgsnd和msgrcv). 尤其是大流量应用中更容易出现. 比较安全的用法是判断 *** 作是否被中断打断,如果被打断,则需要继续尝试。
b) 消息队列满。产生这个错误,则需要考虑提高系统消息队列规格,或者查看消息接收处是否有问题
4. msgctl函数可以用来删除消息队列
消息队列产生之后,除非明确的删除(可以用),产生的队列会一直保留在系统中。linux下消息队列的个数是有限的,注意不要泄露。如果 使用已经达到上限,msgget调用会失败,产生的错误码对应的提示信息为no space left on device.
注意点:
1.消息的类型 mtype 不需为非0值。如果使用0,则msgsnd会失败,并得到”Invalid argument“错误。
2.msgflg为0表示阻塞等待,如果msgflg为IPC_NOWAIT表示非阻塞。
3.最好使用root权限执行消息队列,否则msgrcv 提示 "Permission denied"。
26203就是你这个程序运行时候的 PID 阿,你在发消息之前,不是把当前进程的PID存放到消息里的 mtype 了吗p->mtype = getpid()
然后接收方收到这个消息,又把 mtype 打印出来了阿
printf("Message received from %ld\n%s\n" , p->mtype , p->mdata )
这个就是打印从 PID 收到了消息,消息是 p->mdata里的内容。
向Linux登录终端发消息原创 2013年02月17日 18:00:09 1600
同一台服务器,可能有很多个用户登录在上面,每个用户都是一个系统终端,可以向其他终端发送消息,同在服务器上开发的开发人员可以简单的互动(不能回复)一下哈!
一,效果
先登录一个终端,如下:
[html] view plain copy
[root@localhost /]# who
root tty1 2013-02-16 18:14 (:0)
root pts/02013-02-17 02:01 (:0.0)
[root@localhost /]#
登录的终端为pts/0。然后再打开一个终端,如下:
[html] view plain copy
[root@localhost /]# who
root tty1 2013-02-16 18:14 (:0)
root pts/02013-02-17 02:01 (:0.0)
root pts/12013-02-17 02:02 (:0.0)
[root@localhost /]#
此时登录的终端为pts/1。现在假设pts/1发消息给pts/0。
[html] view plain copy
[root@localhost /]# write root pts/0
hello
在pts/0终端上收到消息如下:
[html] view plain copy
[root@localhost /]#
Message from root@localhost.localdomain on pts/1 at 02:03 ...
hello
在pts/0上只是收到消息,不能回复的。
再看一下write命令的解释吧
NAME
write - send a message to another user
SYNOPSIS
write user [ttyname]
DESCRIPTION
Write allows you to communicate with other users, by copying lines from
your terminal to theirs.
When you run the write command, the user you are writing to gets a mes-
sage of the form:
Message from yourname@yourhost on yourtty at hh:mm ...
Any further lines you enter will be copied to the specified user’s ter-
minal. If the other user wants to reply, they must run write as well.
When you are done, type an end-of-file or interrupt character. The
other user will see the message EOF indicating that the conversation is
over.
You can prevent people (other than the super-user) from writing to you
with the mesg(1) command. Some commands, for example nroff(1) and
pr(1), may disallow writing automatically, so that your output isn’t
overwritten.
If the user you want to write to is logged in on more than one termi-
nal, you can specify which terminal to write to by specifying the ter-
minal name as the second operand to the write command. Alternatively,
you can let write select one of the terminals - it will pick the one
with the shortest idle time. This is so that if the user is logged in
at work and also dialed up from home, the message will go to the right
place.
The traditional protocol for writing to someone is that the string
‘-o’, either at the end of a line or on a line by itself, means that
it’s the other person’s turn to talk. The string ‘oo’ means that the
person believes the conversation to be over.
SEE ALSO
mesg(1), talk(1), who(1)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)