1 Program Explaination
1 Program Explaination
Develop a c program to implement the Process system calls (fork (), exec (), wait (), create
process,terminate process)
● Error Handling:
- If `pid < 0`, the program prints an error message indicating that the `fork()` failed and exits
with a failure status.
================================================
1.Write a c program in Notepad++ with extension filename.c
Open the Terminal
2. Check whether c file is present or not using command
gcc –version
===============================================
gcc -o programname programname.c
./programname.c
You can open the terminal by pressing Ctrl + Alt + T or searching for "Terminal" in the
applications menu.
gcc: This is the GNU Compiler Collection's command for compiling C programs. It is the most
widely used compiler for C and C++ programming on Unix-like systems, including Linux.
Summary
1. gcc: Invokes the compiler.
2. -o myprogram: Specifies the name of the output executable (myprogram).
3. myprogram.c: Specifies the source file to compile
#include<stdio.h> // printf()
#include<stdlib.h> // exit()
#include<sys/types.h> // pid_t
#include<sys/wait.h> // wait()
#include<unistd.h> // fork
int main(int argc, char **argv)//The main function is the entry point of the program. argc and
argv are arguments for handling command-line input, though they are not used in this program.
{
pid_t pid;
pid = fork();
/*fork() returns:
0 in the child process.
A positive integer (the child's process ID) in the parent process.
-1 if there was an error in creating the process.
*/
if(pid==0)/*It prints its process ID using getpid().
Then, it runs a loop to print numbers from 0 to 7.
After completing the loop, the child process calls exit(0); to terminate.
*/
/*pid_t pid; declares a variable pid of type pid_t to store the process ID.
pid = fork(); creates a new process. After this call, there are two processes:
The parent process, which continues execution from where fork() was called.
The child process, which is an exact copy of the parent but with a different process ID.
*/
{
printf("It is the child process and pid is %d\n",getpid());
int i=0;
for(i=0;i<8;i++)
{
printf("%d\n",i);
}
exit(0);
}
else if(pid > 0)
{/*
It prints its own process ID.
The parent process then calls wait(&status); to wait for the child process to finish executing. The
status variable is used to store the exit status of the child process.
Once the child process terminates, the parent process prints "Child is reaped" to indicate that the
child process has been cleaned up.
*/
printf("It is the parent process and pid is %d\n",getpid());
int status;
wait(&status);
printf("Child is reaped\n");
}
else
{
printf("Error in forking..\n");
exit(EXIT_FAILURE);
}
return 0;
}
This C program demonstrates the use of the `fork()`, `wait()`, and `exit()` system calls in process
creation and management. Here's a breakdown of how the code works:
Code Explanation
1. Including Libraries
- `stdio.h`: For standard input/output functions like `printf()`.
- `stdlib.h`: For general utilities like `exit()`.
- `sys/types.h`: For data types used in system calls like `pid_t`.
- `sys/wait.h`: For `wait()` system call.
- `unistd.h`: For various system calls like `fork()` and `getpid()`.
Output
When this program runs, it will generate output similar to the following:
It is the parent process and pid is 12345
It is the child process and pid is 12346
0
1
2
3
4
5
6
7
Child is reaped
- The parent and child processes print their respective messages.
- The child process prints numbers from 0 to 7.
- Finally, the parent process acknowledges that the child process has terminated and been reaped.
Key Concepts
- **Process Creation (`fork()`)**: The `fork()` system call is essential for creating new
processes.
- **Process Synchronization (`wait()`)**: The `wait()` system call allows the parent process to
wait for the child process to finish, ensuring proper process synchronization.
- **Process Termination (`exit()`)**: The `exit()` function is used to terminate a process and
return a status code.
This code is a basic example of process management in C, illustrating the use of fundamental
system calls.
-------------------------------------------------------------------------------------------------------------------
Process Lifecycle
The process lifecycle in an operating system (OS) is the sequence of states a process goes
through from its creation to its termination:
New: The process is created but not yet scheduled for execution.
Ready: The process is ready to be executed and is waiting to be assigned a processor.
Running: The process is currently being executed by a processor.
Waiting: The process is waiting for an event to occur, such as an I/O completion or signal
reception.
Terminated: The process has finished executing or has been terminated by the OS.
Only one process can run on a single CPU at a time. If a system has multiple processors, then the
number of processes that can run at the same time is equal to the number of processors.