进程间通信的代码示例

无名管道

#include<myhead.h>

int main(int argc, const char *argv[])
{
	int pipefd[2] = {0};
	char p[128]={0};
	pipe(pipefd);//创建无名官道
	pid_t pid = -1;
	pid = fork();
	if(pid >0)
	{
		//父进程
		close(pipefd[0]);//关闭读端
		printf("请输入:\n");
		fgets(p,sizeof(p),stdin);
		p[strlen(p)-1]='\0';
		write(pipefd[1],p,sizeof(p));
		close(pipefd[1]);
	}
	else if(pid == 0)
	{
		//子进程
		close(pipefd[1]);//关闭写段

		read(pipefd[0],p,sizeof(p));
		printf("%s\n",p);
		close(pipefd[0]);
		exit(EXIT_SUCCESS);
	}
	wait(NULL);
	return 0;
}

共享内存

#include<myhead.h>

#define PAGE_SIZE 4096


int main(int argc, const char *argv[])
{
    key_t key = ftok("/", 't');
    if(key == -1)
    {
        perror("ftok error");
        return -1;
    }
    printf("key = %#x\n", key);

    int shmid = shmget(key, PAGE_SIZE, IPC_CREAT|0664);
    if(shmid == -1)
    {
        perror("shmget error");
        return -1;
    }
    printf("shmid = %d\n", shmid);

    char *addr = (char *)shmat(shmid, NULL, 0);
    if(addr == (void*)-1 )
    {
        perror("shmat error");
        return -1;
    }       
    while(1)
    {
        printf("请输入>>>>");
        fgets(addr, PAGE_SIZE, stdin);    
        addr[strlen(addr)-1] = 0;           
        if(strcmp(addr, "quit") == 0)
        {
            break;
        }
    }
    
    if(shmdt(addr) == -1)
    {
        perror("shmdt error");
        return -1;
    }


    return 0;
}
#include<myhead.h>

#define PAGE_SIZE 4096


int main(int argc, const char *argv[])
{
    key_t key = ftok("/", 't');
    if(key == -1)
    {
        perror("ftok error");
        return -1;
    }
    int shmid = shmget(key, PAGE_SIZE, IPC_CREAT|0664);
    if(shmid == -1)
    {
        perror("shmget error");
        return -1;
    }
    char *addr = (char *)shmat(shmid, NULL, 0);
    if(addr == (void*)-1 )
    {
        perror("shmat error");
        return -1;
    }
    printf("addr = %p\n", addr);      
    while(1)
    {
        printf("共享内存中的数据为: %s\n", addr);
        if(strcmp(addr, "quit") == 0)
        {
            break;
        }
    }
    if(shmdt(addr) == -1)
    {
        perror("shmdt error");
        return -1;
    }
    if(shmctl(shmid, IPC_RMID, NULL) == -1)
    {
        perror("shmctl error");
        return -1;
    }

    return 0;
}

 消息队列

#include<myhead.h>
struct msgbuf
{
    long mtype;         //消息类型
    char mtext[1024];    //消息正文大小
};
#define SIZE (sizeof(struct msgbuf)-sizeof(long))
nt main(int argc, const char *argv[])
{
    key_t key = ftok("/", 'k');
    if(key == -1)
    {
        perror("key create error");
        return -1;
    }
    printf("key = %#x\n", key);
    int msgid = msgget(key, IPC_CREAT|0664);
    if(msgid == -1)
    {
        perror("msgget error");
        return -1;
    }
    struct msgbuf buf;
    while(1)
    {
        printf("请输入您要发送的类型:");
        scanf("%ld", &buf.mtype);
        printf("请输入消息内容:");
        scanf("%s", buf.mtext);
        msgsnd(msgid, &buf, SIZE, 0);
        if(strcmp(buf.mtext, "quit") == 0)
        {
            break;
        }
    }
    if(msgctl(msgid, IPC_RMID, NULL) == -1)
    {
        perror("msgctl error");
        return -1;
    }

    return 0;
}
#include<myhead.h>
struct msgbuf
{
    long mtype;         //消息类型
    char mtext[1024];    //消息正文大小
};
#define SIZE (sizeof(struct msgbuf)-sizeof(long))
int main(int argc, const char *argv[])
{
    key_t key = ftok("/", 'k');
    if(key == -1)
    {
        perror("key create error");
        return -1;
    }
    int msgid = msgget(key, IPC_CREAT|0664);
    if(msgid == -1)
    {
        perror("msgget error");
        return -1;
    }
    printf("msgid = %d\n", msgid);       
    struct msgbuf buf;
    while(1)
    {
        msgrcv(msgid, &buf, SIZE, 0, 0);
        printf("收到消息:%s\n", buf.mtext);
        if(strcmp(buf.mtext, "quit") == 0)
        {
            break;
        }
    }

    return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值