×
Reviews 4.9/5 Order Now

Program To Allow Users To Fork 2 Processes And Then Exit When They Complete In C Language Assignment Solution

July 01, 2024
Dr. Brian Thompson
Dr. Brian
🇨🇦 Canada
C
Dr. Brian Thompson is a seasoned professional with a PhD in Computer Science from McGill University in Montreal, Canada. With 7 years of experience, he has completed over 700 C programming assignments with precision and accuracy. Dr. Thompson's specialization in software engineering and cybersecurity ensures that his solutions are not only efficient but also robust and secure.
Key Topics
  • Instructions
    • Objective
  • Requirements and Specifications
Tip of the day
Focus on understanding each phase of the compiler—lexical, syntax, semantic analysis, and code generation. Use clear diagrams or flowcharts to visualize the process, which helps in debugging and structuring your assignment effectively.
News
JetBrains Fleet 2.0: Offers AI-powered code completion, real-time collaboration, and cross-language support, providing a unified workspace ideal for modern development education.

Instructions

Objective

Write a program that allows users to fork 2 processes and then exit when they complete in C language.

Requirements and Specifications

Process Activity – Programming Language: C

  • Create a program (forkchildren.c) that will fork off two children.
  • One child will run the “ls -l” command
  • The other will run “cat forkchildren.c”
  • The main program will wait for the children to finish and then print a note saying it is finished and then it will end.
  • No pipe is needed for this assignment, each child is separate.

Screenshots of output

program-to-fork-2-processes-then-exit-when-they-complete-in-C

Source Code

#include #include #include #include int main() { pid_t pid1, pid2; int status; pid1 = fork(); /* fork parent */ if (pid1 == -1) /* if error */ { printf("Error forking first child\n"); return (EXIT_FAILURE); } else if (pid1 == 0) /* if in the first child */ { /* execute "ls -l" */ if (execlp("ls", "ls", "-l", (char *) NULL) < 0) { printf("Error in first child\n"); exit(EXIT_FAILURE); } } pid2 = fork(); /* fork parent again */ if (pid2 == -1) /* if error */ { printf("Error forking second child\n"); return (EXIT_FAILURE); } else if (pid2 == 0) /* if in the second child */ { /* execute "cat forkchildren.c" */ if (execlp("cat", "cat", "forkchildren.c", (char *) NULL) < 0) { printf("Error in second child\n"); exit(EXIT_FAILURE); } } /* wait for all children to end */ while (wait(&status) > 0); printf("Main program completed...\n"); return (EXIT_SUCCESS); }

Similar Samples

At ProgrammingHomeworkHelp.com, we specialize in providing expert assistance for all your programming assignments. Whether you're tackling C++, Java, Python, or any other language, our team of experienced programmers ensures top-quality solutions tailored to your needs. With a commitment to accuracy and timely delivery, we're here to help you excel in your programming endeavors. Explore our services today!"