Ads

Fork( )

 Fork programs

1. Write a C program that creates a new process using the fork() system call and prints the string "pakistan" from both the parent and child processes. 

2. Write a C program that creates three new processes using three consecutive fork() system calls and prints the string "pakistan" from all processes.

3. Write a C program that uses the fork() system call to create a new process and print different messages from the parent and child processes.

Specifically, your program should:

- Create a new process using fork()

- Print "hello from child" from the child process

- Print "hello from parent" from the parent process

4. Write a C program that uses the fork() system call to create multiple new processes in a loop and print the string "OS" from each process.

5. Write a C program that uses multiple fork() system calls to create new processes and print the string "2" from specific processes.

Specifically, your program should:

- Use nested if statements with fork() calls to control process creation

- The outer if statement should execute only if:

    - The first fork() creates a child process (fork() != 0)

    - The second fork() does not create a child process (!fork() == 0)

- The inner if statement should execute if either of the two subsequent fork() calls creates a child process (fork() != 0 or fork() != 0)

- If the inner if statement executes, an additional fork() call should be made

- Print the string "2" from each process that meets the condition. 

6. Write a C++ program that demonstrates the use of the fork() system call to create a new process. The program should:

- Create a new process using fork()

- In the child process:

    - Print the child process ID using getpid()

    - Print the parent process ID using getppid()

- In the parent process:

    - Print the parent process ID using getpid()

- Handle the case where fork() fails. 

7. Write a C program that demonstrates the use of the fork() system call to create a new process. The program should:

- Print a message before creating the new process

- Create a new process using fork()

- In the child process:

    - Print the child process ID using getpid()

    - Print the parent process ID using getppid()

- In the parent process:

    - Print the child process ID

    - Print the parent process ID using getpid()

- Print a common message from both processes.

8. Write a C program that uses multiple fork() system calls to create new processes and print the string "hello" from each process.


Specifically, your program should:


- Use a conditional statement with two fork() calls

- If either of the two fork() calls creates a new process, execute another fork() call

- Print the string "hello" from each process. 

9. Write a C program that retrieves and prints the process ID of the current process and its parent process using the getpid() and getppid() system calls.

Specifically, your program should:

- Use the getpid() function to retrieve the process ID of the current process

- Use the getppid() function to retrieve the process ID of the parent process

- Print the process ID of the current process

- Print the process ID of the parent process. 

10. Write a C program that uses the fork() system call to create a new process and print the process ID of both the parent and child processes using the getpid() function.

Specifically, your program should:

- Use the fork() function to create a new process

- In the child process:

    - Print the current process ID of the child process using getpid()

- In the parent process:

    - Print the parent process ID using getpid().

11. Write a C program that retrieves and prints the process ID of the current process and its parent process using the getpid() and getppid() system calls.

Specifically, your program should:

- Use the getpid() function to retrieve the process ID of the current process

- Use the getppid() function to retrieve the process ID of the parent process

- Print the process ID of the current process

- Print the process ID of the parent process.

12. Write a code of zombie process

13. Write a code of orphan process

14. Write a C program that demonstrates the use of the fork() system call to create a child process, and the wait() function to make the parent process wait for the child process to finish.

Specifically, your program should:

- Create a child process using fork()

- In the child process:

    - Print a message indicating that it's the child process

    - Print the process ID of the child process using getpid()

    - Exit the child process using exit(0)

- In the parent process:

    - Wait for the child process to finish using wait()

    - Print a message indicating that it's the parent process

    - Print the process ID of the parent process using getpid()

    - Print the parent ID of the parent process using getppid()

    - Exit the parent process using exit(0).

15. Scenario:

Imagine you are working as a software developer for a company that requires a

simple parent-child process simulation. The parent process is responsible for

initiating a task, and the child process completes the task before terminating. The

parent process waits for the child to complete its execution before terminating

itself.

You need to create a program that demonstrates the following:

Process Creation: Use the fork() system call to create a child process.

Task Execution: Assign a specific task to the child process (e.g., printing a

message or performing a calculation).

Termination: The child process should terminate itself using exit(), and the

parent process should wait for the child process to complete before terminating

itself.

Post a Comment

0 Comments