c语言操作系统实验案例



我是计算机科学技术专业的本科生,如果是大侠就不要在这里浪费时间了。
都是操作系统专业课时老师出的题目,不是很有难度,但总是花时间写出来的,有的丢了,重要的收藏一下,欢迎大家交流,一起学好c。
 
由于报告册已经上交所以题目不是很记得,请原谅。
题目一和二必须在linux下执行,题目三在win下执行。
 
题目一:进程控制(杀死子进程)
父进程创建两个子进程,等待ctrl+c信号杀死所有子进程。并打印:
Child Process1 is Killed by parent!
Child Process2 is Killed by Parent!
Parent Process is Exit now
 
代码:

#include<unistd.h>
#include<signal.h>
#include<sys/time.h>
#include<stdio.h>
int pid1,pid2;
void sig_alarm()

}
int main()
{
 if(signal(SIGINT,sig_alarm)==SIG_ERR)
   printf("Signal Error");
 pid1=fork();
 if(pid1>0)
 {
  pid2=fork();
  if(pid2>0)
  {  
   pause();  
   if(kill(pid1,SIGKILL)==kill(pid2,SIGKILL)==0)
    printf("ok\n");
   wait(0);
   wait(0);   
   printf("Parent Process is Exit now\n");   
  }
  else
  {
   signal(SIGKILL,sig_alarm);
   pause();
   printf("Child Process2 is Killed by Parent!\n");
  }
 } 
 else
 { 
  signal(SIGKILL,sig_alarm);
  pause();
  printf("Child Process1 is Killed by parent!\n");
 } 
}

题目二:进程间通信(非原创)
由子进程给父进程发送字符串,子进程一发送的字符串全转换为大写,子进程二全转换为小写。

#include <unistd.h>                                                                                                     

#include <signal.h>                                                                                                     

#include <stdio.h>                                

 

#include<stdlib.h>

void strupr(char *string,char *s_tr)

{

int i=0;

while(string[i]!='\0')

 {

        s_tr[i]= toupper(string[i]);

 i++;

 }

return ;

}

 

void strlwr(char *string,char *s_tr)

{

int i=0;

while(string[i]!='\0')

 {

        s_tr[i]= tolower(string[i]);

 i++;

 }

return ;

}                                                                   

int pid1, pid2;          // 定义两个进程变量

main( ) {

  int fd[2];

 char s_tr[80]="\0";

  char OutPipe[100], InPipe[100];           // 定义两个字符数组

  pipe(fd);                      // 创建管道

  while ((pid1 = fork( )) == -1);   // 如果进程1创建不成功,则空循环

  if (pid1 == 0)

     {                         // 如果子进程1创建成功,pid1为进程号

        lockf(fd[1], 1, 0);         // 锁定管道

        sprintf(OutPipe, "\n Child process 1 is sending message!\n");  // 给Outpipe赋值            

        write(fd[1], OutPipe, 50);  // 向管道写入数据

        sleep(5);                   // 等待读进程读出数据

        lockf(fd[1], 0, 0);          // 解除管道的锁定

        exit(0);                     // 结束进程1

     }

     else {

           while ((pid2 = fork()) == -1);            // 若进程2创建不成功,则空循环

           if (pid2 == 0)

              {

                lockf(fd[1], 1, 0);

                sprintf(OutPipe, "\n Child process 2 is sending message!\n");

                write(fd[1], OutPipe, 50);

                sleep(5);

                lockf(fd[1], 0, 0);

                exit(0);

              }

              else {

                     wait(0);                             // 等待子进程1 结束

                     read(fd[0], InPipe, 50);            // 从管道中读出数据

                     strupr( InPipe, s_tr);           //转化为大写

                     printf("%s\n", s_tr);            // 显示读出的数据

                     wait(0);                                // 等待子进程2 结束

                     read(fd[0], InPipe, 50);

                    strlwr( InPipe, s_tr);

                     printf("%s\n", s_tr);

                     exit(0);                           // 父进程结束

                   }

           }

}


题目三:页面置换原理模拟

自己模拟计算机的指令地址访问(50%顺序执行,25%前地址访问25%后地址访问)。

模拟置换算法:

 

代码:


#include "stdio.h"

#include "stdlib.h"

#include "conio.h"

#include "alloc.h"

#include "io.h"

#include "time.h"

//pages

/*memory pages*/

int mp[4]={-1,-1,-1,-1};

//OPTIMAL memory pages

int opg[4]={0,0,0,0};

//LRU memory pages

double lpg[4]={0,0,0,0};

//Improved CLOCK memorypages

struct clock

{

 int mp;

 int a;

 int m;

 struct clock *pre;

 struct clock *next;

}cpg[4]={

 {0,0,0,&cpg[3],&cpg[1]},

 {1,0,0,&cpg[0],&cpg[2]},

 {2,0,0,&cpg[1],&cpg[3]},

 {3,0,0,&cpg[2],&cpg[0]},

},*chead=&cpg[0],*tail=&cpg[3];

/*instructions*/

struct op

{

 int id;

 int pid;

 int offset;

 struct op *pre;

 struct op *next;

}*cd,*head,*t;

// cd:initialized address flow and set value=end of the address flow

// head:set value=head of the address flow

// t:operation var used in arithmetic calculate

 

/*get next m*/

int getM(int m,int oid)

{

 if(m==-1)

  return random(319);

 if(oid%2)

  return m+1;

 else

 {

  if(oid/2%2)

   return random(m);

  else

   return random(319-m)+m;

 }

}

 

//if not in memory return 1

int notInMemory(int m)

{

 int i;

 for(i=0;i<4;i++)

 {

  if(mp[i]>=0&&(m-mp[i]*10)>0&&(m-mp[i]*10)<10)

  return 0;

 }

 return 1;

}

//optimal arithmetic

void optimal()

{

 int j=0;

 int n=0;

 int m=0;

 struct op *in=t->next;

 for(;in->id<320;in=in->next)

 {

  m=in->pid*10+in->offset;

  if(!notInMemory(m)&&n<3)

  {

   for(j=0;j<4;j++)

   {

    if(mp[j]==in->pid&&opg[j]!=1)

    {

     opg[j]=1;

     n++;

     break;

    }

   }

  }

 }

 for(j=0;j<4;j++)

 {

  //printf("pg[%d].sta=%d\t",mp[j],opg[j]);//test

  if(!opg[j])

   mp[j]=t->pid;

  opg[j]=0;

 }

}

 

//FIFO arithmetic

void FIFO()

{

 int i;

 for(i=0;i<4;i++)

 {

  mp[i]=mp[i+1];

 }

 mp[3]=t->pid;

}

 

//LRU arithmetic

void LRU()

{

 int i;

 int n=0;

 double goal=0;

 double k=lpg[0];

 for(i=1;i<4;i++)

 {

  if(k>=lpg[i])

  {

   k=lpg[i];

   n=i;

  }

 }

 goal=clock()+(double)1;

 while(goal>clock());//time sleep for 0.002

 lpg[n]=clock();

 mp[n]=t->pid;

}

 

//CLOCK arithmetic

void CLOCK()

{

 int i;

 int result=-1;

 struct clock *c=chead;

 for(i=0;i<4;i++)

 {

  if(!c->a&&!c->m)

  {

   result=c->mp;

   break;

  }

  c=c->next;

 }

 if(result==-1)

 {

  c=chead;

  for(i=0;i<4;i++)

  {

   if(!c->a)

   {

    result=c->mp;

    break;

   }

   c->a=0;

   c=c->next;

  }

 }

 if(result==-1)

 {

  c=chead;

  for(i=0;i<4;i++)

  {

   if(!c->a&&!c->m)

   {

    result=c->mp;

    break;

   }

   c=c->next;

  }

 }

 if(result==-1)

 {

  c=chead;

  for(i=0;i<4;i++)

  {

   if(!c->a)

   {

    result=c->mp;

    break;

   }

   c=c->next;

  }

 }

 mp[result]=t->pid;

 if(c==chead)

  chead=c->next;

 if(c!=tail)

 {

  c->pre->next=c->next;

  c->next->pre=c->pre;

  c->next=tail->next;

  tail->next->pre=c;

  tail->next=c;

  c->pre=tail;

  tail=c;

  c->a=0;

  c->m=0;

 }

}

 

void main()

{

 //operation id

 int oid=0;

 //the absolute address initialized from -1

 int m=-1;

 //fail page time

 int fpt=0;

 //only var

 int i,j,k;

 time_t now;

 //record address document

 FILE *f=fopen("address.txt","w");

 //initialize a time random seed

 randomize();

 //make address flow

 for(;oid<320;oid++)

 {

  //get address in the form of os

  m=getM(m,oid);

  //record address

  fprintf(f,"oid=%d\tm=%d\n",oid,m);

  //cd=(struct op *)malloc(sizeof(struct op));

  (*cd).id=oid;

 

  //ten instructions in one page

  (*cd).pid=m/10;

  (*cd).offset=m%10;

 

  //two-way queue

  cd->next=(struct op *)malloc(sizeof(struct op));

  cd->next->pre=cd;

  cd=cd->next;

 }

 fclose(f);

 cd->id=320;

 

 //get head

 head=cd;

 for(;;)

 {

  if((*head).id==0)

  break;

  head=head->pre;

 }

 

 //optimal arithmetic

 //start

 for(t=head;t->id<320;)

 {

  m=t->pid*10+t->offset;

  if(notInMemory(m))

  {

   fpt++;

   optimal();

  }

  t=t->next;

 }

 printf("Optimal arithmetic:\nHit number is %d\n Probability:%f%\n",fpt,100*((float)(320-fpt))/320);

 printf("****************************************************************************\n");

 //end

 

 //FIFO arithmetic

 //initializ vars

 fpt=0;

 for(i=0;i<4;i++)

 {

  mp[i]=-1;

 }

 //start

 for(t=head;t->id<320;)

 {

  m=t->pid*10+t->offset;

  if(notInMemory(m))

  {

   fpt++;

   FIFO();

  }

  t=t->next;

 }

 printf("FIFO arithmetic:\nHit number is %d\n Probability:%f%\n",fpt,100*((float)(320-fpt))/320);

 printf("****************************************************************************\n");

 //end

 

 //LRU arithmetic

 //initializ vars

 fpt=0;

 for(i=0;i<4;i++)

 {

  mp[i]=-1;

 }

 //start

 for(t=head;t->id<320;)

 {

  m=t->pid*10+t->offset;

  if(notInMemory(m))

  {

   fpt++;

   LRU();

  }

  else

  {

   for(i=0;i<4;i++)

   {

    if(mp[i]==m/10)

     lpg[i]=clock();

   }

  }

  t=t->next;

 }

 printf("LRU arithmetic :\nHit number is %d\n Probability:%f%\n",fpt,100*((float)(320-fpt))/320);

 printf("****************************************************************************\n");

 //end

 

 //Improved Clock arithmetic

 //initializ vars

 fpt=0;

 for(i=0;i<4;i++)

 {

  mp[i]=-1;

 }

 //start

 for(t=head;t->id<320;)

 {

  m=t->pid*10+t->offset;

  if(notInMemory(m))

  {

   fpt++;

   CLOCK();

  }

  else

  {

   for(i=0;i<4;i++)

   {

    if(mp[i]==m/10)

     cpg[i].a=1;

   }

  }

  t=t->next;

 }

 printf("Improved Clock arithmetic:\nHit number is %d\n Probability:%f%\n",fpt,100*((float)(320-fpt))/320);

 printf("****************************************************************************\n");

 //end

}

原文地址:https://www.cnblogs.com/jinzhao/p/995833.html