您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 操作系统实验_示例代码_linux
《计算机操作系统》实验指导苏州科技学院电子与信息工程系软件教研室二OO二年九月1示例代码第一部分LINUX操作系统平台实验一命令解释程序示例程序minishell.c//文件名minishell.cpp//功能小型SHELL命令解释程序//开发环境#definetrue1#defineflase0#includestdio.h#includestring.h#includestdlib.hvoidmain(){charcmdl[80];char*scwt[]={exit,dir,time};staticintcmdnum=3;//可用的命令数charcmd[80];intj,n;while(true){printf(Pleaseinputcommand:);gets(cmdl);//取命令行输入n=strcspn(cmdl,);//取命令命令部分if(n0||strlen(cmdl)0){strncpy(cmd,cmdl,n);cmd[n]='\0';for(j=0;jcmdnum;j++)if(strcmp(cmd,scwt[j])==0)break;if(j==0)//是exit命令?exit(0);if(jcmdnum)//其他合法命令{2system(cmdl);continue;}printf(Badcommand!\n);//命令错}}}实验二进程管理fork()调用示例#includestdio.hmain(){intp1,p2;while((p1=fork())==-1);if(p1==0)//是子进程?putchar('b');else//父进程{putchar('a');}}实验三进程间通信1)pipe()调用示例#includestdio.hmain(){intid,fd[2];charbuf[50],s[50];pipe(fd);while((id=fork())==-1);if(id==0){sprintf(buf,Childissendingmessage!);write(fd[1],buf,50);exit(0);}else{wait(0);3read(fd[0],s,50);printf(%s\n,s);exit(0);}}2)共享存储器示例shm_sample.c#includestdio.h#includesys/types.h#includesys/ipc.h#includesys/shm.h#defineSEGSIZE100main(intargc,char*argv[]){key_tkey;intshmid,cntr;char*segptr;if(argc==1)usage();/*Createuniquekeyviacalltoftok()*/key=ftok(.,'S');/*Openthesharedmemorysegment-createifnecessary*/if((shmid=shmget(key,SEGSIZE,IPC_CREAT|IPC_EXCL|0666))==-1){printf(Sharedmemorysegmentexists-openingasclient\n);/*Segmentprobablyalreadyexists-tryasaclient*/if((shmid=shmget(key,SEGSIZE,0))==-1){perror(shmget);exit(1);}}else{printf(Creatingnewsharedmemorysegment\n);}/*Attach(map)thesharedmemorysegmentintothecurrentprocess*/if((segptr=shmat(shmid,0,0))==-1){perror(shmat);exit(1);}switch(tolower(argv[1][0])){case'w':writeshm(shmid,segptr,argv[2]);break;case'r':readshm(shmid,segptr);break;4case'd':removeshm(shmid);break;case'm':changemode(shmid,argv[2]);break;default:usage();}}writeshm(intshmid,char*segptr,char*text){strcpy(segptr,text);printf(Done...\n);}readshm(intshmid,char*segptr){printf(segptr:%s\n,segptr);}removeshm(intshmid){shmctl(shmid,IPC_RMID,0);printf(Sharedmemorysegmentmarkedfordeletion\n);}changemode(intshmid,char*mode){structshmid_dsmyshmds;/*Getcurrentvaluesforinternaldatastructure*/shmctl(shmid,IPC_STAT,&myshmds);/*Displayoldpermissions*/printf(Oldpermissionswere:%o\n,myshmds.shm_perm.mode);/*Convertandloadthemode*/sscanf(mode,%o,&myshmds.shm_perm.mode);/*Updatethemode*/shmctl(shmid,IPC_SET,&myshmds);printf(Newpermissionsare:%o\n,myshmds.shm_perm.mode);}usage(){fprintf(stderr,shm_sample-Autilityfortinkeringwithsharedmemory\n);fprintf(stderr,\nUSAGE:shmtool(w)ritetext\n);fprintf(stderr,(r)ead\n);fprintf(stderr,(d)elete\n);fprintf(stderr,(m)odechangeoctalmode\n);exit(1);}53)消息队列使用示例(1)message.h/*message.h*/#ifndefMESSAGE_H#defineMESSAGE_Hstructmymsgbuf{longmtype;charmtext[256];};#endif(2)msg_server.c/*msg_server.c*/#includestdio.h#includestdlib.h#includestring.h#includesignal.h#includesys/msg.h#includesys/ipc.h#includemessage.hintmsqid=-1;voidsig_handle(intsigno){/*软中断*/if(msqid!=-1)msgctl(msqid,IPC_RMID,NULL);printf(serverquit...\n);exit(0);}intmain(){structmymsgbufmsgbuf;intleft,right;charc;intlength;if((msqid=msgget(999,0666))!=-1){msgctl(msqid,IPC_RMID,NULL);}if((msqid=msgget(999,IPC_CREAT|0666))==-1){6printf(error:getmsg\n);exit(1);}signal(SIGINT,sig_handle);/*LINUX置软中断—CTRL-D*/for(;;){if(msgrcv(msqid,&msgbuf,256,1L,0)==-1){printf(error:msgrcv\n);exit(1);}length=strlen(msgbuf.mtext);left=0;right=length-1;while(leftright){c=msgbuf.mtext[left];msgbuf.mtext[left]=msgbuf.mtext[right];msgbuf.mtext[right]=c;left++;right--;}msgbuf.mtext[length]='\0';msgbuf.mtype=2;if(msgsnd(msqid,&msgbuf,256,0)==-1){printf(error:msgsnd);exit(1);}}msgctl(msqid,IPC_RMID,NULL);exit(0);}(3)msg_client.c/*msg_client.c*/#includestdio.h#includestdlib.h#includestring.h#includesys/msg.h#includesys/ipc.h#includemessage.hintmain(){structmymsgbufmsgbuf;7intmsqid;if((msqid=msgget(999,0666))==-1){printf(Serverisnotrunning\n);exit(1);}printf(Inputaline:);scanf(%s,msgbuf.mtext);msgbuf.mtype=1;if(msgsnd(msqid,&msgbuf,256,0)==-1){printf(error:msgsnd\n);exit(1);}if(msgrcv(msqid,&msgbuf,256,2L,0)==-1){printf(error:msgrcv\n);exit(1);}printf(Thereversedlineis:%s\n,msgbuf.mtext);exit(0);}实验四存储管理示例程序(注意阅读并分析可能存在的问题)#defineTRUE1#defineFALSE0#defineINVALID-1#definenull0#definetotal_instruction320/*指令流长*/#definetotal_vp32/*虚页长*/#defineclear_period50/*清零周期*/typedefstruct{/*页面结构*/intpn,pfn,counter,time;}pl_type;pl_typepl[total_vp];/*页面数组*/structpfc_struct{/*页面控制结构*/intpn,pfn;structpfc_struct*next;8};typedefstructpfc_structpfc_type;pfc_typepfc[total_vp],*freepf_head,*busypf_head,*busypf_tail;intdiseffect,a[total_instruction];intpage[total_instruction],offset[total_instruction];voidinitialize(inttotal_pf);voidFIFO(inttotal_pf);voidLRU(inttotal_pf);voidOPT(inttotal_pf);voidLFU(inttotal_pf);voidNUR(inttotal_pf);#includestdlib.h#includestdio.h#includetime.hmain(){intS,i;srand((unsigned)time(NULL));/*srand(getpid()*10);*//*由于每次运行时进程号不同,故可用来作为初始化随机数的种子*/S=(float)319*rand()/32767+1;for(i=0;it
本文标题:操作系统实验_示例代码_linux
链接地址:https://www.777doc.com/doc-2381204 .html