انا بحاول اعمل برنامج يحاكي Round Robin
فكرة البرنامج انه فيه generator
يقوم بانشاء process
و يضع فيه الوقت اللي محتاجه عشان يدخل الcpu
و يضع له priority
typedef struct newprocess // this node for new process
{
int burst; /* CPU Burst Time */
int priority; /* Priority for the multi queueing */
newprocess *nextnode;
} newprocess;
و هذي الGenerator Function
void Generator(nwequeue *pointers)
{
// create a new process
struct newprocess *proc;
//make sure that the process is being created correctly
proc = malloc(sizeof(struct newprocess));
if (proc == NULL) {
// here let the function Generator Sleep until theere is space for the new process
printf("Fatal error: memory allocation failure.\nTerminating.\n");
exit(1);
};
// put burst time < 100
proc->burst=rand(100);
// put priority 0,1 or 2
proc->priority=rand(2);
// set the last link to be null
proc->nextnode=NULL;
// put punction
putnewprocess(proc,pointers);
}
وبعد ما يتم انشاء البروسيس نضعه في queue
محتويه على 2 pointer
واحد يأشر على أول الطابور و الثاني يؤشر على آخره
typedef struct nwequeue // this node for new process
{
newprocess *nphead;// head of the queue
newprocess *nptail;// tail of the queue
} nwequeue;
و هاذي داله ال PUT Into Queue
putnewprocess(newprocess *process,nwequeue *pointers)
{
// make the head and tail = this process if it is the first process
if (nphead==NULL)
{
pointers->nphead=*process;
pointers->nptail=*process;
}
// else if not make the tail = this process and let this process point the the previous one
else
{
pointers->tail->next=process;
pointers->nptail=process;
}
printf ("%p \n",nptail);
printf ("%p \n", nphead);
}
مشكلتي التالي داله الgenerator راح تشتغل طول الوقت (New Process) و راح تستمر في خلق الProcesses
يعني راح تشتغل Independently
و راح ترسل هذا البروسيس لدالة PUT و هاذي الداله راح تاخذ البروسيس و تحطه في الqueue
المفروض انه ما يدخل هذي الداله اكثر من بروسيس و لما يخلص يدخل الثاني
و اذا تعبى الBUFFER من ال Generator لازم نخليه ينام لما يروح واحد بروسيس من الqueue
و الله ما ادري اذا شرحي يساعد انكم تفهمو بس الله يخليكم ساعدوني لأني ماني قادره اسوي هذا الشي و هذا الكود كامل
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
// Struct Prototype
typedef struct newprocess // this node for new process
{
int burst; /* CPU Burst Time */
int priority; /* Priority for the multi queueing */
newprocess *nextnode;
} newprocess;
typedef struct nwequeue // this node for new process
{
newprocess *nphead;// head of the queue
newprocess *nptail;// tail of the queue
} nwequeue;
//Fuctions Prototype
void Generator(void);
void intial_pointers( nwequeue *pointers);
void putnewprocess(newprocess *process,nwequeue *pointers);
//void getnewprocess(nwequeue *pointers);
// Global Value Prototype
//main progrma segment
main()
{
// new queue pointer
nwequeue *mypointers;
intial_pointers(mypointers);
Generator(mypointers);
Generator(mypointers);
return 0;
}
void intial_pointers( nwequeue *pointers)
{
pointers->nphead=NULL;
pointers->nptail=NULL;
}
void Generator(nwequeue *pointers)
{
// create a new process
struct newprocess *proc;
//make sure that the process is being created correctly
proc = malloc(sizeof(struct newprocess));
if (proc == NULL) {
// here let the function Generator Sleep until theere is space for the new process
printf("Fatal error: memory allocation failure.\nTerminating.\n");
exit(1);
};
// put burst time < 100
proc->burst=rand(100);
// put priority 0,1 or 2
proc->priority=rand(2);
// set the last link to be null
proc->nextnode=NULL;
// put punction
printf ("%u \n", proc->burst);
printf ("%p \n", proc->priority);
putnewprocess(proc,pointers);
}
//pthread_mutex_t cs_mutex = PTHREAD_MUTEX_INITIALIZER; /* This is the critical section object*/
//pthread_mutex_lock( &cs_mutex );
putnewprocess(newprocess *process,nwequeue *pointers)
{
// make the head and tail = this process if it is the first process
if (nphead==NULL)
{
pointers->nphead=*process;
pointers->nptail=*process;
}
// else if not make the tail = this process and let this process point the the previous one
else
{
pointers->tail->next=process;
pointers->nptail=process;
}
printf ("%p \n",nptail);
printf ("%p \n", nphead);
}
//pthread_mutex_unlock( &cs_mutex);
و الmain راح تكون شي شبيه بهذي الداله
Main()
{
//There should be three seperate thread doing different works.
int p1, p2, p3;
if(p1 = fork()) //The first thread for Generator
{
Generator.start(); //The Generator starts and keep putting process in the ProcessQueue.
}
if(p2 = fork()) //The second thread for Analysis
{
Analysis.start(); //The Analysis starts and keep getting process in the ProcessQueue,producing Quantum and putting new form of process into three-level Queue
}
}
يا رب اللي يساعدني و لو برفع الموضوع ترفعه في عليين
اخواني المبرمجين فكو عني ازمه لأني من جد محتاجتكم
و جزاكم الله خير
