Frequently Asked Questions About Project 2 ======================================================================= > dar soale dovom, modat zamane "Answering" chegoone beine 0 > ta 2 sanieh taghiir mikonad?(toole in zaman be che chizi > bastegi darad?) It must be a random time. Use "randSleep(2);" for this. ======================================================================= > teacher ba'd az Thanks azad mishavad ya ba'd az Finishing? After printing " T : Finished Answering to : i " teacher is free and can handle another student. ======================================================================= > ayaa bayad baraye har doye " students , teacher " function > joda nevesht? Details of your implementation is not important for me, but I recommand you to write separate functions for students and teachers. ======================================================================= > aya ba'd az Exit har student, bayad aan pardaze raa terminate kard? Yes. ======================================================================= > In your sample always a student "thanks" immediately after > "finished answering to" the student. Is there any problem if > for example we have such scenario: the process of the > student "i" asks his question and gets his answer and > then his time slice is finished and could not do "thanks" and > process "j" asks his question (or whatever) and then the > process of the student "i" do "thanks"? Yes, it is wrong. Only after an student says "Thanks" another student can start asking his question. ======================================================================= > aya estefade az "randsleep" baraye start kardane > pardaazehaa, dar zamaanhaaye motefavet lazem ast?(tebghe sample) It is not necessary, but if you use it, you will have more random results. ======================================================================= > agar proje ra bejaye goroohaye donafare, TANHAEE anjam bedim nomreye ezafe dare? No ======================================================================= > argv[1] yek string ast.aya baayad baraaye estefade az aan ( be onvaane tedaade pardazehaa), > be int tabdil shavad? You must use the atoi function for converting argv[1] from string to integer. For example: num = atoi(argv[1]); It is defined in ======================================================================= > In Elevator question, should we create(fork) all processes (person) at the same time or > the elevator can receive a request when it is in floor ex. 3? In your main() function you create all processes and of course processes will start doing their job, but the elevator doesn't wait for all of the processes to be created (because it doesn't know how many processes will be created). It is possible for the elevator to start moving for handling a request and when it is in floor e.g. 3, another process announce its request. ======================================================================= > In Elevator question, is it a correct assumption that The elevator will wait in every > floor for all the processes that are on that floor to get the CPU and > enter in the elevator? Yes ======================================================================= If you want to have semaphores S1, S2, S3 with initial values of S1=0 , S2=1, S3=5 you must first put these lines at the top of the program: #define S1 0 #define S2 1 #define S3 2 and then use these lines for initialization: sem_init(S1,0); sem_init(S2,1); sem_init(S3,5); ======================================================================= > As I see in your output, every student should leave(exit) just after the he thanked the > teacher. But I see no reason for these two actions to happen consequently (maybe he > doesn't want to leave so rapidly!) You are right. My sample output is just a sample output and many other outputs are possible, so don't try to deduce rules from it. Only those rules that are mentioned explicitly in the project definition are required. ======================================================================= > Can "Entering room"(for another student) happen between "Finished Answering" and "Thanks"? Yes. ======================================================================= > dar masaleie elavator dar soorati ke tabagheie mabda o maghsad ieki bashand aya bayad > person vared shavad o kharej shavd ia inke aslan lazem nist ke hizi neveshte shavad? When generating random numbers for source and destinations floors check and if source = destination then generate another random number. ======================================================================= > chegone mishavad adade tasadfi tolid kard? If you want to generate a random number n between 1 and k then use: srand(time(0)); n = rand() % k + 1; ======================================================================= > barnamehaie ke neveshtam dar soorati ke clean_semaphores() o clean_integers() ra estefade > nakonam dorost kar mikonad o khateme mipazirad.vali vaghti dar entehaa az in do tabe estefade > mikonam , dar hin e ejraie barname error e zir ra midahad(iani baz ham barname ejra mishavad > vali in kho2t e error ham be aan ezafe mishavad) > : Invalid arguement This is because some of the child processes are still running and they want to access shared variables And semaphores. If you have this problem, You must kill all of the child processes with the following instruction before cleaning shared variables: kill(pid, SIGKILL); // pid is the process id of the process that you want to kill ======================================================================= > How can we get the value of a semaphore? In my API you can not do so. But if you use Linux semaphore system calls then you can get the value of a semaphore with semctl system call. By the way, you don't need to get semaphore values for implementing these projects and primitive wait and signal functions are enough for you. ======================================================================= > dar FAQ gofteid ke elevator nemidoone chandta person hastand, > vali baraye inke ejraye process e elevator tamoom she bayad > in ro bedoone (ba farze inke har person faqat 1 darkhast mide), so? No, it is not required for the elevator to know the number of persons. You can run the elevator in a process other that your main process and whenever the work of all of the persons is finished the main process can kill the elevator process. ======================================================================= > va yek soale digar inke agar tabaqeye mabda' va maqsad baraye > yek person yeki bood, tester bayad eshkal begire? Yes ======================================================================= > va yek moshkele digar inke barnameye man rooye console dorost > javab mide vali vaqti be ye file redirectesh mikonam ( > >test.txt) tartib kamelan beham mikhore banabar in natijei ke > too file chap mishe kamelan qalate, in moshkelo chejoori > mishe bartaraf kard? Put a "fflush(stdout);" after all of your printf calls, so that the content will be immediately flushed to the screen. But there is another point that you must take into account. As you mentioned, it is possible that your program be correct logically but output messages be written in the output stream in a different order with program flow. For example it is possible for process p1 to do action a1 and then it must write in the screen "a1 completed" but before writing this, process p2 receives CPU and does action a2 and writes on the screen "a2 completed". In this scenario, although in your program execution a1 is done before a2, you see in the output "a2 completed" before "a1 completed". For handling this problem you must implement "Critical sections" for your programs and whenever you do something in a process, you must put the code for performing that action and code for printing related message in a critical section. ======================================================================= > mishe analyzer ro ba Java nvesht? No, only use C or C++ ======================================================================= > in tekke output baraye elevator : > P : 1 : Wants to go from 1 to 4 > P : 1 : Entered Elevator > Elevator entered floor 2 > P : 2 : Wants to go from 2 to 3 > Elevator entered floor 3 > ... (asansor harkat mikonad ta P1 piade shavad va barmigardad va P2 ra az > 2 be 3 mibarad) > P : 2 Exited elevator > > hatman qalate dige? (chon P2 qabl az inke elev az 2 be 3 bere mitooneste > savar beshe) > Yes ======================================================================= > baraye tashkhise starvation dar readers/writers, hich > raveshe kamelan daqiq i vojood nadare (bejoz khoondane source code!), dar > in mored chekar konim? > Yes, you can not detect starvation and you are not required to handle it in your analyzer. ======================================================================= > yek soale digar inke baranemye readers/writer ke ejra shod ta key edame > bede? A process starts reading or writing then does it for a random period of time and then finishes its job. When all of the processes finished their jobs the program finishes. ======================================================================= > are both these cases correct? > elevator entered floor 2 > p1 exited 2 > p2 want to go from 2 to 4 > p2 entered > elevator reached floor 3 > Correct. > and > elevator entered floor 2 > p1 exited 2 > p2 want to go from 2 to 4 > elevator reached floor 3 Incorrect. > > and can we define a statement "elevator left floor i" > to overcome these situations > No, you can handle this with changing the elevator floor in a critical section. ======================================================================= > As a logical assumption I assumed that after a student says Thanks > another can ask question and for example it is illegal that teacher > answers to i then i+1 asks then teacher starts answering i+1 then i says > thanks. > is it correct? > Only after the first student thanks the teacher another one can start asking his question. ======================================================================= > vaghti az dastoore > ./Elevator 10 > output > estefaade mikonam taa khorooji ro too output berizam peyghamhaaye elevator > aakhar az hame too file zakhire mishe dar haali ke age faghat besoorate > zir: > ./Elevator 10 > barnaamaro ejraa konam peyghaamhaaye elevator be dorosti sarejaashoon > nemaayesh daade mishan. As I mentioned before you must put fflush(stdout) after all of your printf calls and also you must do the print job in a critical section. ======================================================================= > man chand ta soal dar bare'ye tester ha daram: > baraye tester'e soal'e 1 (Readers & writers)az koja bayad > fahmid ke Reader ha ye writer ha starve shodan? > yani ba ye hamchin khoroji 'ee chi mishe goft? > R:1:start > R:2:start > R:1:end > R:2:end > w:1:start > w:1:end > w:2:start > w:2:end > osolan agar ke barnam'ie ba chandin bar run kardan faghat > ye hamchin javabi bede mishe goft ke dorost nist , vali > tester nemitoone ba ien model khoroji (ke faghat ion o ye bar dide) > ghatan bege ke barname ghalat'e!!! ien halat o chi kar bayad kard? > dar mored'e ye khoroji be shekl'e zir chi mishe goft? > R:1:start > R:1:end > R:2:start > R:2:end > R:3:start > R:3:end > ... > kheili mamnoon misham agar ke tozih bedid dar mored'e do halat'e bala > tester chi bayad elam kone. All of these outputs must be detected as correct outputs. In fact your testers can not detect starvation. ======================================================================= > dar mored'e soal'e 2(teacher & students)ham ye soal daram. > dar bein'e ien ke ye student thank mikone va exit kesi nemitone enter > ya soal kone?yani ien khoroji ghalat'e? > S:1:enters > S:1:asks > T:1:answers > T:1:finished answering > S:2:enter > S:1:thanks > S:3:enters > s:1:exits > s:2:asks > . > . > . Yes, this output is correct. =======================================================================