IO进程线程 2月8日作业

1.

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#include <signal.h>
struct msgbuf
{
    long mtype; //消息类型,必须 > 0
    char mtext[128]; //消息内容
};

void handler(int sig)
{
    while (waitpid(-1,NULL,WNOHANG) > 0); }
 
 
int main(int argc, char const *argv[])
{
    //捕获17号信号
    __sighandler_t s = signal(SIGCHLD,handler);
    if(SIG_ERR == s)
    {
        perror("signal");
        return -1;
    }
    //
    //计算Key
    key_t key = ftok("/home/ubuntu/",1);
    if (key < 0)
    {
        perror("ftok");
        return -1;
    }
    printf("key = %#x\n",key);
 
    //消息队列创建
    int msqid = msgget(key,IPC_CREAT|0664);
    if (msqid < 0)
    {
        perror("msgget");
        return -1;
    }
 
    printf("msqid = %d\n",msqid);
 
    system("ipcs -q");
 
    //创建两个进程,一个进程往消息队列中发送数据,一个进程从队列中写数据
 
    
    pid_t cpid = fork();
 
    if(cpid > 0)
    {
        //子进程从msgtype为101的进程中读取数据
        struct msgbuf rcv;
        
        while (1)
        {
            memset(&rcv,0,sizeof(rcv));
            rcv.mtype = 101; //消息类型
            //从消息队列中读取数据
            //阻塞的方式,读取消息队列中的第一条消息
            if(msgrcv(msqid,&rcv,sizeof(rcv.mtext),101,0) < 0)
            //if( msgrcv(msqid,&rcv,sizeof(rcv.mtext),101,IPC_NOWAIT) < 0)
            {
                    perror("msgrcv");
                    return -1;
            }
 
            printf("A==读取到的消息内容:%s\n",rcv.mtext);
 
            if (strcasecmp(rcv.mtext,"quit")==0)
            {
                //父进程退出前回收子进程
                kill(cpid,9);
                break;
            }
        }
        
    } else if(0==cpid)
    {
        struct msgbuf snd;
        //父进程向msgtype为100的进程中发送数据
		while (1)
		{
			memset(&snd,0,sizeof(snd));
			snd.mtype = 100; //消息类型
			//从终端获取消息内容
			//printf("A==清输入消息内容>>>");
			fgets(snd.mtext,sizeof(snd.mtext),stdin);
			snd.mtext[strlen(snd.mtext)-1] = 0;
			//向消息队列中发送数据
			if( msgsnd(msqid,&snd,sizeof(snd.mtext),0) < 0)
			{
				perror("msgsnd");
				return -1;
			}
			if (strcasecmp(snd.mtext,"quit")==0)
			{

				break;
			}
		}
	} else 
	{
		perror("fork");
		return -1;
}
        return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#include <signal.h>
struct msgbuf
{
	long mtype; //消息类型,必须 > 0
	char mtext[128]; //消息内容
};

//回收僵尸进程
void handler(int sig)
{
	while (waitpid(-1,NULL,WNOHANG) > 0); //此处WNOHANG不能填0
}




int main(int argc, char const *argv[])
{
	//捕获17号信号
	__sighandler_t s = signal(SIGCHLD,handler);
	if(SIG_ERR == s)
	{
		perror("signal");
		return -1;
	}

	//计算Key
	key_t key = ftok("/home/ubuntu/",1);
	if (key < 0)
	{
		perror("ftok");
		return -1;
	}

	printf("key = %#x\n",key);

	//消息队列创建
	int msqid = msgget(key,IPC_CREAT|0664);
	if (msqid < 0)
	{
		perror("msgget");
		return -1;
	}

	printf("msqid = %d\n",msqid);

	system("ipcs -q");

	//创建两个进程,一个进程往消息队列中发送数据,一个进程从队列中写数据
	pid_t cpid = fork();

	if(cpid > 0)
	{
		//子进程向从msgtype为100的进程中读取数据
		struct msgbuf rcv;
		while (1)
		{
			memset(&rcv,0,sizeof(rcv));
			rcv.mtype = 100; //消息类型
			//从消息队列中读取数据
			//阻塞的方式,读取消息队列中的第一条消息
			if(msgrcv(msqid,&rcv,sizeof(rcv.mtext),100,0) < 0)
				//if( msgrcv(msqid,&rcv,sizeof(rcv.mtext),100,IPC_NOWAIT) < 0)
			{
				perror("msgrcv");
				return -1;
			}
			printf("B==读取到的消息内容:%s\n",rcv.mtext);
			if (strcasecmp(rcv.mtext,"quit")==0)
			{
				kill(cpid,9);
				break;
			}
		}

	} else if(0==cpid)
	{
		//父进程向msgtype为101的消息队列发送数据
		struct msgbuf snd;
		//父进程向msgtype为100的进程中发送数据
		while (1)
		{
			memset(&snd,0,sizeof(snd));
			snd.mtype = 101; //消息类型
			//从终端获取消息内容
			//printf("B==清输入消息内容>>>");
			fgets(snd.mtext,sizeof(snd.mtext),stdin);
			snd.mtext[strlen(snd.mtext)-1] = 0;
            //向消息队列中发送数据
            if( msgsnd(msqid,&snd,sizeof(snd.mtext),0) < 0)
            {
                perror("msgsnd");
                return -1;
            }
 
            if (strcasecmp(snd.mtext,"quit")==0)
            {
                
                break;
            }
        }
       
    } else {
        perror("fork");
        return -1;
    }
 
    return 0;
}

结果:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值