#include "sys/types.h" #include "sys/file.h" #include "unistd.h" char r_buf[4]; //读缓冲 char w_buf[4]; //写缓冲 int pipe_fd[2]; pid_t pid1, pid2, pid3, pid4; int producer(int id); int consumer(int id); int main(int argc,char **argv) { if(pipe(pipe_fd)<0) { printf("pipe create error "); exit(-1); } else { printf("pipe is created successfully! "); if((pid1=fork())==0) producer(1); if((pid2=fork())==0) producer(2); if((pid3=fork())==0) consumer(1); if((pid4=fork())==0) consumer(2); } close(pipe_fd[0]); //需要加上这两句 close(pipe_fd[1]); //否这会有读者或者写者永远等待 int i,pid,status; for(i=0;i<4;i++) pid=wait(&status); exit(0); } int producer(int id) { printf("producer %d is running! ",id); close(pipe_fd[0]); int i=0; for(i=1;i<10;i++) { sleep(3); if(id==1) //生产者1 strcpy(w_buf,"aaa "); else //生产者2 strcpy(w_buf,"bbb "); if(write(pipe_fd[1],w_buf,4)==-1) printf("write to pipe error "); } close(pipe_fd[1]); printf("producer %d is over! ",id); exit(id); } int consumer(int id) { close(pipe_fd[1]); printf("producer %d is running! ",id); if (id==1) //消费者1 strcpy(w_buf,"ccc "); else //消费者2 strcpy(w_buf,"ddd "); while(1) { sleep(1); strcpy(r_buf,"eee "); if(read(pipe_fd[0],r_buf,4)==0) break; printf("consumer %d get %s, while the w_buf is %s ",id,r_buf,w_buf); } close(pipe_fd[0]); printf("consumer %d is over! ", id); exit(id); } #include "sched.h" #include "pthread.h" #include "stdio.h" #include "stdlib.h" #include "semaphore.h" int producer(void * args); int consumer(void *args); pthread_mutex_t mutex; sem_t product; sem_t warehouse; char buffer[8][4]; int bp=0; main(int argc,char** argv) { pthread_mutex_init(&mutex,NULL); sem_init(&product,0,0); sem_init(&warehouse,0,8); int clone_flag,arg,retval; char *stack; clone_flag=CLONE_VM|CLONE_SIGNAND|CLONE_FS| CLONE_FILES; int i; for(i=0;i<2;i++) { //创建四个线程 arg = i; stack =(char*)malloc(4096); retval=clone((void*)producer,&(stack[4095]),clone_flag, (void*)&arg); stack =(char*)malloc(4096); retval=clone((void*)consumer,&(stack[4095]),clone_flag, (void*)&arg); } exit(1); } int producer(void* args) { int id = *((int*)args); int i; for(i=0;i<10;i++) { sleep(i+1); //表现线程速度差别 sem_wait(&warehouse); pthread_mutex_lock(&mutex); if(id==0) strcpy(buffer[bp],"aaa "); else strcpy(buffer[bp],"bbb "); bp++; printf("producer%d produce %s in %d ",id,buffer[bp],bp-1); pthread_mutex_unlock(&mutex); sem_post(&product); } printf("producer%d is over! ",id); } int consumer(void *args) { int id = *((int*)args); int i; for(i=0;i<10;i++) { sleep(10-i); //表现线程速度差别 sem_wait(&product); pthread_mutex_lock(&mutex); bp--; printf("consumer%d get %s in%d ",id,buffer[bp],bp+1); strcpy(buffer[bp],"zzz "); pthread_mutex_unlock(&mutex); sem_post(&warehouse); } printf("consumer%d is over! ",id); } #include "math.h" #include "sched.h" #include "pthread.h" #include "stdlib.h" #include "semaphore.h" typedef struct{ //实时任务描述 char task_id; int call_num; //任务发生次数 int ci; // Ci int ti; //Ti int ci_left; int ti_left; int flag; //任务是否活跃,0否,2是 int arg; //参数 pthread_t th; //任务对应线程 }task; void proc(int* args); void* idle(); int select_proc(); int task_num = 0; int idle_num = 0; int alg; //所选算法,1 for EDF,2 for RMS int curr_proc=-1; int demo_time = 100; //演示时间 task* tasks; pthread_mutex_t proc_wait[100]; pthread_mutex_t main_wait, idle_wait; float sum=0; pthread_t idle_proc; int main(int argc,char** argv) { pthread_mutex_init(&main_wait,NULL); pthread_mutex_lock(&main_wait); //下次执行lock等待 pthread_mutex_init(&idle_wait,NULL); pthread_mutex_lock(&idle_wait); //下次执行lock等待 printf("Please input number of real time tasks: "); scanf("%d",&task_num); tasks = (task*)malloc(task_num*sizeof(task)); int i; for(i=0;i<task_num;i++) { pthread_mutex_init(&proc_wait[i],NULL); pthread_mutex_lock(&proc_wait[i]); } for(i=0;i<task_num;i++) { printf("Please input task id, followed by Ci and Ti: "); scanf("%c,%d,%d,",&tasks[i].task_id,&tasks[i].ci,&tasks[i].ti); tasks[i].ci_left=tasks[i].ci; tasks[i].ti_left=tasks[i].ti; tasks[i].flag=2; tasks[i].arg=i; tasks[i].call_num=1; sum=sum+(float)tasks[i].ci/(float)tasks[i].ti; } printf("Please input algorithm, 1 for EDF, 2 for RMS:"); scanf("%d",&alg); printf("Please input demo time:"); scanf("%d",&demo_time); double r=1; //EDF算法 if(alg==2) { //RMS算法 r=((double)task_num)*(exp(log(2)/(double)task_num)-1); printf("r is %lf ",r); } if(sum>r) { //不可调度 printf("(sum=%lf > r=%lf) ,not schedulable! ",sum,r); exit(2); } pthread_create(&idle_proc,NULL,(void*)idle,NULL); //创建闲逛线程 for(i=0;i<task_num;i++) //创建实时任务线程 pthread_create(&tasks[i].th,NULL,(void*)proc,&tasks[i].arg); for(i=0;i<demo_time;i++) { int j; if((curr_proc=select_proc(alg))!=-1) { //按调度算法选线程 pthread_mutex_unlock(&proc_wait[curr_proc]); //唤醒 pthread_mutex_lock(&main_wait); //主线程等待 } else { //无可运行任务,选择闲逛线程 pthread_mutex_unlock(&idle_wait); pthread_mutex_lock(&main_wait); } for(j=0;j<task_num;j++) { //Ti--,为0时开始下一周期 if(--tasks[j].ti_left==0) { tasks[j].ti_left=tasks[j].ti; tasks[j].ci_left=tasks[j].ci; pthread_create(&tasks[j].th,NULL,(void*)proc,&tasks[j].arg); tasks[j].flag=2; } } } printf(" "); sleep(10); }; void proc(int* args) { while(tasks[*args].ci_left>0) { pthread_mutex_lock(&proc_wait[*args]); //等待被调度 if(idle_num!=0) { printf("idle(%d)",idle_num); idle_num=0; } printf("%c%d",tasks[*args].task_id,tasks[*args].call_num); tasks[*args].ci_left--; //执行一个时间单位 if(tasks[*args].ci_left==0) { printf("(%d)",tasks[*args].ci); tasks[*args].flag=0; tasks[*args].call_num++; } pthread_mutex_unlock(&main_wait); //唤醒主线程 } }; void* idle() { while(1) { pthread_mutex_lock(&idle_wait); //等待被调度 printf("->"); //空耗一个时间单位 idle_num++; pthread_mutex_unlock(&main_wait); //唤醒主控线程 } }; int select_proc(int alg) { int j; int temp1,temp2; temp1=10000; temp2=-1; if((alg==2)&&(curr_proc!=-1)&&(tasks[curr_proc].flag!=0)) return curr_proc; for(j=0;j<task_num;j++) { if(tasks[j].flag==2) { switch(alg) { case 1: //EDF算法 if(temp1>tasks[j].ci_left) { temp1=tasks[j].ci_left; temp2=j; } case 2: //RMS算法 if(temp1>tasks[j].ti) { temp1=tasks[j].ti; temp2=j; } } } } return temp2; } #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <stdio.h> #include <stdlib.h> #define MAPSIZE 100 struct map //存储资源表结构 { int m_addr; int m_size; }; struct map map[MAPSIZE]; //存储资源表 //BF存储分配函数 int BF_malloc(struct map *mp,int size) { register int a,s; register struct map *bp,*bpp; for(bp = mp; bp->m_size; bp++) { if (bp->m_size >= size) { a = bp->m_addr; s = bp->m_size; for(bpp = bp; bpp->m_size; bpp++) { //最佳适应 if(bpp->m_size >= size && bpp->m_size < s) { a = bpp->m_addr; s = bpp->m_size; bp = bpp; } } bp->m_addr += size; if ((bp->m_size -= size) == 0) do { bp++; (bp-1)->m_addr = bp->m_addr; } while((bp-1)->m_size = bp->m_size); return(a); } } return(-1); } //WF存储分配函数 int WF_malloc(struct map *mp,int size) { register int a,s; register struct map *bp,*bpp; for(bp = mp; bp->m_size; bp++) { if (bp->m_size >= size) { a = bp->m_addr; s = bp->m_size; for(bpp = bp; bpp->m_size; bpp++) { //最坏适应 if(bpp->m_size > s) { a = bpp->m_addr; s = bpp->m_size; bp = bpp; } } bp->m_addr += size; if ((bp->m_size -=size) == 0) do { bp++; (bp-1)->m_addr = bp->m_addr; } while((bp-1)->m_size = bp->m_size); return(a); } } return(-1); } //存储释放函数 void mfree(struct map *mp,int aa,int size) { register struct map *bp; register int t; register int a; a = aa; for(bp = mp; bp->m_addr<=a && bp->m_size != 0; bp++) ; if(bp>mp && (bp-1)->m_addr+(bp-1)->m_size==a) { //与前合并 (bp-1)->m_size += size; if (a+size == bp->m_addr) { //前后合并 (bp-1)->m_size += bp->m_size; while (bp->m_size) { bp++; (bp-1)->m_addr = bp->m_addr; (bp-1)->m_size = bp->m_size; } } } else { if (a+size == bp->m_addr && bp->m_size) { //与后合并 bp->m_addr -= size; bp->m_size += size; } else if (size) do { //无合并 t = bp->m_addr; bp->m_addr = a; a = t; t = bp->m_size; bp->m_size = size; bp++; } while (size = t); } } void init() { struct map *bp; int addr,size; int i=0; bp=map; printf("Please input starting addr and total size:"); scanf("%d,%d",&addr,&size); bp->m_addr=addr; bp->m_size=size; (++bp)->m_size=0; //表尾 } void show_map() { int i=0; //system("clear"); //清屏 struct map *bp; bp=map; printf(" Current memory map... "); printf("Address Size "); while(bp->m_size!=0) { printf("<%d %d> ",bp->m_addr,bp->m_size); bp++; } printf(" "); } main() { int a,s; int c; int i; init(); printf("please input, b for BF, w for WF:"); scanf("%c",&c); do { show_map(); //显示存储资源表 printf(“please input, b for BF, w for WF, e for exit); printf("Please input,1 for request,2 for release,0 for exit:"); scanf("%d",&c); switch(i) { case 1: printf("Please input size:"); scanf("%d", &s); if(c==’b’) //BF a=BF_malloc(map,s)) else //WF a=WF_malloc(map,s)) if(a==-1) printf("request can't be satisfied "); else printf("alloc memory at address:%d,size:%d ",a,s); break; case 2: printf("Please input addr and size:"); scanf("%d,%d",&a,&s); mfree(map,a,s); break; case 0: exit(0); } } while(1); }