linux下进程间通信(1)

1.Linux下进程间通信方式有有那些?

1)管道( pipe )及有名管道 (named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信
2)信号量( semophore ) : 信号量是一个计数器,可以用来控制多个进程对共享资源的访问。它常作为一种锁机制,防止某进程正在访问共享资源时,其他进程也访问该资源。因此,主要作为进程间以及同一进程内不同线程之间的同步手段。
3)消息队列( message queue ) : 消息队列是由消息的链表,存放在内核中并由消息队列标识符标识。消息队列克服了信号传递信息少、管道只能承载无格式字节流以及缓冲区大小受限等缺点。
4)信号 ( sinal ) : 信号是一种比较复杂的通信方式,用于通知接收进程某个事件已经发生。除了用于进程间通信外,进程还可以发送信号给进程本身;linux除了支持Unix早期 信号语义函数sigal外,还支持语义符合Posix.1标准的信号函数sigaction(实际上, 该函数是基于BSD的,BSD为了实现可靠信号机制,又能够统一对外接口,sigaction函数重新实现了signal函数)
5)共享内存( shared memory ) :共享内存就是映射一段能被其他进程所访问的内存,这段共享内存由一个进程创建,但多个进程都可以访问。共享内存是最快的 IPC 方式,它是针对其他进程间通信方式运行效率低而专门设计的。它往往与其他通信机制,如信号两,配合使用,来实现进程间的同步和通信。
6)套接字( socket ) : 套解口也是一种进程间通信机制,与其他通信机制不同的是,它可用于不同及其间的进程通信。

2.无名管道的建立

    /* 适用于具有亲缘关系的进程间通信 */   
    #include <stdio.h>  
    #include <unistd.h>  
    #include <stdlib.h>  
    #include <string.h>  


    int main()  
    {  
        int filedes[2];  
        char buf[100] = {0};  
        int ret;  
        pid_t pid;  
        char s[100] = "Hello World!\n";  

        ret = pipe(filedes);  
        if(ret < 0)  
        {  
            perror("pipe");  
            exit(1);  
        }  

        pid = fork();  
        if(pid < 0)  
        {  
            perror("fork");  
            exit(2);  
        }  
        else if(0 == pid)  
        {  
            printf("Child begin write!\n");  
            write(filedes[1],s,strlen(s));   //filedes[1] is for writing  
        }  
        else  
        {  
            read(filedes[0],buf,sizeof(buf));   //filedes[0] is for reading  
            printf("Parent Read:%s",buf);  
        }  


        return 0;  
    }  


    /* 子进程借管道将字符串"Hello World!\n" 给父进程显示 */  

3.有名管道的建立

    /* 进程一:读有名管道*/   
    #include <stdio.h>   
    #include <unistd.h>   
    void main() {   
    FILE * in_file;   
    int count = 1;   
    char buf[80];   
    in_file = fopen("mypipe", "r");   
    if (in_file == NULL) {   
    printf("Error in fdopen./n");   
    exit(1);   
    }   
    while ((count = fread(buf, 1, 80, in_file)) > 0)   
    printf("received from pipe: %s/n", buf);   
    fclose(in_file);   
    }   


      /* 进程二:写有名管道*/   
    #include <stdio.h>   
    #include <unistd.h>   
    void main() {   
    FILE * out_file;   
    int count = 1;   
    char buf[80];   
    out_file = fopen("mypipe", "w");   
    if (out_file == NULL) {   
    printf("Error opening pipe.");   
    exit(1);   
    }   
    sprintf(buf,"this is test data for the named pipe example/n");   
    fwrite(buf, 1, 80, out_file);   
    fclose(out_file);   
    }   

4.管道通信作用范围
在进程编程时一般会用到进程间的通信方式,管道与信号,管道分无名管道pipe与有名管道fifo,无名管道一般用于有亲缘关系之间的进程通信,如父子进程通信,fifo较多用于不同应用程序之间的进程通信通过建立管道文件,具体的通信机制,查看相应的书籍即可。

5.信号通信:kill

#include <sys/types.h>  
#include <signal.h>  
#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
#include <wait.h>  


int main()  
{  
    pid_t pid;  

    pid = fork();  
    if(pid < 0)  
    {  
        perror("fork");  
        exit(1);  
    }  
    else if(0 == pid)  
    {  
        printf("Child Process!\n");  
        sleep(10);  
    }  
    else  
    {  
        printf("Parent Process!\n");  
        sleep(2);  
        kill(pid,SIGINT);   //给子进程发送SIGINT 信号,使子进程结束  
        waitpid(pid,NULL,0);  //确保父进程后结束  
    }  

    return 0;  
}

6.信号通信:signal

#include <stdio.h>  
#include <unistd.h>  
#include <signal.h>  

void func(int sig)  
{  
    printf("Hello World! %d \n",sig);   
}  

int main()  
{  
    signal(SIGINT,func);   //只要按下contrl+C ,则指向func函数  

    while(1);  

    return 0;  
}  

7.raise

#include <stdio.h>  
#include <signal.h>  

int main()  
{  
    int i;  

    printf("Begin!\n");  
    for(i = 1;i <= 2;i++)  
    {  
        printf("sleep %d\n",i);  
        sleep(1);  
    }  
    raise(SIGINT);  //给当前信号发送终止信号  

    while(1);  

    return 0;  
}  

8.alarm

#include <stdio.h>  
#include <unistd.h>  
#include <signal.h>  

void func(int sig)  
{  
    printf("Hello Wrold!\n");  
}  

int main()  
{  
    int i;  

    signal(SIGALRM,func);  
    alarm(5);   //5秒后发送SIGALRM信号给当前进程  

    for(i = 1;i < 7;i++)  
    {  
        printf("sleep %d!\n",i);  
        sleep(1);  
    }  

    return 0;  
}  

猜你喜欢

转载自blog.csdn.net/costeeer/article/details/78676611