您好,欢迎访问三七文档
操作系统实验报告161220323陈心悦一:多进程实验1.mysystem.c实验内容:实现system.c的功能,要求功能与system函数相同。代码如下:#includefcntl.h#includestdio.h#includesys/stat.h#includestring.h#includeunistd.hstructcommand{intpipe;intflag;charargv[100];charinfile[50];charoutfile[50];};structcommand*mycommand;intp;voidpath(char*cmdstring){inti=0,j=0,k=0;p=0;mycommand[p].pipe=1;mycommand[p].flag=0;for(i=0;cmdstring[i]!='\0';){if(cmdstring[i]==''){k=0;i++;mycommand[p].flag=1;while(cmdstring[i]=='')i++;while(((cmdstring[i]='0')&&(cmdstring[i]='9'))||((cmdstring[i]='A')&&(cmdstring[i]='p'))||cmdstring[i]=='.'||cmdstring[i]=='_'){mycommand[p].infile[k++]=cmdstring[i++];}}elseif(cmdstring[i]==''){k=0;i++;mycommand[p].flag=2;while(cmdstring[i]=='')i++;while(((cmdstring[i]='0')&&(cmdstring[i]='9'))||((cmdstring[i]='A')&&(cmdstring[i]='p'))||cmdstring[i]=='.'||cmdstring[i]=='_'){mycommand[p].outfile[k++]=cmdstring[i++];}}elseif(cmdstring[i]=='|'){p++;j=0;i++;}elsemycommand[p].argv[j++]=cmdstring[i++];}if(p==0)mycommand[p].pipe=0;return;}voidmysystem(intnum){char*prog_argv[1024];intcount,fd;count=0;char*pstr=strtok(mycommand[num].argv,);while(pstr!=NULL){prog_argv[count++]=pstr;pstr=strtok(NULL,);}prog_argv[count]=NULL;if(mycommand[num].flag==2){fd=open(mycommand[num].outfile,O_CREAT|O_RDWR|O_TRUNC,0666);if(fd0){printf(openerror!\n);exit(0);}if(dup2(fd,1)0){printf(errindup2\n);exit(0);}close(fd);}elseif(mycommand[num].flag==1){fd=open(mycommand[num].infile,O_CREAT|O_RDWR,0666);if(fd0){printf(openerror!\n);exit(0);}if(dup2(fd,0)0){printf(errindup2\n);exit(0);}close(fd);}execvp(prog_argv[0],prog_argv);}intmain(){intpid,pid1,i=0;charcmdstring[128]={'\0'};chartemp[128];intpfd[2];while(gets(cmdstring)){mycommand=(structcommand*)malloc(sizeof(structcommand)*2);path(cmdstring);if(strcmp(mycommand[0].argv,exit)==0)exit(0);elseif((mycommand[0].argv[0]=='c')&&(mycommand[0].argv[1]=='d')){for(i=0;mycommand[0].argv[i+3]!='\0';i++)temp[i]=mycommand[0].argv[i+3];if(chdir(temp)0)perror(Nosuchdirectory!);}elseif(strcmp(mycommand[0].argv,pwd)==0)get_current_dir_name();pid=fork();if(pid==0){if(p==0)mysystem(0);elseif(p0){pipe(pfd);pid1=fork();if(pid1==0){sleep(1);dup2(pfd[1],1);close(pfd[0]);close(pfd[1]);mysystem(0);exit(0);}dup2(pfd[0],0);close(pfd[0]);close(pfd[1]);mysystem(1);}}waitpid(pid,NULL,0);}return0;}截图如下:二:多线程实验实验11.1pi1.c实验内容:使用两个线程根据莱布尼兹级数计算π。解决方法:按题意,主线程计算级数的前半部分,由主线程创建的辅助线程计算级数的后半部分。需根据项数的奇偶性判断辅助线程所需计算的首项的正负性。代码如下:#includestdio.h#includepthread.hdoubleworker_output;void*worker(void*arg){inti;doublesum=0;int*par=(int*)arg;inttemp;intstate;if(((*par)/2)%2)state=-1;elsestate=1;if((*par)%2)temp=*par;elsetemp=*par+1;for(i=(*par)/2;i*par;i++){sum+=1.0/temp*state;state*=-1;temp+=2;}worker_output=sum;}intmain(){pthread_tworker_tid;doublesum=0;inti=0;intpar;inttemp=1;intstate=1;printf(pleaseenterthenumberofitems\n);scanf(%d,&par);pthread_create(&worker_tid,NULL,worker,(void*)&par);for(;ipar/2;i++){sum+=1.0/temp*state;temp+=2;state*=-1;}pthread_join(worker_tid,NULL);sum+=worker_output;printf(%f\n,sum);return0;}截图如下:1.2pi2.c实验内容:使用N个线程根据莱布尼兹级数计算π。解决方法:按题意,主线程计算级数的前半部分,由主线程创建的多个(此处为4)辅助线程计算级数的后半部分。仍需根据项数的奇偶性判断辅助线程所需计算的首项的正负性。使用了结构体以传递相应分割点及计算结果。代码如下:#includestdio.h#includestdlib.h#includepthread.h#defineNUMBER_OF_CPU5structparam{intstart;intend;};structresult{doublesum;};void*worker(void*arg){structresult*result;structparam*param=(structparam*)arg;inttemp;intstate;doublesum=0.0;if(((param-start)/2)%2)state=-1;elsestate=1;for(temp=param-start;temp=param-end;temp+=2){printf(worker:temp=%d,state=%d\n,temp,state);sum+=1.0/temp*state;state*=-1;}result=malloc(sizeof(structresult));result-sum=sum;returnresult;}intmain(){pthread_tworker_tid[NUMBER_OF_CPU];doublesum=0;inti=0;intpar;inteve;structparam*param;structparamparams[NUMBER_OF_CPU];printf(pleaseenterthenumberofitems\n);scanf(%d,&par);eve=par/NUMBER_OF_CPU;for(i=0;iNUMBER_OF_CPU-1;i++){param=¶ms[i];param-start=(i*eve+1)*2-1;param-end=((i+1)*eve)*2-1;pthread_create(&worker_tid[i],NULL,worker,param);}param=¶ms[i];param-start=(i*eve+1)*2-1;param-end=par*2-1;pthread_create(&worker_tid[i],NULL,worker,param);for(i=0;iNUMBER_OF_CPU;i++){structresult*result;pthread_join(worker_tid[i],(void**)&result);sum+=result-sum;free(result);}printf(sum=%f\n,sum);return0;}截图如下:1.3sort.c实验内容:多线程排序。解决方法:主进程和辅助进程分别使用选择排序算法对数组的前半部分和后半部分排序,产生中间结果。归并算法对其处理并放入结果数组,扫描指针分别放到中间结果数组开头和中间。代码如下:#includestdio.h#includepthread.hstructparam{intstart;intend;intarray[80];};void*worker(void*arg){inti,j;inttemp;intp;structparam*param=(structparam*)arg;for(i=param-start;iparam-end;i++){temp=i;for(j=i+1;j=param-end;j++){if((param-array)[temp](param-array)[j])temp=j;}p=(param-array)[i];(param-array)[i]=(param-array)[temp];(param-array)[temp]=p;}}intmain(){pthread_tworker_tid;structparamparam;intn;inti,j;inttemp;intp;printf(pleaseenterthenumberofarray\n);scanf(%d,&n);printf(pleaseenterthearray\n);for(i=0;in;i++)scanf(%d,&(param.array[i]));param.start=n/2;param.end=n-1;pthread_create(&worker_tid,NULL,work
本文标题:操作系统实验
链接地址:https://www.777doc.com/doc-4257063 .html