您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > 大连理工大学操作系统实验报告(附代码)
一.进程管理(1)#includestdio.h#includeunistd.h#includestdlib.hintmain(void){pid_tpid;pid=fork();if(pid0){printf(errprinfork!\n);}elseif(pid==0){pid_tpid2;pid2=fork();if(pid20){printf(errorinfork!\n);}elseif(pid2==0){printf(Iamthechildprogress2,Idis%d\n,getpid());}else{waitpid(pid2);printf(Iamthechildprogress1.Idis%d\n,getpid());printf(mychildprogressIDis%d\n,pid2);}}else{waitpid(pid);printf(Iamtheparentprogress,IDis%d\n,getpid());printf(mychildprogressIDis%d\n,pid);wait(0);}return1;}(2)#includestdio.h#includeunistd.h#includestdlib.hintmain(void){pid_tpid;pid=fork();if(pid0){printf(errorinfork!\n);}elseif(pid==0){printf(Ianthechildprogress1,IDis%d\n,getpid());}else{pid_tpid2;pid2=fork();if(pid20){printf(errorinfork!\n);}elseif(pid2==0){printf(Ianthechildprogress2,IDis%d\n,getpid());}else{printf(Iamtheparentprogress,IDis%d\n,getpid());printf(mychildprogress1IDis%d\n,pid);printf(mychildprogress2IDis%d\n,pid2);}}return1;}(3)oswork3.cpp#includeiostream#includestring#includestdlib.h#includeunistd.husingnamespacestd;intmain(intargc,char*argv[]){if(argc2){coutyouneedtoinputenoughargs.endl;return0;}stringcontent=argv[1];inti=argc-1;char**p=newchar*[i+1];for(intj=0;ji;j++){p[j]=argv[j+1];}p[i]=(char*)0;if(content==max){execve(max,p,NULL);}elseif(content==min){execve(min,p,NULL);}elseif(content==average){execve(average,p,NULL);}return1;}max.cpp#includeiostream#includestring#includestdlib.husingnamespacestd;intmain(intargc,char*argv[]){if(argc2){coutyouneedtoinputenoughargs.endl;return0;}inti=argc-1;float*p=newfloat[i];for(intj=0;ji;j++){p[j]=atof(argv[j+1]);}floattemp=p[0];for(intj=1;ji;j++){if(p[j]temp)temp=p[j];}couttempendl;return1;}min.cpp#includeiostream#includestring#includestdlib.husingnamespacestd;intmain(intargc,char*argv[]){if(argc2){coutyouneedtoinputenoughargs.endl;return0;}inti=argc-1;float*p=newfloat[i];for(intj=0;ji;j++){p[j]=atof(argv[j+1]);}floattemp=p[0];for(intj=1;ji;j++){if(p[j]temp)temp=p[j];}couttempendl;return1;}average.cpp#includeiostream#includestring#includestdlib.husingnamespacestd;intmain(intargc,char*argv[]){if(argc2){coutyouneedtoinputenoughargs.endl;return0;}inti=argc-1;float*p=newfloat[i];floatsum=0;for(intj=0;ji;j++){p[j]=atof(argv[j+1]);sum+=p[j];}coutsum/(argc-1)endl;return1;}2.WinSock#includeiostream#includestdio.h#includewindows.husingnamespacestd;DWORDWINAPIfx1(LPVOIDlp);DWORDWINAPIfx2(LPVOIDlp);SOCKETlink,sender,s;//定义连接套接字和数据收发套接字sockaddr_inaddr1,addr2;//定义套接字地址结构intport=75;//默认使用端口char*friendIp;//对方ipboolconnected=false;//是否被连接上,连接成功才可以接收boolconnecting=false;//是否连接上对方,连接成功才可以发送boolclose=false;//是否已经关闭连接intmain(){friendIp=newchar[20];WSADatadata;WORDwVersion=MAKEWORD(2,0);::WSAStartup(wVersion,&data);//初始化套接字库link=::socket(AF_INET,SOCK_STREAM,0);//创建TCP套接字s=::socket(AF_INET,SOCK_STREAM,0);addr1.sin_family=AF_INET;//初始化地址结构addr1.sin_port=htons(port);addr1.sin_addr.S_un.S_addr=INADDR_ANY;while(1){if(::bind(link,(sockaddr*)&addr1,sizeof(addr1))==0){//绑定套接字::listen(link,5);cout你已成功启动.\n;break;}else{addr1.sin_port=htons(++port);}}HANDLEh1=::CreateThread(NULL,0,fx1,NULL,0,NULL);HANDLEh2=::CreateThread(NULL,0,fx2,NULL,0,NULL);while(1){if(close){::CloseHandle(h1);::CloseHandle(h2);::closesocket(link);::WSACleanup();break;}}return0;}//等待连接--发送信息(需等待连接上对方)DWORDWINAPIfx2(LPVOIDlp){//套接字地址结构大小intl=sizeof(addr2);chartext[]=连接成功.\n;while(true){//等待对方连接sender=::accept(link,(sockaddr*)&addr2,&l);if(sender!=NULL){coutinet_ntoa(addr2.sin_addr)已成功连接.endl;connected=true;::send(sender,text,sizeof(text),0);break;}}//发送while(true){if(connecting){TCHARtext1[50]={0};cintext1;fflush(stdin);::send(s,text1,sizeof(text1),0);stringtemp(text1);if(temp==close){close=true;break;}}}::closesocket(s);return0;}//连接线程--收取信息(需等待对方连接)DWORDWINAPIfx1(LPVOIDlp){sockaddr_inaddr3;intport0=75;addr3.sin_family=AF_INET;cout请输入对方的IP:;cinfriendIp;addr3.sin_addr.S_un.S_addr=inet_addr(friendIp);if(port==75){port0=port+1;}else{port0=port-1;}addr3.sin_port=htons(port0);while(true){if(::connect(s,(sockaddr*)&addr3,sizeof(addr3))==0){cout已成功连接到friendIpendl;connecting=true;break;}}while(true){if(connected){TCHARtext[50];::recv(sender,text,sizeof(text),0);stringtemp(text);if(temp.size()!=0){if(temp==close){close=true;break;}couttempendl;}}}::closesocket(sender);return0;}每个程序定义两个套接字,分别作客户端和服务器,所有程序公用一份代码。3.生产者-消费者问题分别定义了一个生产者线程和消费者线程,以及三个事件对象,并初始化为空生产者每生产一次将waitFull置为有信号状态,消费者申请到waitFull再将其置空。消费者与生产者同理以及数组访问权waitMutex同理。#includeiostream#includestdio.h#includewindows.hconstintN=15;usingnamespacestd;HANDLEwaitMutex;HANDLEwaitEmpty;HANDLEwaitFull;CRITICAL_SECTIONsection;//定义临界区对象char*p;intfull=0,empty=N,mutex=1;intin=0,out=0;DWORDWINAPIproducer(LPVOIDlp);DWORDWINAPIconsumer(LPVOIDlp);intmain(){p=newchar[N];memset(p,0,20*sizeof(char));//创建事件对象用于唤醒阻塞线程waitMutex=::CreateEvent(NULL,false,false,NULL);waitEmpty=::CreateEvent(NULL,false,false,NULL);waitFull=::CreateEvent(NULL,false,false,NULL);InitializeCriticalSection(§ion);//初始化临界
本文标题:大连理工大学操作系统实验报告(附代码)
链接地址:https://www.777doc.com/doc-5803321 .html