API 用于获取和释放 PID
API for obtaining and Releasing a PID
我从书中得到了我的第一个硬件作业。任何人都可以帮助设计我的代码。我不知道从哪里开始。我正在考虑使用一个全为零的数组作为第一步,但我真的不知道该怎么做。我不明白如何创建父级以及当我这样做时它应该初始化一个共享内存段这是我的数组应该进入的地方吗?这本书非常好,但确实缺乏准确解释我需要在我的程序中做什么,或者没有提供任何示例输出。感谢您的帮助
An operating system’s pid manager is responsible for managing process
identifiers. When a process is first created, it is assigned a unique
pid by the pid manager. The pid is returned to the pid manager when
the process completes execution, and the manager may later reassign
this pid. Process identifiers are discussed more fully in Section
3.3.1. What is most important here is to recognize that process identifiers must be unique; no two active processes can have the same
pid. Use the following constants to identify the range of possible pid
values:
#define MIN PID 300 #define MAX PID 5000 You may use any data structure of your choice to represent the availability of process
identifiers. One strategy is to adopt what Linux has done and use a
bitmap in which a value of 0 at position i indicates that a process id
of value i is available and a value of 1 indicates that the process id
is currently in use.
int allocate map(void)—创建沙子初始化一个数据结构
代表 pid; returns—不成功1,成功1
int allocate pid(void) — 分配并 returns 一个 pid; returns — 1
如果无法分配 pid(所有 pid 都在使用中)
void release pid(int pid)—释放一个pid
创建一个parent初始化一个共享内存段,然后创建多个children。每个 child 扫描内存段寻找一个空闲槽(意味着从基地址偏移的内存位置),并且 returns 该槽的索引作为其模拟的“pid”,将 0 值替换为它的 child ID 或其他一些表明插槽已被占用的指示。这是一个开始
也许这已经晚了。但是我也遇到了同样的问题,得出了以下结论并想对其进行交叉检查。我找不到任何完整的解决方案,但无论如何,这就是我所做的:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MIN_PID 500
#define MAX_PID 3000
#define cb CHAR_BIT
int sz = MAX_PID - MIN_PID + 1;
unsigned char *b;
int allocate_map();
int allocate_pid();
void release_pid(int pid);
int main()
{
int map = allocate_map();
if (map == 1) {
printf("\nData Structure initialized.\n");
int id = 0, i = 0;
while (i < 15) {
int val = allocate_pid();
printf("\nProcess %d: pid = %d", i+1, val);
i++;
}
release_pid(503); printf("\nProcess 503 released.");
release_pid(505); printf("\nProcess 505 released.");
int val = allocate_pid(); printf("\nProcess %d : pid = %d\n", i+1, val);
}
else printf("\nFailed to initialize data structure.\n");
}
/* Creates and initializes a data structure for representing pids;
returns —1 if unsuccessful, 1 if successful */
int allocate_map() {
b = (unsigned char*)malloc((sz+cb-1)/cb * sizeof(char));
if (b) return 1;
return -1;
}
/* Allocates and returns a pid; returns -1
if unable to allocate a pid (all pids are in use) */
int allocate_pid() {
int i = 0;
int pid = b[i/cb] & (1 << (i & (cb-1)));
while (pid != 0) {
i++;
pid = b[i/cb] & (1 << (i & (cb-1)));
}
if (i+MIN_PID > MAX_PID) return -1;
b[i/cb] |= 1 << (i & (cb-1));
return i+MIN_PID;
}
/* Releases a pid */
void release_pid(int pid) {
if (pid < 500) {
printf("\nInvalid PID: It should lie between 500 and 3000.");
return;
}
int i = pid - MIN_PID;
b[i/cb] &= ~(1 << (i & (cb-1)));
}
我在看Operating system concepts, 10th edition
这本书的时候也遇到了这个问题。我没有找到任何对 cross-check 我的解决方案的引用。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define MIN_PID 300
#define MAX_PID 5000
int get_random();
int allocate_map(void);
int allocate_pid();
void release_pid();
bool* pid_map;
int main() {
// initiate pid map
if(allocate_map() == -1){
printf("unable to create the pid map\n");
}
// sample pid for feature release
int pid1, pid2;
// allocate pids
for(int i = 0; i < 1000; i ++){
int pid = allocate_pid();
if(i == 3) pid1 = pid;
if(i == 4) pid2 = pid;
printf("PID: %d\n", pid);
}
// release pids
release_pid(pid1);
release_pid(1000);
release_pid(pid2);
}
int allocate_map(void){
srand(time(0));
pid_map = malloc(sizeof(bool) * MAX_PID); // yah, allocated extra 300 pid
return pid_map == NULL ? -1 : 1;
}
int allocate_pid(){
int pid = get_random();
while(pid_map[pid] == true){
pid = get_random();
}
pid_map[pid] = true;
return pid;
}
void release_pid(int pid){
if(pid_map[pid] == true){
pid_map[pid] = false;
printf("Release pid %d\n", pid);
} else {
printf("PID %d is not associated with any process\n", pid);
}
}
//to get a random number between max and min pid
int get_random(){
return (rand() % (MAX_PID - MIN_PID + 1) + MIN_PID);
}
我从书中得到了我的第一个硬件作业。任何人都可以帮助设计我的代码。我不知道从哪里开始。我正在考虑使用一个全为零的数组作为第一步,但我真的不知道该怎么做。我不明白如何创建父级以及当我这样做时它应该初始化一个共享内存段这是我的数组应该进入的地方吗?这本书非常好,但确实缺乏准确解释我需要在我的程序中做什么,或者没有提供任何示例输出。感谢您的帮助
An operating system’s pid manager is responsible for managing process identifiers. When a process is first created, it is assigned a unique pid by the pid manager. The pid is returned to the pid manager when the process completes execution, and the manager may later reassign this pid. Process identifiers are discussed more fully in Section 3.3.1. What is most important here is to recognize that process identifiers must be unique; no two active processes can have the same pid. Use the following constants to identify the range of possible pid values: #define MIN PID 300 #define MAX PID 5000 You may use any data structure of your choice to represent the availability of process identifiers. One strategy is to adopt what Linux has done and use a bitmap in which a value of 0 at position i indicates that a process id of value i is available and a value of 1 indicates that the process id is currently in use.
int allocate map(void)—创建沙子初始化一个数据结构 代表 pid; returns—不成功1,成功1
int allocate pid(void) — 分配并 returns 一个 pid; returns — 1 如果无法分配 pid(所有 pid 都在使用中)
void release pid(int pid)—释放一个pid
创建一个parent初始化一个共享内存段,然后创建多个children。每个 child 扫描内存段寻找一个空闲槽(意味着从基地址偏移的内存位置),并且 returns 该槽的索引作为其模拟的“pid”,将 0 值替换为它的 child ID 或其他一些表明插槽已被占用的指示。这是一个开始
也许这已经晚了。但是我也遇到了同样的问题,得出了以下结论并想对其进行交叉检查。我找不到任何完整的解决方案,但无论如何,这就是我所做的:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MIN_PID 500
#define MAX_PID 3000
#define cb CHAR_BIT
int sz = MAX_PID - MIN_PID + 1;
unsigned char *b;
int allocate_map();
int allocate_pid();
void release_pid(int pid);
int main()
{
int map = allocate_map();
if (map == 1) {
printf("\nData Structure initialized.\n");
int id = 0, i = 0;
while (i < 15) {
int val = allocate_pid();
printf("\nProcess %d: pid = %d", i+1, val);
i++;
}
release_pid(503); printf("\nProcess 503 released.");
release_pid(505); printf("\nProcess 505 released.");
int val = allocate_pid(); printf("\nProcess %d : pid = %d\n", i+1, val);
}
else printf("\nFailed to initialize data structure.\n");
}
/* Creates and initializes a data structure for representing pids;
returns —1 if unsuccessful, 1 if successful */
int allocate_map() {
b = (unsigned char*)malloc((sz+cb-1)/cb * sizeof(char));
if (b) return 1;
return -1;
}
/* Allocates and returns a pid; returns -1
if unable to allocate a pid (all pids are in use) */
int allocate_pid() {
int i = 0;
int pid = b[i/cb] & (1 << (i & (cb-1)));
while (pid != 0) {
i++;
pid = b[i/cb] & (1 << (i & (cb-1)));
}
if (i+MIN_PID > MAX_PID) return -1;
b[i/cb] |= 1 << (i & (cb-1));
return i+MIN_PID;
}
/* Releases a pid */
void release_pid(int pid) {
if (pid < 500) {
printf("\nInvalid PID: It should lie between 500 and 3000.");
return;
}
int i = pid - MIN_PID;
b[i/cb] &= ~(1 << (i & (cb-1)));
}
我在看Operating system concepts, 10th edition
这本书的时候也遇到了这个问题。我没有找到任何对 cross-check 我的解决方案的引用。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define MIN_PID 300
#define MAX_PID 5000
int get_random();
int allocate_map(void);
int allocate_pid();
void release_pid();
bool* pid_map;
int main() {
// initiate pid map
if(allocate_map() == -1){
printf("unable to create the pid map\n");
}
// sample pid for feature release
int pid1, pid2;
// allocate pids
for(int i = 0; i < 1000; i ++){
int pid = allocate_pid();
if(i == 3) pid1 = pid;
if(i == 4) pid2 = pid;
printf("PID: %d\n", pid);
}
// release pids
release_pid(pid1);
release_pid(1000);
release_pid(pid2);
}
int allocate_map(void){
srand(time(0));
pid_map = malloc(sizeof(bool) * MAX_PID); // yah, allocated extra 300 pid
return pid_map == NULL ? -1 : 1;
}
int allocate_pid(){
int pid = get_random();
while(pid_map[pid] == true){
pid = get_random();
}
pid_map[pid] = true;
return pid;
}
void release_pid(int pid){
if(pid_map[pid] == true){
pid_map[pid] = false;
printf("Release pid %d\n", pid);
} else {
printf("PID %d is not associated with any process\n", pid);
}
}
//to get a random number between max and min pid
int get_random(){
return (rand() % (MAX_PID - MIN_PID + 1) + MIN_PID);
}