process_2.c

来自「linux下模拟进程互斥的一个小程序。」· C语言 代码 · 共 37 行

C
37
字号
/* process_2.c */#include "msgcom.h"main(){  int a,b,m,qid;  struct msgtype buf;  buf.mtype=1;  buf.text.appli_in=0;  buf.text.appli_exit=0;  qid=msgget(MSGKEY,IPC_CREAT|0666);  printf (" *Process_2 request into eritical_sect !\n" );  printf("    ------If other process using the eritical_sect ,must wait !\n ");    msgsnd ( qid, &buf, sizeof(buf.text), 0 );  msgrcv ( qid, &buf, 512, 2, MSG_NOERROR );    if ( buf.text.appli_in==1 ) {     printf ( " *The process_2 is in the eritical_sect !\n" );          /* my program */     printf ( " *Please input the vaule of a:");     scanf ( "%d",&a );     printf ( " *Please input the vaule of b:");      scanf ( "%d",&b );     m=a+b;     printf ( " *THE RESULT IS : m=a+b=%d\n",m );                printf ( " *The process_2 is request to leave eritical_sect !\n" );     buf.text.appli_exit=1;     buf.mtype=3;     msgsnd( qid, &buf, sizeof(buf.text), 0 );     msgrcv( qid, &buf, 512, 4, MSG_NOERROR );     printf ( " *The process_2 have left eritical_sect ! \n");  }  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?