0% found this document useful (0 votes)
32 views7 pages

Suj Os Lab

This document contains 6 code examples demonstrating various process-related concepts in Unix/Linux systems. Example 1 uses fork() to create a new child process. Example 2 executes a simple shell script. Example 3 uses fork() and sleep() to delay the child process. Example 4 also uses fork() along with getpid() and getppid() to print process IDs. Example 5 demonstrates sending a kill signal to a process. Example 6 uses fork() and wait() while processes share a variable.

Uploaded by

077bct087.sujan
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)
32 views7 pages

Suj Os Lab

This document contains 6 code examples demonstrating various process-related concepts in Unix/Linux systems. Example 1 uses fork() to create a new child process. Example 2 executes a simple shell script. Example 3 uses fork() and sleep() to delay the child process. Example 4 also uses fork() along with getpid() and getppid() to print process IDs. Example 5 demonstrates sending a kill signal to a process. Example 6 uses fork() and wait() while processes share a variable.

Uploaded by

077bct087.sujan
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/ 7

Lab Tasks:-

1. Process Creation

Program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int id;
id = fork();

if (id < 0) {
printf("Cannot create the file\n");
exit(-1);
}

if (id == 0) {
printf("Child process\n");
exit(0);
} else {
printf("Parent process\n");
}
return 0;
}

Output:
Summary:
This C program uses the fork system call to create a new process. The fork function returns a
process ID, and the program uses this ID to distinguish between the parent and child
processes. If the fork is successful, the child process prints "Child process" and exits. The
parent process prints "Parent process." If the fork fails, it prints an error message.

2.Executing a command
Output:

Summary:
This simple shell script, when executed, prints “Program for UNIX” and "Welcome" and then
displays the process status using the "ps" command in a Unix-like environment.

3.Sleep Command

Program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int id = fork();

if (id == -1) {
printf("Cannot create the file\n");
exit(1);
} else if (id == 0) {
sleep(2);
printf("This is the child process\n");
exit(0);
} else {
printf("Parent process\n");
exit(0);
}

return 0; // Not strictly necessary, as it won't be reached


in this case
}

Output:

Summary:
This C program uses fork to create a child process. If the fork is successful, the child process
sleeps for 2 seconds and then prints a message. The parent process prints "Parent process." If
fork fails, an error message is displayed. Both the parent and child processes exit after
printing their respective messages.

4.Sleep command using getpid.


Program:

#include <stdio.h>
#include <unistd.h>

int main() {
int pid;
pid = fork();

if (pid == 0) {
printf("\nChild Process\n");
printf("Child Process id is %d\n", getpid());
printf("Its parent process id is %d\n", getppid());
sleep(5);

printf("Child process after sleep=5\n");


printf("Child Process id is %d\n", getpid());
printf("Its parent process id is %d\n", getppid());
} else {
printf("\nParent process\n");
sleep(10);
printf("Child Process id is %d\n", getpid());
printf("Its parent process id is %d\n", getppid());
printf("Parent terminates\n");
}

return 0;
}

Output:
Summary:
This C program uses fork to create a child process. In the child process, it prints information
about its process ID, parent process ID, sleeps for 5 seconds, and then prints the information
again. In the parent process, it prints its own process ID and parent process ID, sleeps for 10
seconds, and then prints a termination message.

5. Signal Handling.

Program:

echo program for performing KILL operations


ps
echo enter the pid
read pid
kill-9 $pid
echo finished

Output:
Summary:
This C program uses fork to create a child process. In the child process, it prints information
about its process ID, parent process ID, sleeps for 5 seconds, and then prints the information
again. In the parent process, it prints its own process ID and parent process ID, sleeps for 10
seconds, and then prints a termination message.
6. Wait.

Program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int i = 10;

int main() {
int pid = fork();

if (pid == 0) {
printf("Initial value of i: %d\n", i);
i += 10;
printf("Value of i in child process: %d\n", i);
printf("Child terminated\n");
} else {
wait(0);
printf("Value of i in parent process: %d\n", i);
}

return 0;
}

Output:

Summary:
This C program uses fork to create a child process. The parent process waits for the child to
terminate using the wait function. Both the parent and child processes share a common variable
'i.' The child process modifies the value of 'i,' and the parent process prints the final value of 'i'
after the child has terminated.

You might also like