模拟实现进程创建函数process_create

利用fork()、wait()等函数模拟实现进程创建函数process_create()函数

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

typedef void *(*FUNC)(void *); 

typedef struct Arg{
  int reg;
} Arg;

void* func(void *parameter){
  (void)parameter;
  printf("haha\n");
  return NULL;
}

void process_create(pid_t *pid,void *func,void *arg){
  int id = fork();
  if(id < 0){ 
    perror("fork:");
        return ;
  }else if(id == 0){
    //child
   FUNC funcback = (FUNC)func;
   funcback((Arg *)arg);
   exit(0);
  }else{
    //father
   *pid = wait(NULL);
  }
}


int main(){
  pid_t pid;
  Arg arg;
  process_create(&pid,func,&arg);
  printf("pid = %d\n",pid);
  return 0;
}

结果:

[DELL@localhost lesson6]$ ./process_create 
haha
pid = 6852
[DELL@localhost lesson6]$ 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<stdio.h> #include "dos.h" #include "stdlib.h" #include "conio.h" //PCB结构体 struct pcb{ int id; //进程序号 int ra; //所需资源A的数量 int rb; //所需资源B的数量 int rc; //所需资源C的数量 int ntime; //所需的时间片个数 int rtime; //已经运行的时间片个数 char state; //进程状态 struct pcb *next; } *hready=NULL,*hblock=NULL,*p; //hready,hblock分别为指向就绪和阻塞队列 typedef struct pcb PCB; int m,n,r,a,b,c,h=0,i=1,time1Inteval; //m为要模拟进程个数,n为初始化进程个数 //r为可随机产生的进程数(r=m-n) //a,b,c分别为A,B,C三类资源的总量 //i为进城计数,i=1…n //h为运行的时间片次数,time1Inteval为时间片大小(毫秒) //建立一个PCB结构体型的空链表 PCB *increat(void) { PCB *head=NULL; //head=NULL; return(head); } //从链表起始地址开始输出该链表的内容 void disp(PCB *head) {PCB *p1; p1=head; AnsiString str2; if(head!=NULL) //链表非空 { do { str2+=" "; str2+=IntToStr(p1->id);str2+=" "; str2+=(p1->state);str2+=" "; str2+=IntToStr(p1->ra);str2+=" "; str2+=IntToStr(p1->rb);str2+=" "; str2+=IntToStr(p1->rc);str2+=" "; str2+=IntToStr(p1->ntime);str2+=" "; str2+=IntToStr(p1->rtime);str2+="\r\n"; p1=p1->next; }while(p1!=NULL); //不断输出进程的信息,直到链尾! } //if else { str2+="\t\t该 队 列 中 没 有 进 程!\r\n" ;} Form1->Memo1->Lines->Add(str2); } //将进程插入到链尾(包括就绪队列和阻塞队列) PCB *insert(PCB *head,PCB*pcb) //带两个指针形参:队列指针和当前进程PCB { PCB *pi,*p1; p1=head; pi=pcb; if (head==NULL) { head=pi; pi->next=NULL; } else { while(p1->next!=NULL) {p1=p1->next;} p1->next=pi; pi->next=NULL; } return(head); } //对进程进行初始化,建立就绪队阻塞队列。 void input() { AnsiString str1; m=StrToInt (Form1->Edit1->Text); //读取要模拟进程总数给m n=StrToInt (Form1->Edit2->Text); //读取需初
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值