philosophers_prep/subjects/12_process_basics.txt

26 lines
1.1 KiB
Plaintext

Assignment name: process_basics
Expected files: process_basics.c
Allowed functions: memset, printf, malloc, free, write, fork, exit, waitpid, usleep, gettimeofday
-------------------------------------------------------------------------------
Create a program that:
1. Creates 3 child processes using fork()
2. Each child process prints its process ID (PID) and a unique message 5 times
3. Each child sleeps for 100ms between messages
4. The parent process waits for all children to finish using waitpid()
5. The parent prints a summary when all children have finished
Child messages should be:
- Child 1: "Child 1 (PID: <pid>): Message <n>"
- Child 2: "Child 2 (PID: <pid>): Message <n>"
- Child 3: "Child 3 (PID: <pid>): Message <n>"
Parent message: "Parent: All 3 children have finished"
Usage: `./process_basics`
Hint: fork() creates a new process. It returns 0 in the child process and the child's PID in the parent.
Use exit() to terminate child processes properly.
waitpid() allows the parent to wait for specific children to finish.
Unlike threads, processes have separate memory spaces.