Write a C program that creates 2 child processes1 and2 and 2

Write a C program that creates 2 child processes(1 and2) and 2 pipes and support sending an unlimited number of messages between the parent

and either of the 2 child processes.The parent process reads the ID of the destination process as well as the message itself from the user and sends the message through the correct pipe to its intended destination. The sender process ignores any messages that are destined to neither of the children. As soon as a child process receives a message it prints it to the screen as shown in the sample run below.The parent process will keep on reading messagesfrom the user and sending them to the destination processes until the user types -1 for the ID of the destination.It then closes all open pipes and terminates.Please test for the success of the different system calls you use in your code, and make sure that the parent processwaits for its children before it terminates.

Your program should behave as follows:

After opening the pipes and creating the children, the parent process should prompt the user for the messages in the form: \"<child_to_receive_msg> <one_word_message>\":

1)You may assume that messages are only one word in length (you do not need to handle spaces in messages).

2)The parent will then use pipes P1 and P2 to send all the messages to the appropriate children.

3)Each process should ensure that its pipes are unidirectional.

4)Once received, a message is printed out in the form:

---> Child 1 received: msg_1_to_child_1

A sample run is shown is below:

1.)3 messages are sent to child process 1. These messages are received and printed by process 1.

2.)2 messages were sent to child process 2. The messages were received and printed by process 2.

3.)A message was sent to process 7. It was ignored and an error message was printed by the parent process.

4.)The process terminates as soon as the user types -1 and hits enter.

   

Hints:

To avoid blocking reads in the children, you should consider what happens when processes close one end of a pipe.

To guarantee the order of printing to the screen make sure that the parent process sleeps for a fraction of second. Investigate using the function usleep().

Assume a maximum message size of 64 bytes.

int fd[2];

int pipe(int filedes[2]);

int open(const char *path, int oflags);

int open(const char *path, int oflags, mode_t mode);

ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count);

int usleep(useconds_t usec);

Solution

#include #include #include #include #include #include #include enum { BUFFER_SIZE = 1024 }; typedef int Pipe[2]; static int debug = 0; static void fd_close(int fd); /* These functions normally declared in stderr.h */ static void err_setarg0(const char *argv0); static void err_sysexit(char const *fmt, ...); static void err_usage(char const *usestr); static void err_remark(char const *fmt, ...); static void be_childish(Pipe in, Pipe out) { /* Close irrelevant ends of relevant pipes */ fd_close(in[1]); fd_close(out[0]); char buffer[BUFFER_SIZE]; ssize_t nbytes; while ((nbytes = read(in[0], buffer, sizeof(buffer))) > 0) { buffer[0]++; if (write(out[1], buffer, nbytes) != nbytes) err_sysexit(\"%d: failed to write to pipe\", (int)getpid()); } fd_close(in[0]); fd_close(out[1]); exit(0); } int main(int argc, char **argv) { err_setarg0(argv[0]); int nkids; if (argc != 2 || (nkids = atoi(argv[1])) <= 1 || nkids >= 10) err_usage(\"n # for n in 2..9\"); err_remark(\"Parent has PID %d\ \", (int)getpid()); Pipe pipelist[nkids]; if (pipe(pipelist[0]) != 0) err_sysexit(\"Failed to create pipe #%d\", 0); if (debug) err_remark(\"p[0][0] = %d; p[0][1] = %d\ \", pipelist[0][0], pipelist[0][1]); for (int i = 1; i < nkids; i++) { pid_t pid; if (pipe(pipelist[i]) != 0) err_sysexit(\"Failed to create pipe #%d\", i); if (debug) err_remark(\"p[%d][0] = %d; p[%d][1] = %d\ \", i, pipelist[i][0], i, pipelist[i][1]); if ((pid = fork()) < 0) err_sysexit(\"Failed to create child #%d\", i); if (pid == 0) { /* Close irrelevant pipes */ for (int j = 0; j < i-1; j++) { fd_close(pipelist[j][0]); fd_close(pipelist[j][1]); } be_childish(pipelist[i-1], pipelist[i]); /* NOTREACHED */ } err_remark(\"Child %d has PID %d\ \", i, (int)pid); } /* Close irrelevant pipes */ for (int j = 1; j < nkids-1; j++) { fd_close(pipelist[j][0]); fd_close(pipelist[j][1]); } /* Close irrelevant ends of relevant pipes */ fd_close(pipelist[0][0]); fd_close(pipelist[nkids-1][1]); int w_fd = pipelist[0][1]; int r_fd = pipelist[nkids-1][0]; /* Main loop */ char buffer[BUFFER_SIZE]; while (printf(\"Input: \") > 0 && fgets(buffer, sizeof(buffer), stdin) != 0) { int len = strlen(buffer); if (write(w_fd, buffer, len) != len) err_sysexit(\"Failed to write to children\"); if (read(r_fd, buffer, len) != len) err_sysexit(\"Failed to read from children\"); printf(\"Output: %.*s\", len, buffer); } fd_close(w_fd); fd_close(r_fd); putchar(\'\ \'); int status; int corpse; while ((corpse = wait(&status)) > 0) err_remark(\"%d exited with status 0x%.4X\ \", corpse, status); return 0; } static void fd_close(int fd) { if (debug) err_remark(\"%d: close(%d)\ \", (int)getpid(), fd); if (close(fd) != 0) err_sysexit(\"%d: Failed to close %d\ \", (int)getpid(), fd); } /* Normally in stderr.c */ static const char *arg0 = \"\"; static void err_setarg0(const char *argv0) { arg0 = argv0; } static void err_usage(char const *usestr) { fprintf(stderr, \"Usage: %s %s\ \", arg0, usestr); exit(1); } static void err_vsyswarn(char const *fmt, va_list args) { int errnum = errno; fprintf(stderr, \"%s:%d: \", arg0, (int)getpid()); vfprintf(stderr, fmt, args); if (errnum != 0) fprintf(stderr, \" (%d: %s)\", errnum, strerror(errnum)); putc(\'\ \', stderr); } static void err_sysexit(char const *fmt, ...) { va_list args; va_start(args, fmt); err_vsyswarn(fmt, args); va_end(args); exit(1); } static void err_remark(char const *fmt, ...) { va_list args; va_start(args, fmt); vfprintf(stderr, fmt, args); va_end(args); }
Write a C program that creates 2 child processes(1 and2) and 2 pipes and support sending an unlimited number of messages between the parent and either of the 2
Write a C program that creates 2 child processes(1 and2) and 2 pipes and support sending an unlimited number of messages between the parent and either of the 2

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site