Modify the code to achieve the same results using only two processes. (A single
ID: 3602189 • Letter: M
Question
Modify the code to achieve the same results using only two processes.
(A single process begins executing the main program. The process calls create twice to start two new processes, that will execute code from function sndch (so, that would make 3 processes, how to use only two processes?)
/* main.c - main */
#include <xinu.h>
void sndch(char);
/*-------------------------------------------------------------------
* main - Example of 2 processes executing the same code concurrently
*--------------------------------------------------------------------
*/
void main(void)
{
resume( create(sndch, 1024, 20, "send A", 1, 'A') );
resume( create(sndch, 1024, 20, "send B", 1, 'B') );
}
/*-------------------------------------------------------------------------
* sndch - Output a character on a serial device indefinitely
*---------------------------------------------------------------------------
*/
void sndch (char ch)
{
while(1)
putc(CONSOLE, ch);
}
Explanation / Answer
This can be achieved using the process ID. Use this pid to check the process completed and to start new process.
#include <xinu.h>
void sndch(char);
/*-------------------------------------------------------------------
* main - Example of 2 processes executing the same code concurrently
*--------------------------------------------------------------------
*/
void main(void)
{
resume( create(sndch, 1024, 20, "send A", 1, 'A') );
pid_t return_pid = waitpid(PID, &status, WNOHANG);
(if return_pid ==0)
{
resume( create(sndch, 1024, 20, "send B", 1, 'B') );
}
}
/*-------------------------------------------------------------------------
* sndch - Output a character on a serial device indefinitely
*---------------------------------------------------------------------------
*/
void sndch (char ch)
{
while(1)
putc(CONSOLE, ch);
PID=getpid();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.