⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test15_a.c

📁 进程间调度
💻 C
字号:
#include "test15.h"char * fun_itoa(int i);int main(int argc, char *argv[]){ int i, sta; long N; int pid, shm_id; struct PInfo *shm_addr; if(argc != 2)   {    printf("agrc error!!!\n");    exit(0);   } N = atol(argv[1]); if(N < 4)             /*要求输入的N值>=4*/   {    printf("the N  should >= 4\n");    exit(0);   } shm_id = shmget(SHM_KEY, sizeof(struct PInfo)*5, IPC_CREAT|0600); if(shm_id == -1)       /*创建共享内存,并获得共享内存的id*/   {    printf("shmget faild in main() !!!\n");    exit(0);   } shm_addr = shmat(shm_id, 0, 0); if(!shm_addr)         /*求和进程与共享内存相连,并得到首地址*/   {     printf("shmat faild in main() !!!\n");    exit(0);   }  shm_addr[0].pstatus = 'X';     /*对共享内存中各进程的状态位初始化*/ shm_addr[1].pstatus = 'X'; shm_addr[2].pstatus = 'X'; shm_addr[3].pstatus = 'X'; shm_addr[4].pstatus = 'X'; if((pid=fork()) == -1)   {    printf("fork faild !!!\n");    exit(0);   } if(!pid)      /*创建子进程,用于从消息队列获取消息,重新创建求和进程或并行计算进程*/     execl("/root/test15_b", "test15_b", argv[1], (char *)0);    if((pid=fork()) == -1)   {    printf("fork faild in main() !!!\n");    exit(0);   } if(!pid)      /*创建求和进程*/    execl("/root/test15_c", "test15_c", argv[1], (char *)0); for(i=1; i<=4; i++)         /*创建四个并行计算进程*/    {     pid = fork();     switch(pid)           {            case -1:                    printf("fork faild in main() !!!\n");                    exit(0);            case  0:                    execl("/root/test15_d", "test15_d", fun_itoa(i), (char *)0);                    break;            default:                    ;           }           } if(shmdt(shm_addr) == -1)     /*父进程与共享内存脱离*/    printf("shmdt faild in main() !!!\n");}/*char * fun_itoa(int i)返回值:char型指针,即字符串参数:i    int型变量函数功能:得到int变量的字符串形式。*/char * fun_itoa(int i){ switch(i)       {        case 1: return "1";        case 2: return "2";        case 3: return "3";        case 4: return "4";       }}

⌨️ 快捷键说明

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