Operating system first on-machine experiment Process Control (Ultimate Edition)

Source: Internet
Author: User

Experiment one Process Control

First, the purpose of the experiment

1. Understand the structure of the process Control block.

2. Understand the process of conversion between the various states of the process.

3. Learn how to create and undo processes.

4. Learn how process hangs and wakes up.

Second, the experimental equipment

1. Install PC for Windows or Linux operating system

2. C Program Compilation Environment

Iii. contents of the experiment

The process control process of the operating system is simulated by C language programming. Process Control mainly includes the creation and revocation of processes, the suspend and wake-up of processes, and so on.

Iv. requirements of the experiment

1. Compile and debug the Process Control simulation program on the machine.

2. The corresponding program code and program operation results should be written into the experimental report.

Operating system-binary control simulation
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tlhelp32.h>
#define N 10
struct PB
{
int ko;
int no;
int cha;
};

typedef struct PB PCB;
PCB A[n];
int Showallproc ()
{
PROCESSENTRY32 pe32;//The structure used to store process information
Pe32.dwsize=sizeof (PE32);
HANDLE Hprocesssnap=createtoolhelp32snapshot (th32cs_snapprocess,0);//Get process Snapshot
if (Hprocesssnap==invalid_handle_value)
{
printf ("Call failed \ n");
return 1;
}
BOOL Bproc=process32first (HPROCESSSNAP,&AMP;PE32);
while (Bproc)
{
printf ("%5d%s\n", pe32.th32processid,pe32.szexefile);//output process ID and process name
Bproc=process32next (HPROCESSSNAP,&AMP;PE32);
}
CloseHandle (HPROCESSSNAP);
return 0;
}
static int count=1;
void Create ()//Create
{
static int pi=1;
int k,l=0,m;
printf ("Please input n0:");
scanf ("%d", &m);
For (k= 1;k<= n;k++)
if (a[k].no ==m)
l=1;
if (L = = 1)
{
printf ("error!\n");
Create ();
}
Else
{
For (k= 1;k<= n;k++)
{
if (a[k].no==0)
{
Pi=k;break;}
}
A[pi].no=m;
printf ("\nplease input ko0:");
scanf ("%d", &a[pi].ko);
if (count>1)//First process state is executed, then activity ready
A[pi++].cha=3;
Else
a[pi++].cha=5;
count++;
}
}

Int Stopproc ()
{
    DWORD ProcessID;
    printf ("Please enter the process you want to terminate id\n");
& nbsp;   scanf ("%d", &processid);
    HANDLE hprocess=openprocess (PROCESS_TERMINATE,FALSE,PROCESSID);//Open the corresponding process handle
     if (hprocess==null)
    {
        printf ("Failed \ n");
        return-1;
   }
    if (! TerminateProcess (hprocess,0)//close process
    {
        printf ("Shutdown failed \ n");
   }
    Else
    {
        printf ("Close successfully \ n" );
   }
    CloseHandle (hprocess);
   
    return 0;
}

void dispatch ()//Dispatch
{
int i,m,l=0,j=0,k,min=10000;

For (m= 1;m<= n;m++)
if (a[m].cha==5)
L=m;
if (l!=0)
{
printf ("error!");
Return
}
Else
For (k= 1;k<= n;k++)
{
if (a[k].cha==3)
if (Min>a[k].ko)
{
Min=a[k].ko;
I=k;
J=1;
}
}
if (j)
a[i].cha=5;
else if (j==0)
printf ("error!\n");

}

Void block ()   //block
{
  int k,m,l=0;
  printf ("Please input no0:");
  scanf ("%d", &m);
  for (k= 1;k<= n;k++)
    if (a[k].no==m)
   l=k;
 if (l!=0)
&N Bsp {
   if (a[l].cha==3 | | 5)
     a[l].cha=4;
  else if (a[l].cha==1)
    a[l].cha=2;
    Else
    printf ("error!\n");
 }
 else
   printf ("error!");
}

Void Wakeup ()   //wake-up
{
  int k,m,l=0;
  printf ("Please input no0:");
  scanf ("%d", &m);
  for (k= 1;k<= n;k++)
   if (a[k].no==m)
     l=k;
   if (l!=0)
   {
  if (a[l].cha==2)
    a[l].cha=1;
    else if (a[l]. cha==4)
      a[l].cha=3;
      Else
   printf ("error!\n");
  }
   Else
    printf ("error!");
}


void Suspend ()   //hangs
{
  int k,m,l=0;
  printf ("Please input no0:");
  scanf ("%d", &m);
  for (k= 1;k<= n;k++)
    if (a[k].no==m)
   l=k;
 if (l!=0)
&N Bsp {
   if (a[l].cha==3 | | 5)
     a[l].cha=1;
  else if (a[l].cha==4)
    a[l].cha=2;
    Else
    printf ("error!\n");
 }
 else
   printf ("error!");
}

void active ()//activation
{
int k,m,l=0;
printf ("Please input no0:");
scanf ("%d", &m);
For (k= 1;k<= n;k++)
if (a[k].no==m)
L=k;
if (l!=0)
{
if (a[l].cha==1)
A[l].cha=3;
else if (a[l].cha==2)
a[l].cha=4;
Else
printf ("error!\n");
}
Else
printf ("error!");
}

void display ()//view PCB function module
{
int i;
printf ("\t\tpcb\tno\tko\tcha\n");
For (i= 1;i<= 10;i++)
printf ("\t\t%d\t%d\t%d\t%d\n", I,a[i].no,a[i].ko,a[i].cha);
printf ("\t1-quiesce-ready 2--static blocking 3--Active-ready 4--activity blocking 5--execution \ n");
}

void Change_ko ()//Change priority number
{
int k,m,l=0;
printf ("Please input no0:");
scanf ("%d", &m);
For (k= 1;k<= n;k++)
if (a[k].no==m)
L=k;
if (l!=0)
{
printf ("Please input new KO:");
scanf ("%d", &a[l].ko);
Display ();
}
Else
printf ("error!");
}

int main ()
{
int i;

Do
{
printf ("\t\t************** choice ***************\n");
printf ("\t\t* 1-----Process creation *\n");
printf ("\t\t* 2-----process undo *\n");
printf ("\t\t* 3-----Dispatch *\n");
printf ("\t\t* 4-----Block *\n");
printf ("\t\t* 5-----Wake *\n");
printf ("\t\t* 6-----hangs *\n");
printf ("\t\t* 7-----activating *\n");
printf ("\t\t* 8-----View PCB Function Module *\n");
printf ("\t\t* 9-----Change priority number *\n");
printf ("\t\t*----View process *\n");
printf ("\t\t* 0-----exit *\n");
printf ("\t\t*************************************\n");
printf ("\t\t Please select 0-10:");
scanf ("%d", &i);
printf ("\ n");
Switch (i)
{
Case 1:create ();
Case 2:stopproc ();
Case 3:dispatch ();
Case 4:block ();
Case 5:wakeup ();
Case 6:suspend ();
Case 7:active ();
Case 8:display ();
Case 9:change_ko ();
Case 10:showallproc ();
Case 0:exit (0); Program exits normally
}
}
while (i!=0);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.