Suj Os Lab
Suj Os Lab
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);
}
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.
#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);
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:
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.