EssayGhost Assignment代写,Essay代写,网课代修,Quiz代考

EssayGhost-Essay代写,作业代写,网课代修代上,cs代写代考

一站式网课代修,论文代写

高质量的Assignment代写、Paper代写、Report代写服务

EG1hao
网课代修代上,cs代写代考
C/C++代写
您的位置: 主页 > 编程案例 > C/C++代写 >
代写C++/C:C语言代写之Make 3 programs/Hitting a pinata Game - C++代写
发布时间:2021-07-25 22:28:18浏览次数:
Overview:An adult (thegameOfficial) lets some number of children take turns hitting a pinata. With probability 1/20 (as computed by apinataprocess) the pinata will bust, and the winning child get all the candy.In this assignment we ll make 3 programs:a.A pinata program that waits until it receivesSIGUSR1. It will thenosendSIGUSR1with probability 19/20, orosendSIGUSR2with probability 1/20to the process that signalled it.b.A child program who hits the pinata.oIt waits for its parent process (the gameOfficial) to send itSIGUSR1and tell the child that it is that child s turn.oIt then sendsSIGUSR1to the pinata-processes.oIf it gets backSIGUSR1it knows it failed to break the pinata, and it sendsSIGUSR1back to the gameOfficial.oHowever, if it gets backSIGUSR2is knows it did break the pinata, and it sendsSIGUSR2back to the gameOfficial.c.A gameOfficial program.oIt asks the user for:§the number of pinata-whacking children to makeoIt makes all the processesoIt regulates the game, andoIt tells the processes to end after the game is finished.Assignment:1.pinata.cThe pinata s job is to wait for a child to whack it and/or to wait to be told to stop:oChildren whack the pinata by sending itSIGUSR1.oThe gameOfficial tells the pinata to stop by sendingSIGINT.Upon receipt ofSIGUSR1the pinata sees if it broke:oThere is a 19/20 chance that the pinata survives, this causesSIGUSR1to be sent back to the sender.oThere is a 1/20 chance that the pinata breaks, this causesSIGUSR2to be sent back to the sender.Hint:This code might be useful:int isBroken = (rand() % 20) == 19;int signalToSend = (isBroken ? SIGUSR2 : SIGUSR1);Upon receipt ofSIGINTthe program should do:printf( Pinata stopping fflush(stdout);and end.Yourpinataprogram must:A.Inmain(), do:A.srand(getpid()); // This seeds the random number generatorB.Install a signal handler forSIGINTthat causes the program to end. This signal handler shouldprintf()a message that the pinata process is ending.C.Install a signal handler forSIGUSR1. The signal handler should see if the pinata broke by computing(rand() % 20) == 19.§If it istruethen the pinata did break andSIGUSR2should be sent back to the sender.§If it isfalsethen the pinata survived andSIGUSR1should be sent back to the sender.D.While the pinata is waiting to besignal()-ed it should not be doing much of anything. It should run code like:E.while (shouldRun)F.sleep(1);1.child.cThe child s job is to wait for signal numberedSIGUSR1from its parent(the gameOfficial) This means it is its turn.Then the child sends the signalSIGUSR1to thepinataprocess.If the child receivesSIGUSR1(from the pinata) that means it didnotbust the pinata. Itprintf()-s a message of disappointment, and sendsSIGUSR1to its parent say that it has not yet won.If the child receivesSIGUSR2(from the pinata) that means itdidbust the pinata. Itprintf()-s a boastful message, and sendsSIGUSR2to its parent say that it has won.Yourchildprogram must:A.Inmain(), do:B.srand(getpid()); // This seeds the random number generatorC.Check that there are 3 arguments total on the command line (the program s name and 2 others). If there are not then do:D.fprintf(stderr, Usage: %sE. argv[0]F.);G.exit(EXIT_FAILURE);H.Install a signal handler for signalSIGINTto be told when to quit. This signal handler shouldprintf()a message that the child process is ending and actually stop the program.I.Install a signal handler for signalSIGUSR1.§If this signal comes from its parent then it shouldprintf()a message that acknowledges that it is its turn and it should sendSIGUSR1to the pinata.§If this signal comes from the pinata process then it shouldprintf()a message of disappointment and send signalSIGUSR1to its parent (the gameOfficial).J.Install signal handlers for signalSIGUSR2. This handler shouldprintf()a boastful message and send signalSIGUSR2to its parent (the gameOfficial).K.While the child is waiting to besignal()-ed it should not be doing much of anything. It should run code like:L.while (shouldRun)M.sleep(1);2.gameOfficial.cThe gameOfficial s job is to ask for the number of children, launch the pinata process, launch the desired number of children, and then regulate the game.After one of the children has reported that it has won then it should sendSIGINTto the pinata and all children processes. It should also reap them withwaitpid().YourgameOfficialprogram must:A.In a loop get a valid number fornumChildren(greater than 0).B.Install aSIGCHLDsignal handler. This handler should have awhileloop that does a non-hangingwaitpid()that detects whether or not a child process (including pinata process) has ended. If it has it should print that process s PID and whether or not it ended successfully or crashed.C.InstallSIGUSR1signal handlerturnOver(). This function tells the gameOfficial that the current child has finished its turn, but that the game has not yet finished.D.InstallSIGUSR2signal handlerturnOverStopGame(). This function tells the gameOfficial both that the current child has finished its turnandthat the game has finished.E.Have a loop that regulates which child s turn it is. The loop sendsSIGUSR1to the current child.F.After the above loop, have code that sendsSIGINTto all child processes (including the pinata process)I have written much of the gameOfficial program for you. All you have to do is fill in thejuicybits.gameOfficial.c/*--------------------------------------------------------------------------**--------**----gameOfficial.c----**--------**----Thisprogramcontrolsversion2.0ofthepinata-whacking----**----simulator.----**--------**----------------------------------------**--------**----Version2.02017September27JosephPhillips----**--------**--------------------------------------------------------------------------*//*----**----*Commonincludesequence:*----*/#include#include#include#include#include#include/*----**----*Declarationofconstants:*----*/#defineLINE_LEN16#definePINATA_PROG_NAME pinata #defineCHILD_PROG_NAME child /*----**----*Definitionofglobalvars:*----*/intshouldRun=1;intisWaitingForTurn;pid_t*childPidArray;pid_tpinataPid;/*----**----*Definitionofglobalfncs:*----*//*PURPOSE:Tochangetheglobalstatesothattheprogramknowsboththat*thecurrentchildprocesshasfinisheditsturn,andthatitthegame*isover(thatchildwon).Ignoresparameters.Noreturnvalue.*/voidturnOverStopGame(intsig,siginfo_t*info,void*datashouldRun=0;isWaitingForTurn=0;/*PURPOSE:Tochangetheglobalstatesothattheprogramknowsthatthe*currentchildprocesshasfinisheditsturn,butthatitthegameis*notyetover(thatchildlost).Ignoresparameters.Noreturnvalue.*/voidturnOver(intsig,siginfo_t*info,void*dataisWaitingForTurn=0;/*PURPOSE:Toreapallchildprocessesthathavealreadyfinished.Ignores*parameters.Noreturnvalue.*/voidchild(intsig,siginfo_t*info,void*dataintstatus;pid_tfinishedId;//YOURCODEHERE/*PURPOSE:Tosimulatethepinata-whackinggame.Ignorescommandline*parameters.ReturnsEXIT_SUCCESStoOSonsuccessorEXIT_FAILURE*otherwise.*/intmain()//I.Applicationvaliditycheck://II.Dosimulation://II.A.Getsimulationparameters:intnumChildren;charline[LINE_LEN];//II.A.1.Get numChildren (mustbegreaterthanorequalto1)://YOURCODEHERE//II.B.Preparegame://II.B.1.Initialize childPidArray :childPidArray=(pid_t*)calloc(numChildren,sizeof(pid_t));//II.B.2.Installsignalhandlers:structsigactionsa;//YOURCODEHERE//II.C.Launchchildprocesses://II.C.1.Launchpinataprocess:pinataPid=/*REPLACETHISZERO- */0;if(pinataPid==-1){fprintf(stderr, YourOSisbeingfork()-bombed!:( exit(EXIT_FAILURE);}if(pinataPid==0){//YOURCODEHERETOLAUNCHPINATA_PROG_NAMEfprintf(stderr, Couldnotfindprogram%s!:( ,PINATA_PROG_NAME);exit(EXIT_FAILURE);}//II.C.2.Launchpinata-whackingchildprocess(es):inti;for(i=0;i numChildren;i++){childPidArray[i]=/*REPLACETHISZERO- */0;if(childPidArray[i]==-1){fprintf(stderr, YourOSisbeingfork()-bombed!:( exit(EXIT_FAILURE);}if(childPidArray[i]==0){charnumText[LINE_LEN];snprintf(line,LINE_LEN, %d ,pinataPid);snprintf(numText,LINE_LEN, %d ,i);//YOURCODEHERETOLAUNCHCHILD_PROG_NAMEWITH line ASTHEFIRSTARGAND numText ASTHE2NDARGfprintf(stderr, Couldnotfindprogram%s!:( ,CHILD_PROG_NAME);exit(EXIT_FAILURE);}}//II.D.Playgame://II.D.1.Waitasec forallchildprocessestocomposethemselves:sleep(1);//II.D.2.Eachiterationtellsdoesoneturnofonepinata-whacking//childprocess:intcurrentChild=0;while(1){printf( Child%d sturn: ,currentChild);isWaitingForTurn=1;//YOURCODEHERETOSEND SIGUSR1 TO childPidArray[currentChild] while(isWaitingForTurn)sleep(3);if(!shouldRun)break;currentChild++;if(currentChild =numChildren)currentChild=0;}printf( Child%dwon! ,currentChild);//II.E.Cleanupaftergame://II.E.1.Tellallprocessestoendthemselves:for(currentChild=0;currentChild numChildren;currentChild++){//YOURCODEHERETOSEND SIGINT TO childPidArray[currentChild] sleep(1);}//YOURCODEHERETOSEND SIGINT TO pinataPid sleep(1);//II.E.2.Cleanupmemory:free(childPidArray);//III.Finished:return(EXIT_SUCCESS);Communicationprotocolexample:gameOfficial||pinata+--(fork/execl)------------------------ +(gameOfficialmakesthepinata)|||child1|+--(fork/execl)---------- +|(gameOfficialmakeschild1)||||child2||+--(fork/execl) +||(gameOfficialmakeschild2)||||||||||||||||||||||||||||+--(SIGUSR1)-----|------- +|ControltoChild1: Yourturn ||||||+-(SIGUSR1)- +Child1toPinata: Whack! ||||||+- (SIGUSR1)-+PinatatoChild1: Isurvived ||||+ +||ControltoChild2: Yourturn |||||+---------|-(SIGUSR1)- +Child2toPinata: Whack! |||||+---------|- (SIGUSR1)-+PinatatoChild1: Isurvived ||||+ +|ControltoChild1: Yourturn ||||||+-(SIGUSR1)- +Child1toPinata: Whack! ||||||+- (SIGUSR1)-+PinatatoChild1: Isurvived ||||+ +||ControltoChild2: Yourturn |||||+---------|-(SIGUSR1)- +Child2toPinata: Whack! |||||+---------|- (SIGUSR2)-+PinatatoChild1: Youbrokeme! ||||+ +ControltoPinata Timetostop |||||||(processends)|||+--(SIGINT)------|------- +ControltoChild1 Timetostop |||||(processends)||+--(SIGINT)---- +ControltoChild2 Timetostop |||(processends)|(processends)Sampleoutput:$./gameOfficialNumberofchildren?2Child0 sturn:Child0:I mgoingtowhackatit!Child0:Aswingandamiss!Child1 sturn:Child1:I mgoingtowhackatit!Child1:Curses!IthinkIjustweakeneditforthenextchild!Child0 sturn:Child0:I mgoingtowhackatit!Child0:Curses!IthinkIjustweakeneditforthenextchild!Child1 sturn:Child1:I mgoingtowhackatit!Child1:Damn,Imissedthepinatacompletely!Child0 sturn:Child0:I mgoingtowhackatit!Child0:Aswingandamiss!Child1 sturn:Child1:I mgoingtowhackatit!Child1:Aswingandamiss!Child0 sturn:Child0:I mgoingtowhackatit!Child0:HeyIhitit!What sthatthingmadeof?Kevlar?Child1 sturn:Child1:I mgoingtowhackatit!Child1:Curses!IthinkIjustweakeneditforthenextchild!Child0 sturn:Child0:I mgoingtowhackatit!Child0:Aswingandamiss!Child1 sturn:Child1:I mgoingtowhackatit!Ohyeah!AllthatcandyisMINEbaby!Child1won!Child0stopping4405hasfinished.Child1stopping4406hasfinished.Pinatastopping4404hasfinished.代写CS Finance|建模|代码|系统|报告|考试编程类:C代写,JAVA代写,数据库代写,WEB代写,Python代写,Matlab代写,GO语言,R代写金融类:统计,计量,风险投资,金融工程,R语言,Python语言,Matlab,建立模型,数据分析,数据处理服务类:Lab/Assignment/Project/Course/Qzui/Midterm/Final/Exam/Test帮助代写代考辅导天才写手,代写CS,代写finance,代写statistics,考试助攻E-mail:[email protected]微信:BadGeniuscs 工作时间:无休息工作日-早上8点到凌晨3点如果您用的手机请先保存二维码到手机里面,识别图中二维码。如果用电脑,直接掏出手机果断扫描。

所有的编程代写范围:essayghost为美国、加拿大、英国、澳洲的留学生提供C语言代写、代写C语言、C语言代做、代做C语言、数据库代写、代写数据库、数据库代做、代做数据库、Web作业代写、代写Web作业、Web作业代做、代做Web作业、Java代写、代写Java、Java代做、代做Java、Python代写、代写Python、Python代做、代做Python、C/C++代写、代写C/C++、C/C++代做、代做C/C++、数据结构代写、代写数据结构、数据结构代做、代做数据结构等留学生编程作业代写服务。