Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views9 pages

Include

Uploaded by

pmahakalkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Include

Uploaded by

pmahakalkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

#include <stdio.

h>

#include <unistd.h>

#include <sys/types.h>

int main() {

pid_t pid;

// Create a new process

pid = fork();

if (pid < 0) {

// fork() failed

perror("Fork failed");

return 1;

} else if (pid == 0) {

// Child process

printf("This is the child process.\n");

printf("Child PID: %d\n", getpid());

printf("Parent PID: %d\n", getppid());

} else {

// Parent process

printf("This is the parent process.\n");

printf("Parent PID: %d\n", getpid());

printf("Child PID: %d\n", pid);

return 0;

}
Explanation:

 fork() creates a new process by duplicating the current one.

 It returns:

o 0 in the child process

o The child’s PID in the parent process

o -1 if fork fails

Output (Example):

yaml

CopyEdit

This is the parent process.

Parent PID: 12345

Child PID: 12346

This is the child process.

Child PID: 12346

Parent PID: 12345

Let me know if you want a version with wait() or with multiple fork()s to show process trees.

You said:

yes

ChatGPT said:

Here's a slightly extended version of the previous code that includes:

 fork() – to create a new process

 wait() – to make the parent wait for the child to finish

✅ Program: fork() with wait()

CopyEdit
#include <stdio.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>

int main() {

pid_t pid;

// Create a child process

pid = fork();

if (pid < 0) {

// fork failed

perror("Fork failed");

return 1;

} else if (pid == 0) {

// Child process

printf("Child process:\n");

printf("PID: %d, Parent PID: %d\n", getpid(), getppid());

sleep(2); // Simulate work

printf("Child process done.\n");

} else {

// Parent process

printf("Parent process:\n");

printf("PID: %d, Child PID: %d\n", getpid(), pid);

wait(NULL); // Wait for the child to complete

printf("Parent process resumes after child finishes.\n");

}
return 0;

🧠 Key Concepts:

 wait(NULL); – causes the parent to pause until child exits

 sleep(2); – simulates child doing some work before exiting

Sample Output:

yaml

CopyEdit

Parent process:

PID: 10500, Child PID: 10501

Child process:

PID: 10501, Parent PID: 10500

Child process done.

Parent process resumes after child finishes.

Would you like a version showing multiple children or a process tree structure using
multiple fork() calls?

Practical Problem Statement: Process Creation and State Transitions Using fork()

Objective:
Write a C program that demonstrates the creation of multiple processes using the fork()
system call and simulates the process state transitions including ready, running, and
terminated states.

💡 Problem Statement

Design a program in C that performs the following:

1. The main (parent) process creates two child processes using fork().
2. Each child process performs a different task:

o Child 1: Prints numbers from 1 to 5 with a delay of 1 second.

o Child 2: Prints lowercase letters from 'a' to 'e' with a delay of 1 second.

3. The parent process waits for both child processes to complete using wait() system
call.

4. While executing, print the process IDs and show which process is currently running.

5. Demonstrate the transition of each process through different states:

o Ready (immediately after creation),

o Running (when executing its task),

o Terminated (after task completion).

🧪 Expected Output Format

Example (simplified):

bash

CopyEdit

Parent process started. PID: 3456

Child 1 created. PID: 3457 — [State: Ready]

Child 2 created. PID: 3458 — [State: Ready]

Child 1 — [State: Running] — Output: 1

Child 2 — [State: Running] — Output: a

...

Child 1 — [State: Terminated]

Child 2 — [State: Terminated]

Parent process ends.


🎯 Learning Outcomes

 Understand and implement process creation using fork().

 Observe and print process states transitions.

 Use of getpid(), getppid(), fork(), and wait() functions in C.

 Understand process synchronization using wait().

Would you like me to provide the complete C code solution for this?

You said:

yes

ChatGPT said:

Here's the complete C program that demonstrates process creation and process states
using fork(), including transitions like Ready, Running, and Terminated:

✅ C Program: Process Creation and State Transitions

CopyEdit

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/wait.h>

void child1_task() {

printf("Child 1 [PID: %d] — [State: Running]\n", getpid());

for (int i = 1; i <= 5; i++) {

printf("Child 1 Output: %d\n", i);

sleep(1);

printf("Child 1 [PID: %d] — [State: Terminated]\n", getpid());


exit(0);

void child2_task() {

printf("Child 2 [PID: %d] — [State: Running]\n", getpid());

for (char c = 'a'; c <= 'e'; c++) {

printf("Child 2 Output: %c\n", c);

sleep(1);

printf("Child 2 [PID: %d] — [State: Terminated]\n", getpid());

exit(0);

int main() {

pid_t pid1, pid2;

printf("Parent process started. PID: %d\n", getpid());

// Create first child

pid1 = fork();

if (pid1 < 0) {

perror("Fork failed for Child 1");

exit(1);

} else if (pid1 == 0) {

// Child 1 process

printf("Child 1 created. PID: %d — [State: Ready]\n", getpid());

sleep(1); // Simulate transition from Ready to Running


child1_task();

} else {

// Parent process continues

// Create second child

pid2 = fork();

if (pid2 < 0) {

perror("Fork failed for Child 2");

exit(1);

} else if (pid2 == 0) {

// Child 2 process

printf("Child 2 created. PID: %d — [State: Ready]\n", getpid());

sleep(1); // Simulate transition from Ready to Running

child2_task();

} else {

// Parent waits for both children

waitpid(pid1, NULL, 0);

waitpid(pid2, NULL, 0);

printf("Parent process [PID: %d] — All child processes terminated.\n", getpid());

return 0;

💡 Explanation:

 fork() creates a new process.

 Each child process has its own task.


 sleep(1) is used to simulate the transition from Ready to Running.

 waitpid() ensures the parent process waits for both child processes to complete.

🧪 Sample Output:

bash

CopyEdit

Parent process started. PID: 12345

Child 1 created. PID: 12346 — [State: Ready]

Child 2 created. PID: 12347 — [State: Ready]

Child 1 [PID: 12346] — [State: Running]

Child 1 Output: 1

Child 2 [PID: 12347] — [State: Running]

Child 2 Output: a

...

Child 1 [PID: 12346] — [State: Terminated]

Child 2 [PID: 12347] — [State: Terminated]

Parent process [PID: 12345] — All child processes terminated.

Would you like a flowchart or diagram to explain this visually?

You might also like