c#中的拟合算法_C和C ++中的首次拟合算法

c#中的拟合算法

Here you will learn about first fit algorithm in C and C++ with program examples.

在这里,您将通过程序示例了解C和C ++中的“最适合算法”。

There are various memory management schemes in operating system like first fit, best fit and worst fit. In this section we will talk about first fit scheme.

操作系统中有多种内存管理方案,例如最适合,最适合和最不适合。 在本节中,我们将讨论最适合的方案。

什么是“首次健身记忆管理方案”? (What is First Fit Memory Management Scheme?)

In this scheme we check the blocks in a sequential manner which means we pick the first process then compare it’s size with first block size if it is less than size of block it is allocated otherwise we move to second block and so on.

在该方案中,我们以顺序方式检查块,这意味着我们选择第一个进程,然后将其大小与第一个块大小进行比较(如果它小于分配的块的大小),否则我们移至第二个块,依此类推。

When first process is allocated we move on to the next process until all processes are allocated.

分配第一个流程后,我们将继续进行下一个流程,直到分配了所有流程。

Also Read: Best Fit Algorithm in C and C++

另请阅读: C和C ++中的最佳拟合算法

首次拟合算法 (First Fit Algorithm)

  1. Get no. of Processes and no. of blocks.

    拿到 的过程,没有。 块。
  2. After that get the size of each block and process requests.

    之后,获取每个块的大小并处理请求。
  3. Now allocate processes

    现在分配流程

    if(block size >= process size)

    如果(块大小> =进程大小)

    //allocate the process

    //分配过程

    else

    其他

    //move on to next block

    //前进到下一个块

  4. Display the processes with the blocks that are allocated to a respective process.

    显示带有分配给相应过程的块的过程。
  5. Stop.

    停止。

C语言中的首次拟合算法程序 (Program for First Fit Algorithm in C)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<stdio.h>
void main()
{
int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
for(i = 0; i < 10; i++)
{
flags[i] = 0;
allocation[i] = -1;
}
printf("Enter no. of blocks: ");
scanf("%d", &bno);
printf("\nEnter size of each block: ");
for(i = 0; i < bno; i++)
scanf("%d", &bsize[i]);
printf("\nEnter no. of processes: ");
scanf("%d", &pno);
printf("\nEnter size of each process: ");
for(i = 0; i < pno; i++)
scanf("%d", &psize[i]);
for(i = 0; i < pno; i++)         //allocation as per first fit
for(j = 0; j < bno; j++)
if(flags[j] == 0 && bsize[j] >= psize[i])
{
allocation[j] = i;
flags[j] = 1;
break;
}
//display allocation details
printf("\nBlock no.\tsize\t\tprocess no.\t\tsize");
for(i = 0; i < bno; i++)
{
printf("\n%d\t\t%d\t\t", i+1, bsize[i]);
if(flags[i] == 1)
printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]);
else
printf("Not allocated");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<stdio.h>
void main ( )
{
int bsize [ 10 ] , psize [ 10 ] , bno , pno , flags [ 10 ] , allocation [ 10 ] , i , j ;
for ( i = 0 ; i < 10 ; i ++ )
{
flags [ i ] = 0 ;
allocation [ i ] = - 1 ;
}
printf ( "Enter no. of blocks: " ) ;
scanf ( "%d" , & bno ) ;
printf ( "\nEnter size of each block: " ) ;
for ( i = 0 ; i < bno ; i ++ )
scanf ( "%d" , & bsize [ i ] ) ;
printf ( "\nEnter no. of processes: " ) ;
scanf ( "%d" , & pno ) ;
printf ( "\nEnter size of each process: " ) ;
for ( i = 0 ; i < pno ; i ++ )
scanf ( "%d" , & psize [ i ] ) ;
for ( i = 0 ; i < pno ; i ++ )          //allocation as per first fit
for ( j = 0 ; j < bno ; j ++ )
if ( flags [ j ] == 0 && bsize [ j ] >= psize [ i ] )
{
allocation [ j ] = i ;
flags [ j ] = 1 ;
break ;
}
//display allocation details
printf ( "\nBlock no.\tsize\t\tprocess no.\t\tsize" ) ;
for ( i = 0 ; i < bno ; i ++ )
{
printf ( "\n%d\t\t%d\t\t" , i + 1 , bsize [ i ] ) ;
if ( flags [ i ] == 1 )
printf ( "%d\t\t\t%d" , allocation [ i ] + 1 , psize [ allocation [ i ] ] ) ;
else
printf ( "Not allocated" ) ;
}
}

C ++中的首次拟合算法程序 (Program for First Fit Algorithm in C++)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
using namespace std;
int main()
{
int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
for(i = 0; i < 10; i++)
{
flags[i] = 0;
allocation[i] = -1;
}
cout<<"Enter no. of blocks: ";
cin>>bno;
cout<<"\nEnter size of each block: ";
for(i = 0; i < bno; i++)
cin>>bsize[i];
cout<<"\nEnter no. of processes: ";
cin>>pno;
cout<<"\nEnter size of each process: ";
for(i = 0; i < pno; i++)
cin>>psize[i];
for(i = 0; i < pno; i++)         //allocation as per first fit
for(j = 0; j < bno; j++)
if(flags[j] == 0 && bsize[j] >= psize[i])
{
allocation[j] = i;
flags[j] = 1;
break;
}
//display allocation details
cout<<"\nBlock no.\tsize\t\tprocess no.\t\tsize";
for(i = 0; i < bno; i++)
{
cout<<"\n"<< i+1<<"\t\t"<<bsize[i]<<"\t\t";
if(flags[i] == 1)
cout<<allocation[i]+1<<"\t\t\t"<<psize[allocation[i]];
else
cout<<"Not allocated";
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
using namespace std ;
int main ( )
{
int bsize [ 10 ] , psize [ 10 ] , bno , pno , flags [ 10 ] , allocation [ 10 ] , i , j ;
for ( i = 0 ; i < 10 ; i ++ )
{
flags [ i ] = 0 ;
allocation [ i ] = - 1 ;
}
cout << "Enter no. of blocks: " ;
cin >> bno ;
cout << "\nEnter size of each block: " ;
for ( i = 0 ; i < bno ; i ++ )
cin >> bsize [ i ] ;
cout << "\nEnter no. of processes: " ;
cin >> pno ;
cout << "\nEnter size of each process: " ;
for ( i = 0 ; i < pno ; i ++ )
cin >> psize [ i ] ;
for ( i = 0 ; i < pno ; i ++ )          //allocation as per first fit
for ( j = 0 ; j < bno ; j ++ )
if ( flags [ j ] == 0 && bsize [ j ] >= psize [ i ] )
{
allocation [ j ] = i ;
flags [ j ] = 1 ;
break ;
}
//display allocation details
cout << "\nBlock no.\tsize\t\tprocess no.\t\tsize" ;
for ( i = 0 ; i < bno ; i ++ )
{
cout << "\n" << i + 1 << "\t\t" << bsize [ i ] << "\t\t" ;
if ( flags [ i ] == 1 )
cout << allocation [ i ] + 1 << "\t\t\t" << psize [ allocation [ i ] ] ;
else
cout << "Not allocated" ;
}
return 0 ;
}

Output

输出量

c#中的拟合算法_C和C ++中的首次拟合算法

Comment below if have any queries or found any information incorrect in above first fit algorithm in C and C++.

如果在C和C ++中的上述首次适合算法中有任何疑问或发现任何信息不正确,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2017/01/first-fit-algorithm-in-c.html

c#中的拟合算法