الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

محاكات Round Robin

بدأه نسرين محمد في 28 مارس 2008 · 14 رد · 1,248 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

انا بحاول اعمل برنامج يحاكي 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

}

}

يا رب اللي يساعدني و لو برفع الموضوع ترفعه في عليين

اخواني المبرمجين فكو عني ازمه لأني من جد محتاجتكم

و جزاكم الله خير

#2

تيتي متل ما رحتي, متل ما جيتي <_<

/index.php?showtopic=155935

Chao!!!

Great place, and people, but full of sectarianism.

#3

السلام عليكم كيف يمكن كتابة الكود من اليسار لليمين حتى يصبح مفهوما

#4

السلام عليكم اخوي da666ni

مشكور بس انا الكود هاذا اعطيته صديقتي عشان تحاول تساعدني فيه

و شكلها كمان وضعته في المنتدى

عموما مشكور لكن يشهد الله انه هذا الكود انا كتبته بنفسي و ما نقلته زي ما حضرتك فاهم

و الله يعطيك العافيه

تم تعديل هذه المشاركة بواسطة نسرين محمد في 28 مارس 2008 في 04:31

#5

استخدم التاغ المخصص لها على المنتدى.

Chao!!!

Great place, and people, but full of sectarianism.

#6

this is the code from left to right

#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);

//void getnewprocess(void)

//{

// newprocess *temp;

// temp=nphead;

// // let the head of the queue point to the next node

// nphead=nphead->nextnode;

// temp->nextnode=NULL;

//}

#7

الكود في غلط. تأكدي من الكود!!!

Chao!!!

Great place, and people, but full of sectarianism.

#8

pthread.h

هاذي عشان نظام التشغيل

Linux

لأنه اكيد نظام التشغيل عندك وندوز

عموما انت تقدر تشيلها من الHeader file

و هذا الكود كمان مره بعد ما شلتها

#include<stdio.h>

#include <stdlib.h>

#include <time.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);

}

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);

}

و ترى اخوي انا ما حطيت الكود الا و بعد ما عقدني

و البركه فيك و في غيرك من المبرمجين

ومشكور على الإهتمام

#9
#include<stdio.h>

#include <stdlib.h>

#include <time.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);

}

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);

}

Compiling...
main.cpp
D:\Test\main.cpp(15) : error C2059: syntax error : '['
D:\Test\main.cpp(15) : error C2238: unexpected token(s) preceding ';'
D:\Test\main.cpp(55) : error C2660: 'Generator' : function does not take 1 parameters
D:\Test\main.cpp(57) : error C2660: 'Generator' : function does not take 1 parameters
D:\Test\main.cpp(83) : error C2440: '=' : cannot convert from 'void *' to 'struct newprocess *'
		Conversion from 'void*' to pointer to non-'void' requires an explicit cast
D:\Test\main.cpp(97) : error C2660: 'rand' : function does not take 1 parameters
D:\Test\main.cpp(101) : error C2039: 'priority' : is not a member of 'newprocess'
		D:\Test\main.cpp(11) : see declaration of 'newprocess'
D:\Test\main.cpp(101) : error C2660: 'rand' : function does not take 1 parameters
D:\Test\main.cpp(111) : error C2039: 'priority' : is not a member of 'newprocess'
		D:\Test\main.cpp(11) : see declaration of 'newprocess'
D:\Test\main.cpp(119) : error C2556: 'int __cdecl putnewprocess(struct newprocess *,struct nwequeue *)' : overloaded function differs only by return type from 'void __cdecl putnewprocess(struct newprocess *,struct nwequeue *)'
		D:\Test\main.cpp(37) : see declaration of 'putnewprocess'
D:\Test\main.cpp(119) : error C2371: 'putnewprocess' : redefinition; different basic types
		D:\Test\main.cpp(37) : see declaration of 'putnewprocess'
D:\Test\main.cpp(123) : error C2065: 'nphead' : undeclared identifier
D:\Test\main.cpp(127) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct newprocess' (or there is no acceptable conversion)
D:\Test\main.cpp(129) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct newprocess' (or there is no acceptable conversion)
D:\Test\main.cpp(135) : error C2143: syntax error : missing ';' before '['
D:\Test\main.cpp(137) : error C2143: syntax error : missing ';' before '{'
D:\Test\main.cpp(139) : error C2039: 'tail' : is not a member of 'nwequeue'
		D:\Test\main.cpp(23) : see declaration of 'nwequeue'
D:\Test\main.cpp(139) : error C2227: left of '->next' must point to class/struct/union
D:\Test\main.cpp(145) : error C2065: 'nptail' : undeclared identifier
Error executing cl.exe.

main.exe - 19 error(s), 0 warning(s)

منشان ما توجعي راسك. و راسي, البرنامج عبارة عن Round Robin , بدك مصفوفتين, وحدة ل Working Process و التانية ل Idel Process . شو كمان مطلوب؟؟

تم تعديل هذه المشاركة بواسطة da666ni في 28 مارس 2008 في 04:55

Chao!!!

Great place, and people, but full of sectarianism.

#10

السلام عليكم

اخوي da666ni

اول شي مشكور على الرد السريع

ثاني شي علمني كيف نزلت الكود بهذي الطريقه ،، ترى اختك غشيمه :)

و بعدين اخوي هذا الكود مكتوب في الـ C

وليس C++

فأكيد راح يطلع لك غلط لأنه

C++ compiler does not support C coding

أما لو قلت لي انك استخدمت C compiler

فبعد اذنك انا رايحه انتحر لأنه الكود دوخني :wacko:

و ما ادري كيف دخلت في الكود شكلها لما نزلته طلع حجم الخط في نص البرنامج

و مشكور مره ثانيه و الله يعطيك العافيه :)

#11

ال Compiler مافي مشكلة, لأنو أنا شغال على ال Visual Studio C++ هو بدعم ال С و С++ و Assembly ... ال С متل ال С++ الفرق فقط في OPP.

لإضافة الكود إضغطي على rte-code-button.png

الأهم هو جوبتي على:

اقتباس
منشان ما توجعي راسك. و راسي, البرنامج عبارة عن Round Robin , بدك مصفوفتين, وحدة ل Working Process و التانية ل Idel Process . شو كمان مطلوب؟؟

Chao!!!

Great place, and people, but full of sectarianism.

#12
da666ni كتب:
ال Compiler مافي مشكلة, لأنو أنا شغال على ال Visual Studio C++ هو بدعم ال С و С++ و Assembly ... ال С متل ال С++ الفرق فقط في OPP.

لإضافة الكود إضغطي على rte-code-button.png

الأهم هو جوبتي على:

مشكور على المعلمومه

اخوي صراحه انا ما فهمت سؤالك بس البرنامج عباره عن دالة تقوم بخلق الprocess

و تدخل queue

على شكل linked list

المشكله انه الدكتور يبغاها نبرمجها بحيث تشتغل داله generator

بالموازاه مع داله put in queue

يعني الgenerator

راح يرسل للطابور بدون ما يهمه اذا تم ادخال البروسيس في الطابور لما لا

و الداله المسئوله عن ترتيب البروسيس في الطابور ممكن تنيم الداله generator

اذا كانت الميموري او الbuffer

مليان عشان ما نسبب memroy error

و بعد ما تفضى الميموري شويه نرجع نشغل الgenerator

otherwise the generator is alwyas running and the putinqueue is always running in parallel

ان شاء الله اخوي اكون قدرت افهمك

و الله يعطيك العافيه

#13

بسطة إذا هيك, الكود إن شاء الله بكرة بكون جاهز.. كلو كم If Statment و Loop Statment مع شوي Data Structur.

Chao!!!

Great place, and people, but full of sectarianism.

#14

مشكور اخوي الله يعطيك العافيه مليون مره

و يفرج كل كربه لك زي ما بتحاول انك تفرج كربي :)

#15

da666ni

هدي شوي و اعقل في ردودك :)

تم تعديل هذه المشاركة بواسطة محمد أكرم عليان في 28 مارس 2008 في 11:06

It's hard to stay in such ridiculous situation

مواضيع مشابهة