Linux进程间通信类型 含示例

1:Linux进程间通信类型

2:   管道(pipe)和有名管道(FIFO)。

3:  信号(signal),见signal函数、sigaction函数及信号集操作函数信号的发送和捕捉函数(alarm、kill、raise、pause、 sleep、abort)

4:  共享内存,见共享内存函数(shmget、shmat、shmdt、shmctl)及其范例

5:  消息队列,见消息队列函数(msgget、msgctl、msgsnd、msgrcv)及其范例

6:  信号量,见信号量函数(semget、semop、semctl)及其范例

7:  套接字(socket),入门见SOCKET网络编程 (通俗易懂入门篇)

                                     若想全面,可见TCP/IP模型socket主要函数说明socket地址说明及转换函数

                                      及listen()函数中backlog参数分析linux进程间通信--消息队列相关函数(ftok)详解

 

附4:共享内存中程序如下:

//父子进程
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <error.h>

#define SIZE 1024
int main()
{
    int shmid ;
    char *shmaddr ;
    struct shmid_ds buf ;
    int flag = 0 ;
    int pid ; 

    shmid = shmget(IPC_PRIVATE, SIZE, IPC_CREAT|0600 ) ;
    if ( shmid < 0 ) {
            perror("get shm  ipc_id error") ;
            return -1 ;
    }

    pid = fork() ;
    if ( pid == 0 ) {
        shmaddr = (char *)shmat( shmid, NULL, 0 ) ;
        if ( (int)shmaddr == -1 ) {
            perror("shmat addr error") ;
            return -1 ;
        }

        strcpy( shmaddr, "Hi, I am child process!\n") ;
        shmdt( shmaddr ) ;
        return  0;
    } else if ( pid > 0) {
        sleep(3 ) ;
        flag = shmctl( shmid, IPC_STAT, &buf) ;
        if ( flag == -1 ) {
            perror("shmctl shm error") ;
            return -1 ;
        }

        printf("shm_segsz =%d bytes\n", buf.shm_segsz ) ;
        printf("parent pid=%d, shm_cpid = %d \n", getpid(), buf.shm_cpid ) ;
        printf("chlid pid=%d, shm_lpid = %d \n",pid , buf.shm_lpid ) ;

        shmaddr = (char *) shmat(shmid, NULL, 0 ) ;
        if ( (int)shmaddr == -1 ) {
            perror("shmat addr error") ;
            return -1 ;
        }
        printf("%s", shmaddr) ;

        shmdt( shmaddr ) ;
        shmctl(shmid, IPC_RMID, NULL) ;
    } else {
        perror("fork error") ;
        shmctl(shmid, IPC_RMID, NULL) ;
    }

    return 0 ;
}

shm_segsz =1024 bytes
parent pid=12, shm_cpid = 12 
chlid pid=13, shm_lpid = 13 
Hi, I am child process!


//写端进程
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

typedef struct{
    char name[8];
    int age;
} people;

int main(int argc, char** argv)
{
    int shm_id,i;
    key_t key;
    char temp[8];
    people *p_map;
   	char pathname[30] ;

    strcpy(pathname,"/tmp") ;
    key = ftok(pathname,0x03);
    if(key==-1)
    {
        perror("ftok error");
        return -1;
    }
    printf("key=%d\n",key) ;

    shm_id=shmget(key,4096,IPC_CREAT|IPC_EXCL|0600); 
    if(shm_id==-1)
    {
        perror("shmget error");
        return -1;
    }
    printf("shm_id=%d\n", shm_id) ;

    p_map=(people*)shmat(shm_id,NULL,0);
    memset(temp, 0x00, sizeof(temp)) ;
    strcpy(temp,"test") ;
    temp[4]='0';
    for(i = 0;i<3;i++)
    {
        temp[4]+=1;
        strncpy((p_map+i)->name,temp,5);
        (p_map+i)->age=0+i;
    }

    shmdt(p_map) ;
    return 0 ;
}

key=52772138
shm_id=0

//读端进程
#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>

typedef struct{
    char name[8];
    int age;
} people;

int main(int argc, char** argv)
{
    int shm_id,i;
    key_t key;
    people *p_map;
    char pathname[30] ;

    strcpy(pathname,"/tmp") ;
    key = ftok(pathname,0x03);
    if(key == -1)
    {
        perror("ftok error");
        return -1;
    }
    printf("key=%d\n", key) ;

    shm_id = shmget(key,0, 0);   
    if(shm_id == -1)
    {
        perror("shmget error");
        return -1;
    }
    printf("shm_id=%d\n", shm_id) ;

    p_map = (people*)shmat(shm_id,NULL,0);
    for(i = 0;i<3;i++)
    {
        printf( "name:%s\n",(*(p_map+i)).name );
        printf( "age %d\n",(*(p_map+i)).age );
    }
    if(shmdt(p_map) == -1)
    {
        perror("detach error");
        return -1;
    }

    return 0 ;
}

附5:消息队列中程序如下:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <error.h>

#define TEXT_SIZE  512
struct msgbuf
{
    long mtype ;
    char mtext[TEXT_SIZE] ;
}  ;

int main(int argc, char **argv)
{
    int msqid ;
    struct msqid_ds info ;
    struct msgbuf buf ;
    struct msgbuf buf1 ;
    struct msgbuf buf2 ; //
    int flag ;
    int sendlength, recvlength ;

    msqid = msgget( IPC_PRIVATE, 0666 ) ;
    if ( msqid < 0 )
    {
        perror("get ipc_id error") ;
        return -1 ;
    }

    buf.mtype = 1 ;
    strcpy(buf.mtext, "happy new year!") ;
    sendlength = sizeof(struct msgbuf) - sizeof(long) ;
    flag = msgsnd( msqid, &buf, sendlength , 0 ) ;
    if ( flag < 0 )
    {
        perror("send message error") ;
        return -1 ;
    }

    buf.mtype = 3 ;
    strcpy(buf.mtext, "good bye!") ;
    sendlength = sizeof(struct msgbuf) - sizeof(long) ;
    flag = msgsnd( msqid, &buf, sendlength , 0 ) ;
    if ( flag < 0 )
    {
        perror("send message error") ;
        return -1 ;
    }

    flag = msgctl( msqid, IPC_STAT, &info ) ;
    if ( flag < 0 )
    {
        perror("get message status error") ;
        return -1 ;
    }

    printf("uid:%d, gid = %d, cuid = %d, cgid= %d\n" ,
        info.msg_perm.uid,  info.msg_perm.gid,  info.msg_perm.cuid,  info.msg_perm.cgid  ) ;
    printf("read-write:%03o, cbytes = %lu, qnum = %lu, qbytes= %lu\n" ,
        info.msg_perm.mode&0777, info.msg_cbytes, info.msg_qnum, info.msg_qbytes ) ;

    system("ipcs -q") ;
    recvlength = sizeof(struct msgbuf) - sizeof(long) ;
    memset(&buf1, 0x00, sizeof(struct msgbuf)) ;

    flag = msgrcv( msqid, &buf1, recvlength ,3,0 ) ;
    if ( flag < 0 )
    {
        perror("recv message error") ;
        return -1 ;
    }
    printf("type=%d, message=%s\n", buf1.mtype, buf1.mtext) ;


    memset(&buf2, 0x00, sizeof(struct msgbuf)) ;
    flag = msgrcv( msqid, &buf2, recvlength ,1,0 ) ;
    if ( flag < 0 )
    {
        perror("recv message error") ;
        return -1 ;
    }
    printf("type=%d, message=%s\n", buf2.mtype, buf2.mtext) ;


    flag = msgctl( msqid, IPC_RMID,NULL) ;
    if ( flag < 0 )
    {
        perror("rm message queue error") ;
        return -1 ;
    }
    system("ipcs -q") ;

    return 0 ;
}

输出:

uid:0, gid = 0, cuid = 0, cgid= 0
read-write:666, cbytes = 1024, qnum = 2, qbytes= 16384
------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    
0x00000000 0          root       666        1024         2           
type=3, message=good bye!
type=1, message=happy new year!
------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值