c语言-操作系统实验案例

简介:
题目一:进程控制(杀死子进程)
父进程创建两个子进程,等待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

}

本文转自today4king博客园博客,原文链接:http://www.cnblogs.com/jinzhao/archive/2007/12/15/995833.html,如需转载请自行联系原作者
相关文章
|
2月前
|
存储 监控 安全
C语言与操作系统交互探秘
系统调用与库函数 在 C语言中,系统调用是用户程序与操作系统内核交互的桥梁。以下是常见系统调用的概述: 文件操作类:open()、read()、write()、close()、lseek() 进程控制类:fork()、exec()、wait()、exit() 信号处理类:signal()、kill() 进程间通信:pipe()、shmget()、msgget() 网络通信:socket()、bind()、listen()、accept() 系统调用 vs 库函数:
104 20
|
2月前
|
人工智能 安全 算法
|
8月前
|
算法
数据结构实验之操作系统打印机管理器问题
本实验旨在通过实现操作系统中的打印机管理器问题,掌握队列的基本操作如入队、出队等,利用队列的先进先出特性解决先申请先打印的问题。实验包括队列的初始化、入队、出队、打印队列内容等功能,并通过菜单式界面进行交互。实验结果显示基本功能可正常执行,但在连续操作时存在执行失败的情况,需进一步优化。
121 4
|
10月前
|
Unix 编译器 Shell
[oeasy]python0033_先有操作系统还是先有编程语言_c语言是怎么来的
本文回顾了计算机语言与操作系统的起源,探讨了早期 Unix 操作系统及其与 C 语言的相互促进发展。Unix 最初用汇编语言编写,运行在 PDP-7 上,后来 Thompson 和 Ritchie 开发了 C 语言及编译器,使 Unix 重写并成功编译。1974 年 Ritchie 发表论文,Unix 开始被学术界关注,并逐渐普及。伯克利分校也在此过程中发挥了重要作用,推动了 Unix 和 C 语言的广泛传播。
194 9
[oeasy]python0033_先有操作系统还是先有编程语言_c语言是怎么来的
|
10月前
|
Oracle Java 关系型数据库
CentOS 7.6操作系统部署JDK实战案例
这篇文章介绍了在CentOS 7.6操作系统上通过多种方式部署JDK的详细步骤,包括使用yum安装openjdk、基于rpm包和二进制包安装Oracle JDK,并提供了配置环境变量的方法。
423 80
|
8月前
|
存储 人工智能 算法
数据结构实验之C 语言的函数数组指针结构体知识
本实验旨在复习C语言中的函数、数组、指针、结构体与共用体等核心概念,并通过具体编程任务加深理解。任务包括输出100以内所有素数、逆序排列一维数组、查找二维数组中的鞍点、利用指针输出二维数组元素,以及使用结构体和共用体处理教师与学生信息。每个任务不仅强化了基本语法的应用,还涉及到了算法逻辑的设计与优化。实验结果显示,学生能够有效掌握并运用这些知识完成指定任务。
182 4
|
8月前
|
JSON JavaScript 前端开发
harmony-chatroom 自研纯血鸿蒙OS Next 5.0聊天APP实战案例
HarmonyOS-Chat是一个基于纯血鸿蒙OS Next5.0 API12实战开发的聊天应用程序。这个项目使用了ArkUI和ArkTS技术栈,实现了类似微信的消息UI布局、输入框光标处插入文字、emoji表情图片/GIF动图、图片预览、红包、语音/位置UI、长按语音面板等功能。
663 2
|
9月前
|
C语言
大学生期末C语言实验(学生成绩和鞍点)
大学生期末C语言实验(学生成绩和鞍点)
384 0
大学生期末C语言实验(学生成绩和鞍点)
|
10月前
|
存储 数据挖掘 Linux
服务器数据恢复—Linux操作系统网站服务器数据恢复案例
服务器数据恢复环境: 一台linux操作系统服务器上跑了几十个网站,服务器上只有一块SATA硬盘。 服务器故障: 服务器突然宕机,尝试再次启动失败。将硬盘拆下检测,发现存在坏扇区
|
9月前
|
Linux C语言 iOS开发
MacOS环境-手写操作系统-06-在mac下通过交叉编译:C语言结合汇编
MacOS环境-手写操作系统-06-在mac下通过交叉编译:C语言结合汇编
186 0

热门文章

最新文章

推荐镜像

更多