📄 chinaunix_net - unix进程之间的通信.htm
字号:
<BR>perror("msgsnd"); <BR>exit(1); <BR>} <BR>}
<BR><BR>void read_message(int qid, struct mymsgbuf *qbuf, long type)
<BR>{
<BR>/* Read a message from the queue */
<BR>printf("Reading a message ...\n");
<BR>qbuf->mtype = type;
<BR>msgrcv(qid, (struct msgbuf *)qbuf, MAX_SEND_SIZE, type, 0);
<BR><BR>printf(" Type: %ld Text: %s\n", qbuf->mtype, qbuf->mtext);
<BR>} <BR><BR>void remove_queue(int qid) <BR>{
<BR>/* Remove the queue */
<BR>msgctl(qid, IPC_RMID, 0); <BR>}
<BR><BR>void change_queue_mode(int qid, char *mode)
<BR>{ <BR>struct msqid_ds myqueue_ds;
<BR><BR>/* Get current info */
<BR>msgctl(qid, IPC_STAT, &myqueue_ds);
<BR><BR>/* Convert and load the mode */
<BR>sscanf(mode, "%ho", &myqueue_ds.msg_perm.mode);
<BR><BR>/* Update the mode */
<BR>msgctl(qid, IPC_SET, &myqueue_ds); <BR>}
<BR><BR>void usage(void) <BR>{
<BR>fprintf(stderr, "msgtool - A utility for tinkering with msg queues\n");
<BR>fprintf(stderr, "\nUSAGE: msgtool (s)end <type> <messagetext> <msgid>\n");
<BR>fprintf(stderr, " (r)ecv <type>\n");
<BR>fprintf(stderr, " (d)elete\n");
<BR>fprintf(stderr, " (m)ode <octal mode>\n");
<BR>fprintf(stderr, "note: type must be number!\n");
<BR>exit(1); <BR>} <BR><BR><BR>2,Tcp/IP socket编程例子
<BR><BR>1), Client方 <BR><BR>#include <stdio.h>
<BR>#include <sys/types.h>
<BR>#include <sys/socket.h>
<BR>#include <netinet/in.h>
<BR>#include <arpa/inet.h>
<BR>#include <netdb.h>
<BR><BR>int main(int argc, char *argv[]) <BR>{
<BR>int sockfd ,newsockfd, help, sent;
<BR>struct sockaddr_in peer;
<BR>struct hostent *serverhost; <BR>char buff[5000];
<BR><BR><BR>if(argc<2) {
<BR>fprintf(stderr, "Usage: coc <hostname>\n");
<BR>exit(1); <BR>}
<BR><BR>if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
<BR>perror("socket"); <BR>exit(1); <BR>}
<BR><BR><BR>if((serverhost = gethostbyname(argv[1])) == 0) {
<BR>perror("gethostbyname"); <BR>exit(1); <BR>}
<BR><BR><BR>peer.sin_family = AF_INET;
<BR>peer.sin_port = htons(10000);
<BR>peer.sin_addr = *(struct in_addr*)serverhost->h_addr_list[0];
<BR><BR>if (connect(sockfd, &peer, sizeof(peer)) < 0 ) {
<BR>perror("connect"); <BR>exit(1); <BR>}
<BR><BR>for(help=0; help<sizeof(buff); help++)
<BR>buff[help] = '0'+help%10;
<BR><BR>write(sockfd, buff, 5000); <BR><BR>close(sockfd);
<BR>} <BR><BR><BR>2, Server方
<BR><BR>#include <stdio.h>
<BR>#include <sys/types.h>
<BR>#include <sys/socket.h>
<BR>#include <netinet/in.h>
<BR><BR>void process(int fd) <BR>{
<BR>char buff[10000]; <BR>int received;
<BR>int help,read_bytes; <BR><BR>received = 5000;
<BR>memset ( buff, '.', received );
<BR>read_bytes = read(fd, buff, received);
<BR>if (read_bytes < 0) { <BR>perror("read");
<BR>exit(1); <BR>}
<BR><BR>printf("%d bytes have received on socket %d\n", read_bytes, fd);
<BR>printf("buff=\n%s\n", buff);
<BR>for(help=0; help<received; help++)
<BR>if(buff[help] != '0'+help%10) <BR>{
<BR>printf("Error on position %d\n", help);
<BR>break; <BR>} <BR>} <BR><BR>int main(void) <BR>{
<BR>int sockfd ,newsockfd;
<BR>struct sockaddr_in myaddr, peer;
<BR><BR>int addrlen1,addrlen2;
<BR><BR>if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0 ) {
<BR>perror("socket"); <BR>exit(1); <BR>}
<BR><BR>addrlen1 = sizeof(myaddr);
<BR><BR>myaddr.sin_family = AF_INET;
<BR>myaddr.sin_port = htons(10000);
<BR>myaddr.sin_addr.s_addr = INADDR_ANY;
<BR>if (bind(sockfd, &myaddr , addrlen1) < 0 ) {
<BR>perror("bind"); <BR>exit(1); <BR>}
<BR><BR>if (listen(sockfd, 5 )) {
<BR>perror("listen"); <BR>exit(1); <BR>} <BR><BR>for (;;) <BR>{
<BR>addrlen2 = sizeof(peer);
<BR>newsockfd = accept(sockfd, &peer , &addrlen2);
<BR>if ( newsockfd < 0) {
<BR>perror("accept"); <BR>exit(1); <BR>}
<BR>if (fork() == 0) { <BR>close(sockfd);
<BR><BR>/* process request */
<BR>printf("connection on socket %d from %s\n", newsockfd, inet_ntoa(peer.sin_addr.s_addr));
<BR>process(newsockfd); <BR>close(newsockfd); <BR>exit(0); <BR><BR>}
<BR>close(newsockfd); <BR>} <BR>} <BR><BR><BR>3,共享内存编程例子
<BR><BR>例子1: <BR><BR>#include <sys/types.h>
<BR><BR>#include <sys/ipc.h>
<BR><BR>#include <sys/shm.h>
<BR><BR>#define SHMKEY 74 <BR><BR>#define K 1024
<BR><BR>int shmid; <BR><BR>cleanup() <BR><BR>{
<BR><BR>shmctl(shmid,IPC_RMID,0); <BR><BR>exit(0); <BR><BR>}
<BR><BR>main() <BR><BR>{ <BR><BR>int *pint;
<BR><BR>char *addr1,*addr2;
<BR><BR>extern char *shmat();
<BR><BR>extern cleanup(); <BR><BR>for (i=0;i<20;i++)
<BR><BR>signal(i,cleanup);
<BR><BR>shmid=shmget(SHMKEY,128*K,0777|IPC_CREAT);
<BR><BR>addr1=shmat(shmid,0,0); <BR><BR>addr2=shmat(shmid,0,0);
<BR><BR>printf("addr1 0x%x addr2 0x%x\n",addr1,addr2);
<BR><BR>pint=(int*)addr1; <BR><BR>for (i=0;i<256;i++)
<BR><BR>*pint++=i; <BR><BR>pint=(int*)addr1; <BR><BR>*pint=256;
<BR><BR>pint=(int*)addr2; <BR><BR>for (i=0;i<256;i++)
<BR><BR>printf("index %d\tvalue%d\n",i,*pint++);
<BR><BR>shmdt(addr1); <BR><BR>shmdt(addr2); <BR><BR>pause();
<BR><BR>} <BR><BR>例子2 <BR><BR>1),创建和写共享内存:
<BR><BR>/* Includes */ <BR>#include <errno.h>
<BR>#include <stdio.h> <BR>#include <stdlib.h>
<BR>#include <string.h>
<BR>#include <sys/types.h>
<BR>#include <sys/ipc.h>
<BR>#include <sys/shm.h> <BR><BR>typedef struct
<BR>{ <BR>int tc_number; <BR>char ap_name[5];
<BR>char mymessage[20]; <BR><BR>} COMM_TABLE;
<BR><BR>main() <BR>{ <BR><BR>/* local variables */
<BR>int ret= 0; <BR>key_t key; <BR>int i;
<BR>int shm_id; <BR>int found = 0;
<BR>COMM_TABLE *comm_reg;
<BR><BR>key = ftok(".",'w');
<BR><BR>/* create a share memory if not exist */
<BR>if ((shm_id = shmget(key ,sizeof(COMM_TABLE),IPC_CREAT|IPC_EXCL|0666)) == -1)
<BR>{
<BR>/* share memory has been created */
<BR>if ((shm_id = shmget(key , sizeof(COMM_TABLE),0)) == -1)
<BR>{ <BR>printf("error = %d\n", errno);
<BR>return (ret); <BR><BR>} <BR>}
<BR>comm_reg = (COMM_TABLE *) shmat(shm_id, (char *) 0, SHM_SHARE_MMU);
<BR>comm_reg->tc_number= 56110563; <BR>}
<BR><BR>2), 读共享内存,再删除共享内存: <BR><BR>/* Includes */
<BR>#include <errno.h> <BR>#include <stdio.h>
<BR>#include <stdlib.h>
<BR>#include <string.h>
<BR>#include <sys/types.h>
<BR>#include <sys/ipc.h>
<BR>#include <sys/shm.h> <BR><BR>typedef struct
<BR>{ <BR>int tc_number; <BR>char ap_name[5];
<BR>char mymessage[20]; <BR><BR>} COMM_TABLE;
<BR><BR>main() <BR>{ <BR><BR>/* local variables */
<BR>int ret= 0; <BR>key_t key; <BR>int i;
<BR>int shm_id; <BR>int found = 0;
<BR>COMM_TABLE *comm_reg; <BR>char * pointer;
<BR><BR><BR>key = ftok(".",'w');
<BR><BR>/* share memory has been created */
<BR>if ((shm_id = shmget(key , sizeof(COMM_TABLE),0)) == -1)
<BR>{ <BR>printf("error = %d\n", errno);
<BR>return (ret); <BR><BR>}
<BR>comm_reg = (COMM_TABLE *) shmat(shm_id, (char *) 0, SHM_SHARE_MMU);
<BR>printf("tc number=%d!!!\n", comm_reg->tc_number);
<BR><BR>/* kill share memory */
<BR><BR>shmctl(shm_id,IPC_RMID,0);
<BR>exit(0);[/code:1:d22bc4f5e9]<BR><BR></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD align=middle><SMALL>【<A
href="http://bbs.chinaunix.net/forum/posting.php?mode=reply&t=240959">发表回复</A>】【<A
href="http://bbs.chinaunix.net/forum/viewtopic.php?t=240959">查看CU论坛原帖</A>】【<A
href="http://bbs.chinaunix.net/forum/favorites.php?mode=add&t=240959">添加到收藏夹</A>】【<A
href="javascript:window.close()">关闭</A>】</SMALL> </TD></TR></TBODY></TABLE><!-----------回复----------->
<TABLE border=0 cellPadding=0 cellSpacing=0 width="75%">
<TBODY>
<TR>
<TD bgColor=#edf0f5>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0
style="BORDER-COLLAPSE: collapse; WORD-BREAK: break-all" width="90%">
<TBODY>
<TR>
<TD>
<HR>
<SMALL> <A
href="http://bbs.chinaunix.net/forum/profile.php?mode=viewprofile&u=6759"
target=_blank>unixbaby</A> 回复于:2004-01-10 18:16:26</SMALL></TD></TR>
<TR>
<TD>good<BR><BR></TD></TR>
<TR>
<TD>
<HR>
<SMALL> <A
href="http://bbs.chinaunix.net/forum/profile.php?mode=viewprofile&u=114120"
target=_blank>unix_sco</A> 回复于:2004-01-10 21:21:08</SMALL></TD></TR>
<TR>
<TD>顶<BR><BR></TD></TR>
<TR>
<TD>
<HR>
<SMALL> <A
href="http://bbs.chinaunix.net/forum/profile.php?mode=viewprofile&u=56198"
target=_blank>newince</A> 回复于:2004-01-15 09:43:26</SMALL></TD></TR>
<TR>
<TD>天书啊!<BR><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE border=0 cellPadding=0 cellSpacing=0 width="75%">
<TBODY>
<TR>
<TD bgColor=#49ade9 colSpan=5 height=2 width="100%"><IMG height=1
src="ChinaUnix_net - UNIX进程之间的通信.files/bline.gif"
width=1></TD></TR></TBODY></TABLE><BR><SMALL>Copyright © ChinaUnix.net
<BR> * 请尊重我们的劳动,转载请注明出自<A
href="http://www.chinaunix.net/">ChinaUnix.net</A>及作者名 *
</SMALL></CENTER></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -