Skip to content

Instantly share code, notes, and snippets.

@rsmahmud
Last active February 22, 2020 03:45
Show Gist options
  • Save rsmahmud/409300a187341f87aa750aa5295d895c to your computer and use it in GitHub Desktop.
Save rsmahmud/409300a187341f87aa750aa5295d895c to your computer and use it in GitHub Desktop.
OS Assignment - PID Manager
/********************************************************************************************
Create a Process Id (PID) manager that keeps track of free PIDs and ensures
that no two active processes are having the same pid. Once a process terminates
the PID manager may assigns its pid to new process.
Use the following constants to identify the range of possible pid values:
#define MIN PID 100
#define MAX PID 1000
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.
Implement the following API for obtaining and releasing a pid:
• int allocate map(void)—Creates and initializes a data structure for representing pids;
returns—1 if unsuccessful, 1 if successful
• int allocate pid(void)—Allocates and returns a pid; returns— 1 if unable to allocate
a pid (all pids are in use)
• void release pid(int pid)—Releases a pid
Modify the above problem by writing a multithreaded program that tests your solution.
You will create a number of threads—for example, 100—and each thread will request a pid,
sleep for a random period of time, and then release the pid. (Sleeping for a
random period of time approximates the typical pid usage in which a pid is assigned
to a new process, the process executes and then terminates,
and the pid is released on the process’s termination.)
**********************************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/wait.h>
#define MIN_PID 100
#define MAX_PID 1000
#define NO_OF_P 16 //no of process/threads
//not using map (bitmap), using a preallocated char array instead
int pid[MAX_PID-MIN_PID]={0};
int allocate_pid(void){
int i,flag=1;
for(i=0; i<MAX_PID-MIN_PID; i++){
if(pid[i]==0){
//pid available//next line should be in critical section
//with a mutex lock, whicever thread gets the lock gets the id.
pid[i]=1;
flag=0;
break;
}
}
return flag?-1:i;
}
void release_pid(int id){
pid[id]=0;
}
void *threadRoutine(void *arg){
int tid = *(( int* )arg);
//get a pid
int id = allocate_pid();
if(id==-1){
puts("No PID available.");
}
else{
printf("Thread [%3d] PID [%3d] Allocated\n",tid,id+MIN_PID);
//sleep for a random time between 1-10 seconds
int r=1+rand()%30;
//uncomment next line to make the thread sleep for a random time
//sleep(r);
printf("Thread [%3d] PID [%3d] Released after %d sec\n",tid,id+MIN_PID,r);
release_pid(id);
}
pthread_exit(NULL);
}
int main(){
int i;
pthread_t process[NO_OF_P];
srand(time(NULL));
for(i=0; i<NO_OF_P; i++){
if(pthread_create(&process[i],NULL,threadRoutine,(void*)&i))
return -1*printf("Error in thread %d creation!!!\n",i);///return a negative integer
}
for(i=0; i<NO_OF_P; i++)
pthread_join(process[i],NULL);
//wait(NULL);
return 0*printf("\nSUCCESSFUL EXIT\n");
}
@siraajul
Copy link

tnx...bhai

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment