`
jubincn
  • 浏览: 242573 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
文章分类
社区版块
存档分类
最新评论

6.087 Practical Programming in C, lec14

 
阅读更多

Linux inter process communication

Preliminaries

• Each process has its own address space. Therefore, individualprocesses cannot communicate unlike threads.

• Interprocess communication: Linux/Unix provides several waysto allow communications

  • signal
    pipes
    FIFO queues
    shared memory
    semaphores
    sockets

<signals.h>

• Unix/Linux allows us to handle exceptions that arise duringexecution (e.g., interrupt, floating point error, segmentationfault etc.).

• A process recieves a signal when such a condition occurs.

void (∗signal( int sig,void(∗handler)(int )))( int )

• determines how subsequent signals will be handled.

• pre-defined behavior: SIG_DFL (default), SIG_IGN (ignore)

• returns the previous handler.

这里的exceptions应该就是运行时异常吧,从signal的类型可以得到运行时异常的类别,这个在实际应用中和工作面试中都会经常用到哦:)

Valid signals:

SIGABRT abnormal termination

SIGFPE floating point error

SIGILL illegal instruction

SIGINT interrupt

SIGSEGV segmentation fault

SIGTERM termination request

SIGBUS bus error

SIGQUIT quit

The two signals SIGSTOP,SIGKILL cannot be handled.

<signal.h>

int raise( int sig) can be used to send signal sig to the program.

Notes:

• There can be race conditions.

• signal handler itself can beinterrupted.

• use of non-reentrant functionsunsafe.

• sigprocmask can be used toprevent interruptions.

• handler is reset each time it iscalled.

signal实际上存在共享数据:操作系统中的signal记录,因此线程同步的问题也存在于signal中。

Example

#include <stdio.h>

void sigproc()

{

printf("you have pressed ctrl−c \n");

}

void quitproc()

{

printf("ctrl−\\ pressed toquit");

exit(0); ∗normal exit status∗/

}

main ( )

{

signal(SIGINT, sigproc);

signal(SIGQUIT, quitproc);

printf(‘‘ctrl−c disabled use ctrl−\\ to quit\n’’);

for( ; ; ); /∗infinite loop∗/

}

signal方法的作用是定义接受到某种信号时对应的操作。

Fork

pid_t fork (void)

• fork() is a system call to createa new process

• In the child process, it returns 0

• In the parent process, it returnsthe PID (process id) of the child.

• The child PID can be used to sendsignals to the child process.

• returns -1 on failure (invalidPID)

原来创建一个子进程是如此简单,只需复制自己即可。操作系统自身应该是所有进程的祖先进程。

Example

#include <stdlib.h>

#include <stdio.h>

#include <signal.h>


int main(){

pid_t pid =fork();

int i;

if(pid){

for(i=0;i<5; i++){

sleep(2);

}

printf("parent process: %d\n", i);

}

运行结果:

childprocess: 0

parent process: 0

child process: 1

child process: 2

parent process: 1

child process: 3

child process: 4

parent process: 2

parent process: 3

parent process: 4

Fork

• fork() makes a full copy of the parents address space.

• pid_t getpid() returns PID of the current process.

• pid_t getppid() returns PID of the parent process.

• wait( int∗) is used to wait for the child to finish.

• waitpid () is used to wait for a specific child.

Zombies:

• the child process can exit before the parent

• stray process is marked as <defunct>

• preap can be used to reap zombie processes.

子进程可以在父进程结束之前结束,父进程能否在子进程之前结束呢?我很怀疑,因为子进程有父进程的PID,如果父进程没了,那么这个PID就为空,看起来不怎么舒服。

Pipes

Pipes are used in unix to redirect output of one command toanother. Pipes also allow parent processes to communicate with itschildren.

Examples

• ls | more - displays results of lsone screen at a time

• cat file.txt | sort -displayscontents of file.txt in sorted order

int pipe(int FILEDES[2])

• A pipe can be thought of as a pairof file descriptors

• no physical file is associatedwith the file descriptor

• one end is opened in write mode.

• other end is opened in read mode.

管道在UNIX中是一个十分重要的工具,实现简单但功能强大,很符合UNIXstyle。其实现可以看作用一根两端为fd(filedescriptor)的管道将两个进程连接起来,管道前面的进程会有输出到一个文件,而后面的进程则会读取一个文件。

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <unistd.h>


int main(void)

{

int pfds[2];

char buf[30];

if (pipe(pfds) == -1) {

perror("pipe");

exit(1);

}

printf("writing to file descriptor#%d\n", pfds[1]);

write(pfds[1], "test", 5);

printf("reading from filedescriptor #%d\n", pfds[0]);

read(pfds[0], buf, 5);

printf("read \"%s\"\n",buf);

return 0;

}

FIFO

• FIFO queues may be thought of as named pipes.

• Multiple processes can read and write from a FIFO.

• Unlike pipes, the processes can be unrelated.

• FIFOs can be created using mknod system call.

intmknod (const char ∗path,mode_t mode,dev_t dev)

• <sys/stat.h> contains the declaration for mknod.

• mknod used to create special files - devices,fifos etc.

• mode can have special bits such as S_IFIFO | 0644

• dev is interpreted based on the mode.

Example: mknod("myfifo", S_IFIFO | 0644 , 0);

FIFO和管道本质上都是使用读写同一个文件的方法实现线程间的通信。

Example


分享到:
评论

相关推荐

    Umich反应工程_1-25课件.zip

    lec14.ppt lec15.ppt lec16.ppt lec17.ppt lec18.ppt lec19.ppt lec2.ppt lec20.ppt lec21.ppt lec22.ppt lec23.ppt lec24.ppt lec25.ppt lec3.ppt lec4.ppt lec5.ppt lec6.ppt lec7.ppt lec8.ppt lec9.ppt

    MIT10_626S11_lec06.pdf

    In the previous lecture, we leant about impedance spectroscopy. Electrochemical impedance spectroscopy is the technique where the cell or electrode impedance is platted versus frequency. Thus, the ...

    programming in computing 10a lec2 ppt

    programming in computing 10a lec2

    lec.rar_LEC

    6. **划分风险等级**:根据LEC分值将危险源分为不同的风险等级,如低风险、中风险和高风险。 总的来说,"lec.rar_LEC"提供的工具是进行LEC风险评估的重要助手,它使用MATLAB语言编写,能够客观地计算和分析工作场所...

    EI374 高级算法-全套 PPT 课件-笔记

    EI374 高级算法-全套 PPT 课件-笔记 lec1-slides.pdf lec1.pdf lec2-slides.pdf lec2.pdf lec3-slides.pdf lec3.pdf lec4-slides.pdf ...lec6.pdf lec7.pdf lec8.pdf lec9.pdf lec10.pdf lec11.pdf

    MIT计算机图形学课程6.837课件

    Lec 14 Acceleration Structures for Ray Casting Assignment 3 Lec 15 Shading and Material Appearance Lec 16 Texture Mapping and Shaders Lec 17 Sampling, Aliasing, and Mipmaps Lec 18 Global ...

    数字逻辑设计及应用教学英文课件:Lec14-Chap 6 4bit2比较in85 奇偶280.ppt

    数字逻辑设计及应用教学英文课件:Lec14-Chap 6 4bit2比较in85 奇偶280.ppt

    lec-培训(完整版).pdf

    《LEC培训(完整版).pdf》是一份关于逻辑等效检查的详细教程,重点介绍了使用Conformal工具进行逻辑等效验证的方法和技术。Conformal是一款强大的逻辑等效检查工具,广泛应用于芯片设计的验证阶段,确保设计的逻辑...

    lec.rar_V2

    在Linux系统中,头文件是C语言编程的关键部分,它们允许程序员在源代码(如lec.c)中使用特定的函数和数据类型,而无需在每个文件中包含完整的实现。lec.c很可能是这个LAN Emulation客户端的实现文件,包含实际的...

    EI338 计算机系统工程-Computer Systems Engineering-全套 PPT 课件

    EI338 计算机系统工程-Computer Systems Engineering-全套 PPT 课件 CA-lec1.pdf ...lec6-OS.pdf lec7-OS.pdf lec8-OS.pdf lec9-OS.pdf lec10-OS.pdf lec11-OS.pdf lec12-OS.pdf Study-Guide.pdf Summary.pdf

    麻省理工matlab课件-MIT6_094IAP10_lec04.pdf

    麻省理工matlab课件-MIT6_094IAP10_lec04.pdf 本帖最后由 sunchy11 于 2012-2-8 15:46 编辑 分享个MIT的matlab 教程,属于初级入门,希望对大家有帮助哈。

    lec14_软件测试.pptx

    描述一种用来促进鉴定软件的正确性、完整性、安全性和质量的过程。换句话说,软件测试是一种实际输出与预期输出之间的审核或者比较过程。软件测试的经典定义是:在规定的条件下对程序进行操作,以发现程序错误,衡量...

    HOLLiAS-LEC G3 PLC选型手册.pdf

    由于提供的文件内容主要是一些网站链接、电子邮箱地址和数字的排列,没有提供实际的HOLLiAS-LEC G3 PLC选型手册的详细内容,我无法直接从中提取相关的知识点。然而,我可以根据HOLLiAS-LEC G3 PLC这个主题,根据一般...

    Week1—4_Note_Lec1—6.pdf

    Week1—4_Note_Lec1—6.pdf

    lec.zip_LEC _点名_点名系统_点名系统C_随机点名

    总结而言,“lec.zip_LEC _点名_点名系统_点名系统C_随机点名”提供的是一款基于C语言实现的随机点名系统,它集成了随机数生成和数据管理技术,为教学环境带来了便捷和公平。通过理解和掌握这样的系统,不仅可以提升...

    立华 LEC-3210 19’@2U嵌入式通讯管理机 产品介绍.pdf

    立华LEC-3210是一款19英寸标准2U机架式的嵌入式通讯管理机,针对电力自动化行业设计,具备无风扇和低功耗的特点。该设备基于Intel Atom D525双核处理器,搭载6个千兆以太网接口(GbE)、10/18个串行通信接口(COM)...

    Lec-1-SDLC.rar_LEC

    "Lec 1 SDLC.pptx"这个文件很可能包含了对SDLC和UML的详细介绍,涵盖了以上各个阶段的理论知识和实际应用案例。通过学习这个讲座材料,可以深入理解这两个概念如何在实际项目中协同工作,提升软件开发的效率和质量。...

    demo_Lec.m

    demo_Lec.m

    Lec1-Introduction.pdf.zip.zip

    Lec1-Introduction.pdf.zip

    麻省理工matlab课件-MIT6_094IAP10_lec05.pdf

    麻省理工matlab课件-MIT6_094IAP10_lec05.pdf 本帖最后由 sunchy11 于 2012-2-8 15:46 编辑 分享个MIT的matlab 教程,属于初级入门,希望对大家有帮助哈。

Global site tag (gtag.js) - Google Analytics