• os实验例子源代码(linux)


    (注)源代码还要再修改才能运行

    头文件

    #define MSGKEY 1183
    
    struct msgform {
    	long mtype;
    	int source_pid;	
    	double a,b;
    	char opcode;
    	double result;
    	char result_msg[128];
    }msg;
    int msgsize=sizeof(struct msgform)-sizeof(long);
    int msgqid;
    

    server.c

    
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    
    #include <msg_mycs.h>
    
    main()
    {
    	int i;
    	
    	extern cleanup();
    	for(i=0;i<20;i++)
    	 signal(i,cleanup);
    
    	msgqid = msgget(MSGKEY,0777|IPC_CREAT);
    
    	for(;msg.opcode!='q';)
    	{
     	 printf("server pid= %d is ready (msgqid=%d)... 
    ",getpid(),msgqid);
    
    	 msgrcv(msgqid,&msg,msgsize,1,0);
    	 printf("server: receive from pid=%d
    ",msg.source_pid);
             msg.result_msg[0]='1';
             switch(msg.opcode){
    	 case '+':
    		msg.result=msg.a+msg.b;
    		break;
    
    	 case '-':
    		msg.result=msg.a-msg.b;
    		break;
    
    	 case '*':
    		msg.result=msg.a*msg.b;
    		break;
    	 case '/':
    		if(msg.b!=0)
    	 	 msg.result=msg.a/msg.b;
                    else
                     strcpy(msg.result_msg,"0. divide by 0.");
    		break;
    	 default:
                     strcpy(msg.result_msg,"0. exit by user.") ;
    		break;
    	 }
    
             if(msg.result_msg[0]=='1')
    		printf("%.2f  %c %.2f = %.2f
    ",msg.a,msg.opcode,msg.b,msg.result);
             else
    	    printf("%s
    ",msg.result_msg);
    
    	 msg.mtype=msg.source_pid;
    	 msg.source_pid=getpid();
    	 msgsnd(msgqid,&msg,msgsize,0);
    	}
            printf("server exit by client pid=%d
    ",msg.mtype);
    }
    cleanup()
    {
    	msgctl(msgqid,IPC_RMID,0);
    	exit(0);
    }  
    
    

    client.c

    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <msg_mycs.h>
    main()
    {
    	struct msgform msg;
    	int pid;
    	
    	msgqid = msgget(MSGKEY,0777);
            
            pid=getpid();
    	for(;msg.opcode!='q';){
             printf("a=");
    	 scanf("%lf",&msg.a);
       
             printf("b=");
    	 scanf("%lf",&msg.b);
       
             printf("opcode=(+,-,*,/,q for EXIT)");
    
    	msg.opcode=getchar();
    	while(msg.opcode=='
    ')msg.opcode=getchar();
             
            if(msg.opcode=='+'||msg.opcode=='-'||'*'==msg.opcode ||
                    '/'==msg.opcode||'q'==msg.opcode)
             { 
              msg.source_pid=pid;
              msg.mtype=1;
    	 
    	  msg.result_msg[0]=0;
    	  msgsnd(msgqid,&msg,msgsize,0);
    	  msgrcv(msgqid,&msg,msgsize,pid,0);
    	  
    	  printf("client: receive from pid=%d
    ",msg.source_pid);
             
              if(msg.result_msg[0]=='1')
    		printf("%.2f  %c %.2f = %.2f
    ",msg.a,msg.opcode,msg.b,msg.result);
    	  else
    	    printf("%s
    ",msg.result_msg);
             
    
             }
            }
    }
      
    
  • 相关阅读:
    GRIDVIEW导出到EXCEL
    数据表示:字节 高位低位
    js学习笔记0
    12奇招,循序删除顽固的文件
    加快开关机速度
    PHP正则表达式的快速学习
    firefox下height不能自动撑开的解决办法
    给MySQL加密(也适用于Wamp5中)
    我的电脑创建资源管理器
    css 圆角7种CSS圆角框解决方案
  • 原文地址:https://www.cnblogs.com/yangminting/p/11950271.html
Copyright © 2020-2023  润新知