Program 1
#include <stdlib.h>
#include <stdio.h>
int main()
{
printf("Running ps with system\n");
system("ps -ax &");
printf("Done.\n");
exit(0);
}
Program 2
#include <unistd.h>
#include <stdio.h>
int main()
{
printf("Running ps with execlp\n");
execlp("ps", "ps", "-ax", 0);
printf("Done.\n");
exit(0);
}
Program 3
#include <stdio.h>
#include <unistd.h>
int main(){
pid_t child_p;
printf("Running ps with fork\n");
child_p = fork();
execlp("ps", "ps", "-ax", 0);
return 0;
}
Program -4
#include <unistd.h>
#include <stdio.h>
int main()
{
pid_t pid;
printf("Running ps again with fork\n");
pid = fork();
if ( pid == 0 ) { // in the child, do exec
execlp("ps", "ps", "-ax", 0);
}
else if (pid < 0) // failed to fork
{
printf("fork failed.\n");
exit(1);
}
else // parent
{
wait(NULL);
}
exit(0);
}
Program – 5
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main() //This is the main function where the execution of
the program begins.
{
pid_t pid; //Declares a variable pid to store the process ID.
char *message; //Declares a pointer message to store a string.
int n; //Declares an integer variable n to control the number of loop
iterations.
printf("fork program starting\n");
pid = fork(); //Creates a new process by duplicating the current
process. fork() returns:
//-1 if the fork failed.0 to the child process. The process ID of the
child to the parent process.
switch(pid) //Evaluates the value of pid and branches the code based
on its value.
{
case -1:
perror("fork failed");
exit(1);
case 0:
message = "This is the child";
n = 7; //n = 7;: Sets n to 7, meaning the child process will loop
7 times.
break;
default:
message = "This is the parent";
n = 3;
break;
}
for(; n > 0; n--) {
puts(message); //Prints the message (either "This is the child" or
"This is the parent") to the console.
sleep(1);
}
exit(0);
}
Program – 6
/* zombie test. */
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t pid; //Declares a variable pid to store the process ID returned by
fork().
switch(pid = fork()) //Creates a new process by duplicating the current
process. The fork() function returns:
//Evaluates the value of pid and branches the code based on its value.
//'-1' if the fork failed
// '0' to the child process.
// The process ID of the child to the parent process.
{
case -1:
perror("fork failed");
exit(1);
case 0:
printf(" CHILD: My PID is %d, My parent's PID is %d\n", getpid(),
getppid());
exit(0); // Exits the child process immediately with a status code
'0'. This creates a "zombie" state for the child because the parent does
not immediately wait for it.
default:
printf("PARENT: My PID is %d, My child's PID is %d\n", getpid(),
pid); //Prints the parent's process ID and the child's process ID.
printf("PARENT: I'm now looping...\n"); //Prints a message
indicating that the parent process is entering an infinite loop
while(1); //An infinite loop, which means the parent process will
run indefinitely without terminating or performing a 'wait()' call to
clean up the child process.
}
exit(0); // This line will never be reached because the parent process
is stuck in the infinite loop. The infinite loop ensures the parent
process remains alive, keeping the child process in the zombie state
}
Program -7
#include <unistd.h>
#include <stdio.h>
int main()
{
pid_t pid;
pid_t pid1; //Declares a variable 'pid1' to store the process ID
returned by the second 'fork()'
pid = fork();
if ( pid > 0 ) { //This condition is true for the parent process
because 'pid' contains the process ID of the child. It means the parent
process will execute the following block.
pid1=fork();
printf("Hello\n"); //Both the parent process and the new child process
will execute this line, printing "Hello".
}
else
{
printf("World\n"); //This block executes if 'pid == 0', meaning
this is the first child process created by the initial 'fork()'.
}
Program – 8
/* ----------------------------------------------------------------- */
/* PROGRAM fork-02.c */
/* This program runs two processes, a parent and a child. Both of */
/* them run the same loop printing some messages. Note that printf()*/
/* is used in this program. */
/* ----------------------------------------------------------------- */
#include <stdio.h>
#include <sys/types.h>
#define MAX_COUNT 200 //Defines a constant 'MAX_COUNT' with a value of
200, which determines the number of iterations for the loops in both the
child and parent processes.
void ChildProcess(void); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
void main(void)
{
pid_t pid;
pid = fork();
if (pid == 0)
ChildProcess(); //Calls the 'ChildProcess' function if the
current process is the child
else
ParentProcess(); // Calls the 'ParentProcess' function if the
current process is the parent
}
void ChildProcess(void)
{
int i; // Declares a loop counter variable 'i'
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d\n", i); // Prints
a message from the child process with the current value of 'i'
printf(" *** Child process is done ***\n");
}
void ParentProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i); //Prints a
message from the parent process with the current value of 'i'
printf("*** Parent is done ***\n");
}
Program – 9
/* ----------------------------------------------------------------- */
/* PROGRAM fork-01.c */
/* This program illustrates the use of fork() and getpid() system */
/* calls. Note that write() is used instead of printf() since the */
/* latter is buffered while the former is not. */
/* ----------------------------------------------------------------- */
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#define MAX_COUNT 200
#define BUF_SIZE 100 //Defines a constant 'BUF_SIZE' with a value of
100, determining the size of the buffer for output
void main(void) //The main function where program execution begins. In
modern C, it's recommended to use int ''main(void)'', but this is still
valid
{
pid_t pid;
int i;
char buf[BUF_SIZE]; //Declares a buffer buf with a size of BUF_SIZE
(100 characters) for storing the output string
fork(); //Creates a new process by duplicating the current process.
After this call, there are two processes: the parent and the child. Both
processes continue executing from this point.
pid = getpid();
for (i = 1; i <= MAX_COUNT; i++) {
sprintf(buf, "This line is from pid %d, value = %d\n", pid, i);
//Formats a string with the current process ID ('pid') and the loop
counter value ('i') and stores it in 'buf'
write(1, buf, strlen(buf)); //Writes the contents of 'buf' to
file descriptor 1 (standard output). 'write()' is used instead of
'printf()' because 'write()' is unbuffered, ensuring immediate output.
}
}
Program -10
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#define SIZE 5 //Defines a constant SIZE with a value of 5, which is the
size of the array 'nums'
int nums[SIZE] = {0,1,2,3,4}; //Declares and initializes a global array
nums with five elements
int main()
{
int i;
pid_t pid;
pid = fork();
if (pid == 0) { //Checks if the process is the child process
for (i = 0; i < SIZE; i++) { // Iterates over the nums array and
modifies each element by multiplying it by '-i'
nums[i] *= -i; //Modifies the value of nums'[i]'
printf("CHILD: %d ",nums[i]); // Prints the modified value
prefixed with "CHILD:"
}
}
else if (pid > 0) { //hecks if the process is the parent process
wait(NULL); //Waits for the child process to finish execution
for (i = 0; i < SIZE; i++)
printf("PARENT: %d ",nums[i]); //Prints the value of 'nums[i]'
prefixed with "PARENT:"/
}
puts(""); //rints a newline character to ensure the next output starts
on a new line.
return 0; //Indicates successful termination of the program
}