Write a program in C language thatshould be able to run in Linux environment. Th
ID: 3610635 • Letter: W
Question
Write a program in C language thatshould be able to run in Linux environment. This program should beable to perform following tasks. Use necessary system calls tocomplete this program.
· A processcreates a child process.
· Displaythe process id of both parent and child processes.
· Create apipe and through that pipe, child sends a message“Assalam O Alaikum” to its parentprocess. And parent process displays that message on thescreen.
Note:
Develop this program in Linux oruse any other software application (like Cygwin) that supports allthe APIs (system calls) necessary to develop this program.
Explanation / Answer
//Hope this will help you. //Don't forget to rate it. #include #include #include #include #include #include int main(int argc, char *argv[]) { int pfd[2]; pid_t cpid; char buf; if (pipe(pfd) == -1) { perror("pipe");exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork");exit(EXIT_FAILURE); } if (cpid == 0) { close(pfd[1]); while (read(pfd[0],&buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); write(STDOUT_FILENO," ", 1); close(pfd[0]); _exit(EXIT_SUCCESS); } else { printf(" child pid =%d",cpid); printf(" parent pid =%d",getpid()); close(pfd[0]); write(pfd[1],"Assalam OAlaikum", strlen("Assalam O Alaikum")); close(pfd[1]); wait(NULL); exit(EXIT_SUCCESS); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.