没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Beej's Guide to Unix IPC
Brian “Beej Jorgensen” Hall
beej@beej.us
Version 1.0.1
July 25, 2007
Copyright © 2007 Brian “Beej Jorgensen” Hall
iii
Contents
1. Intro...........................................................................................................................................1
1.1. Audience 1
1.2. Platform and Compiler 1
1.3. Official Homepage 1
1.4. Email Policy 1
1.5. Mirroring 2
1.6. Note for Translators 2
1.7. Copyright and Distribution 2
2. A fork() Primer..................................................................................................................... 3
2.1. “Seek ye the Gorge of Eternal Peril” 3
2.2. “I'm mentally prepared! Give me The Button!” 4
2.3. Summary 5
3. Signals........................................................................................................................................7
3.1. You can't SIGKILL the President! 7
3.2. Everything you know is wrong 9
3.3. Some signals to make you popular 9
4. Pipes.........................................................................................................................................11
4.1. “These pipes are clean!” 11
4.2. fork() and pipe()—you have the power! 12
4.3. The search for Pipe as we know it 13
4.4. Summary 13
5. FIFOs.......................................................................................................................................15
5.1. A New FIFO is Born 15
5.2. Producers and Consumers 15
5.3. O_NDELAY! I'm UNSTOPPABLE! 17
5.4. Multiple Writers—How do I multiplex all these? 17
5.5. Concluding Notes 18
6. File Locking............................................................................................................................ 19
6.1. Setting a lock 19
6.2. Clearing a lock 21
6.3. A demo program 21
6.4. Summary 22
7. Message Queues......................................................................................................................23
7.1. Where's my queue? 23
7.2. “Are you the Key Master?” 23
7.3. Sending to the queue 24
7.4. Receiving from the queue 25
7.5. Destroying a message queue 26
7.6. Sample programs, anyone? 26
7.7. Summary 28
Contents
iv
8. Semaphores............................................................................................................................. 29
8.1. Grabbing some semaphores 29
8.2. Controlling your semaphores with semctl() 30
8.3. semop(): Atomic power! 31
8.4. Destroying a semaphore 32
8.5. Sample programs 32
8.6. Summary 35
9. Shared Memory Segments.................................................................................................... 37
9.1. Creating the segment and connecting 37
9.2. Attach me—getting a pointer to the segment 37
9.3. Reading and Writing 38
9.4. Detaching from and deleting segments 38
9.5. Concurrency 38
9.6. Sample code 39
10. Memory Mapped Files.........................................................................................................41
10.1. Mapmaker 41
10.2. Unmapping the file 42
10.3. Concurrency, again?! 42
10.4. A simple sample 42
10.5. Summary 44
11. Unix Sockets..........................................................................................................................45
11.1. Overview 45
11.2. What to do to be a Server 45
11.3. What to do to be a client 48
11.4. socketpair()—quick full-duplex pipes 49
12. More IPC Resources............................................................................................................51
12.1. Books 51
12.2. Other online documentation 51
12.3. Linux man pages 51
1
1. Intro
You know what's easy? fork() is easy. You can fork off new processes all day and have
them deal with individual chunks of a problem in parallel. Of course, its easiest if the processes
don't have to communicate with one another while they're running and can just sit there doing
their own thing.
However, when you start fork()'ing processes, you immediately start to think of the
neat multi-user things you could do if the processes could talk to each other easily. So you try
making a global array and then fork()'ing to see if it is shared. (That is, see if both the child
and parent process use the same array.) Soon, of course, you find that the child process has its
own copy of the array and the parent is oblivious to whatever changes the child makes to it.
How do you get these guys to talk to one another, share data structures, and be generally
amicable? This document discusses several methods of Interprocess Communication (IPC) that
can accomplish this, some of which are better suited to certain tasks than others.
1.1. Audience
If you know C or C++ and are pretty good using a Unix environment (or other POSIXey
environment that supports these system calls) these documents are for you. If you aren't that
good, well, don't sweat it—you'll be able to figure it out. I make the assumption, however, that
you have a fair smattering of C programming experience.
As with Beej's Guide to Network Programming Using Internet Sockets
1
, these documents
are meant to springboard the aforementioned user into the realm of IPC by delivering a concise
overview of various IPC techniques. This is not the definitive set of documents that cover this
subject, by any means. Like I said, it is designed to simply give you a foothold in this, the
exciting world of IPC.
1.2. Platform and Compiler
The examples in this document were compiled under Linux using gcc. They should
compile anywhere a good Unix compiler is available.
1.3. Official Homepage
This official location of this document is http://beej.us/guide/bgipc/.
1.4. Email Policy
I'm generally available to help out with email questions so feel free to write in, but I can't
guarantee a response. I lead a pretty busy life and there are times when I just can't answer a
question you have. When that's the case, I usually just delete the message. It's nothing personal;
I just won't ever have the time to give the detailed answer you require.
As a rule, the more complex the question, the less likely I am to respond. If you can narrow
down your question before mailing it and be sure to include any pertinent information (like
platform, compiler, error messages you're getting, and anything else you think might help
me troubleshoot), you're much more likely to get a response. For more pointers, read ESR's
document, How To Ask Questions The Smart Way
2
.
If you don't get a response, hack on it some more, try to find the answer, and if it's still
elusive, then write me again with the information you've found and hopefully it will be enough
for me to help out.
1. http://beej.us/guide/bgnet/
2. http://www.catb.org/~esr/faqs/smart-questions.html
剩余55页未读,继续阅读
资源评论
micojade
- 粉丝: 2
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功