进程间通信具体实现代码(两个程序间互发信息)

无名管道

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

int main()
{
    int ret;
    pid_t pid;
    int count = 0;
    int fd[2] = {0};

    ret = pipe(fd);
    if(-1 == ret)
    {
        perror("pipe");
        exit(1);
    }

    pid = fork();
    if(-1 == pid)
    {
        perror("fork");
        exit(1);
    }
    else if (0 == pid)
    {
        sleep(1);
        count ++;
        printf("Child Process : count = %d\n",count);
        ret = write(fd[1],&count,sizeof(count));
        if(-1 == ret)
        {
            perror("write");
            exit(1);
        }
    }
    else
    {
        ret = read(fd[0],&count,sizeof(count));
        if(-1 == ret)
        {
            perror("read");
            exit(1);
        }
        count++;
        printf("Parent Process : count = %d\n",count);
        waitpid(pid,NULL,0);
    }
}

有名管道fiffowrite

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int main()
{
    int fd,ret;
    char buf[100] = {0};

    fd = open("fifo.tmp",O_WRONLY);
    if(-1 == fd)
    {
        perror("open");
        exit(1);
    }

    while(1)
    {
        scanf("%s",buf);
        ret = write(fd,buf,strlen(buf));
        if(-1 == ret)
        {
            perror("write");
            exit(1);
        }
        if(!strncmp(buf,"bye",3))
        {
            break;
        }
    }
    return 0;
}

fiforead:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int ret,fd;
    char buf[100] = {0};

    ret = mkfifo("fifo.tmp",O_CREAT|O_EXCL);
    if(-1 == ret)
    {
        perror("mkfifo");
        exit(1);
    }

    fd = open("fifo.tmp",O_RDONLY);
    if(-1 == fd)
    {
        perror("open");
        exit(1);
    }

    while(1)
    {
        ret = read(fd,buf,sizeof(buf));
        if(-1 == ret)
        {
            perror("read");
            exit(1);
        }
        if(!strncmp(buf,"bye",3))
        {
            break;
        }
        printf("Read From Fifo : %s\n",buf);
        memset(buf,0,sizeof(buf));
        unlink("fifo.tmp");
    }


    return 0;
}

消息队列:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <string.h>
#include <stdlib.h>

#define MSGKEY 1234

struct msgbuf
{
    long mtype;
    char mtext[100];
};


int main()
{
    int msgid,ret;
    struct msgbuf buf;

    msgid = msgget(MSGKEY,IPC_CREAT|IPC_EXCL);
    if(-1 == msgid)
    {
        perror("msgget");
        exit(1);
    }

    while(1)
    {
        memset(&buf,0,sizeof(buf));
        scanf("%s",buf.mtext);
        buf.mtype = 1;

        ret = msgsnd(msgid,&buf,sizeof(buf) - sizeof(long),0);
        if(-1 == ret)
        {
            perror("msgsnd");
            exit(1);
        }
        if(!strncmp("bye",buf.mtext,3))
        {
            break;
        }
    }

    msgctl(msgid,IPC_RMID,NULL);

    return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <string.h>
#include <stdlib.h>

#define MSGKEY 1234

struct msgbuf
{
    long mtype;
    char mtext[100];
};


int main()
{
    int msgid,ret;
    struct msgbuf buf;

    msgid = msgget(MSGKEY,0);
    if(-1 == msgid)
    {
        perror("msgget");
        exit(1);
    }

    while(1)
    {
        memset(&buf,0,sizeof(buf));
        buf.mtype = 1;

        ret = msgrcv(msgid,&buf,sizeof(buf.mtext),1,0);
        if(-1 == ret)
        {
            perror("msgsnd");
            exit(1);
        }
        if(!strncmp("bye",buf.mtext,3))
        {
            break;
        }
        printf("Receive From Message : %s\n",buf.mtext);
    }


    return 0;
}

共享内存:

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>

#define SHMKEY  1234
#define SHMSIZE 4096

int main()
{
    int shmid,ret;
    void *shmaddr;

    shmid = shmget(SHMKEY, SHMSIZE, IPC_CREAT | IPC_EXCL);
    if(-1 == shmid)
    {
        perror("shmget");
        exit(1);
    }

    shmaddr = shmat(shmid,NULL,0);
    if((void *)-1 == shmaddr)
    {  
        perror("shmat");
        exit(1);
    }

    strcpy(shmaddr,"chenzheshizhu");

    sleep(5);

    ret = shmdt(shmaddr);
    if(-1 == ret)
    {
        perror("shmdt");
        exit(1);
    }

    shmctl(shmid, IPC_RMID, NULL);

    return 0;
}
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>

#define SHMKEY  1234
#define SHMSIZE 4096

int main()
{
    int shmid,ret;
    void *shmaddr;
    char buf[100] = {0};

    shmid = shmget(SHMKEY, SHMSIZE,0);
    if(-1 == shmid)
    {
        perror("shmget");
        exit(1);
    }

    shmaddr = shmat(shmid,NULL,0);
    if((void *)-1 == shmaddr)
    {  
        perror("shmat");
        exit(1);
    }

    strcpy(buf,shmaddr);

    printf("Read From sharememory : %s\n",buf);

    ret = shmdt(shmaddr);
    if(-1 == ret)
    {
        perror("shmdt");
        exit(1);
    }


    return 0;
}

共享内存——结构体:

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/sem.h>

#define SHMKEY  1234
#define SHMSIZE 4096
#define SEMKEY  1234

union semun
{
    int              val;
    struct semid_ds *buf;
    unsigned short  *array;
    struct seminfo  *__buf;
};

int sem_p(int id)
{
    struct sembuf buf;
    int ret;

    buf.sem_num = 0;
    buf.sem_op = -1;
    buf.sem_flg = SEM_UNDO;

    ret = semop(id,&buf,1);
    if(-1 == ret)
    {
        perror("semop");
        return 1;
    }

    return 0;
}

int sem_v(int id)
{
    struct sembuf buf;
    int ret;

    buf.sem_num = 0;
    buf.sem_op = 1;
    buf.sem_flg = SEM_UNDO;

    ret = semop(id,&buf,1);
    if(-1 == ret)
    {
        perror("semop");
        return 1;
    }

    return 0;
}



int main()
{
    int shmid,ret,semid;
    void *shmaddr;
    int count = 0;
    int tmp;
    union semun union_sem;

    shmid = shmget(SHMKEY, SHMSIZE, IPC_CREAT | IPC_EXCL);
    if(-1 == shmid)
    {
        perror("shmget");
        exit(1);
    }

    shmaddr = shmat(shmid,NULL,0);
    if((void *)-1 == shmaddr)
    {  
        perror("shmat");
        exit(1);
    }

    semid = semget(SEMKEY,1,IPC_CREAT | IPC_EXCL);
    if(-1 == semid)
    {
        perror("semget");
        exit(1);
    }

    union_sem.val = 1;
    ret = semctl(semid,0,SETVAL,union_sem);

    *(int *)shmaddr = count;

    while(1)
    {
        sem_p(semid);
        tmp = *(int *)shmaddr;
        if(tmp > 1000)
        {
            break;
        }
        printf("Process 1 : count = %d\n",tmp);
        usleep(10);
        tmp++;
        *(int *)shmaddr = tmp;
        sem_v(semid);
    }


    ret = shmdt(shmaddr);
    if(-1 == ret)
    {
        perror("shmdt");
        exit(1);
    }

    sleep(1);

    shmctl(shmid, IPC_RMID, NULL);

    semctl(semid,0,IPC_RMID);
    return 0;
}
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/sem.h>

#define SHMKEY  1234
#define SHMSIZE 4096
#define SEMKEY  1234

union semun
{
    int              val;
    struct semid_ds *buf;
    unsigned short  *array;
    struct seminfo  *__buf;
};

int sem_p(int id)
{
    struct sembuf buf;
    int ret;

    buf.sem_num = 0;
    buf.sem_op = -1;
    buf.sem_flg = SEM_UNDO;

    ret = semop(id,&buf,1);
    if(-1 == ret)
    {
        perror("semop");
        return 1;
    }

    return 0;
}

int sem_v(int id)
{
    struct sembuf buf;
    int ret;

    buf.sem_num = 0;
    buf.sem_op = 1;
    buf.sem_flg = SEM_UNDO;

    ret = semop(id,&buf,1);
    if(-1 == ret)
    {
        perror("semop");
        return 1;
    }

    return 0;
}



int main()
{
    int shmid,ret,semid;
    void *shmaddr;
    int count = 0;
    int tmp; 

    shmid = shmget(SHMKEY, SHMSIZE, 0);
    if(-1 == shmid)
    {
        perror("shmget");
        exit(1);
    }

    shmaddr = shmat(shmid,NULL,0);
    if((void *)-1 == shmaddr)
    {  
        perror("shmat");
        exit(1);
    }

    semid = semget(SEMKEY,1,0);
    if(-1 == semid)
    {
        perror("semget");
        exit(1);
    }

    *(int *)shmaddr = count;

    while(1)
    {
        sem_p(semid);
        tmp = *(int *)shmaddr;
        if(tmp > 1000)
        {
            break;
        }
        printf("Process 2 : count = %d\n",tmp);
        usleep(10);
        tmp++;
        *(int *)shmaddr = tmp;
        sem_v(semid);
    }


    ret = shmdt(shmaddr);
    if(-1 == ret)
    {
        perror("shmdt");
        exit(1);
    }


    return 0;
}

两个程序间互发信息:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>

#define MSGKEY 1234

struct msgbuf
{
    long mtype;
    char mtext[100];
};


int main()
{
    int msgid,ret;
    struct msgbuf buf;
    pid_t pid;

    msgid = msgget(MSGKEY,IPC_CREAT|IPC_EXCL);
    if(-1 == msgid)
    {
        perror("msgget");
        exit(1);
    }

    pid = fork();
    if(-1 == pid)
    {
        perror("fork");
        exit(1);
    }

    else if(0 == pid)
    {
        while(1)
        {
         memset(&buf,0,sizeof(buf));
            scanf("%s",buf.mtext);
            buf.mtype = 1;

            ret = msgsnd(msgid,&buf,sizeof(buf) - sizeof(long),0);
         if(-1 == ret)
                {
                perror("msgsnd");
                exit(1);
                }
         if(!strncmp("bye",buf.mtext,3))
                 {
                    buf.mtype = 2;
                    ret = msgsnd(msgid,&buf,sizeof(buf) - sizeof(long),0);
                    if(-1 == ret)
                    {
                        perror("msgsnd");
                        exit(1);
                    }
            break;
                }   
            }
    }   
    else
    {
        while(1)
        {
            memset(&buf,0,sizeof(buf));
            ret = msgrcv(msgid,&buf,sizeof(buf.mtext),2,0);
            if(-1 == ret)
            {
                perror("msgrcv");
                exit(1);
            }

            if(!strncmp(buf.mtext,"bye",3))
            {
                kill(pid,SIGKILL);
                break;
            }
            printf("\t\t\t%s\n",buf.mtext);
        }
        waitpid(pid,NULL,0);
    }

    msgctl(msgid, IPC_RMID, NULL);
    return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>

#define MSGKEY 1234

struct msgbuf
{
    long mtype;
    char mtext[100];
};


int main()
{
    int msgid,ret;
    struct msgbuf buf;
    pid_t pid;

   msgid = msgget(MSGKEY, 0);
    if (-1 == msgid)
    {
        perror("msgget");
        exit(1);
    }

    pid = fork();
    if(-1 == pid)
    {
        perror("fork");
        exit(1);
    }

    else if(0 == pid)
    {
        while(1)
        {
         memset(&buf,0,sizeof(buf));
            scanf("%s",buf.mtext);
            buf.mtype = 2;

            ret = msgsnd(msgid,&buf,sizeof(buf.mtext),0);
         if(-1 == ret)
                {
                perror("msgsnd");
                exit(1);
                }
         if(!strncmp("bye",buf.mtext,3))
                 {
                    buf.mtype = 1;
                    ret = msgsnd(msgid,&buf,sizeof(buf.mtext),0);
                    if(-1 == ret)
                    {
                        perror("msgsnd");
                        exit(1);
                    }
            break;
                }   
            }
    }   
    else
    {
        while(1)
        {
            memset(&buf,0,sizeof(buf));
            ret = msgrcv(msgid,&buf,sizeof(buf.mtext),1,0);
            if(-1 == ret)
            {
                perror("msgrcv");
                exit(1);
            }

            if(!strncmp(buf.mtext,"bye",3))
            {
                kill(pid,SIGKILL);
                break;
            }
            printf("\t\t\t%s\n",buf.mtext);
        }
        waitpid(pid,NULL,0);
    }


    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值