[Link] a simple Unix program that uses the fork() system call in C.
The program demonstrates creating a child process using fork().
The parent process waits for the child process to complete using wait().
Explanation:
1.Forking: fork() creates a new child process. After fork(), two processes run
independently:
● The parent process (with pid > 0)
● The child process (with pid == 0)
2.Parent vs Child:
● If the process is the child, it prints its own message and exits.
● If the process is the parent, it prints its message and waits for the child to finish
using wait(NULL).
3.wait(): The wait() system call ensures that the parent process waits for the child to
finish execution before proceeding.
Program
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
pid_t pid;
// Create a new child process
pid = fork();
if (pid < 0) {
// Error occurred during fork
perror("fork failed");
exit(1);
}
if (pid == 0) {
// This block is executed by the child process
printf("Hello from the child process! PID: %d\n", getpid());
exit(0);
} else {
// This block is executed by the parent process
printf("Hello from the parent process! PID: %d, Child PID: %d\n", getpid(), pid);
// Wait for the child process to finish
wait(NULL);
printf("Parent process: Child has completed.\n");
}
return 0;
}
OUTPUT:
vitit-01-38@vitit01-38:~$ gedit fork.c
vitit-01-38@vitit01-38:~$ gcc fork.c
vitit-01-38@vitit01-38:~$ ./[Link]
Hello from the parent process! PID: 10088, Child PID: 10089
Hello from the child process! PID: 10089
Parent process: Child has completed.
[Link] program in C that simulates the functionality of the ls,opendir(), readdir(), and
closedir() system calls command.
Explanation:
1.list_directory() function:
● This function accepts a path (directory) and opens it using opendir().
● Then, it uses readdir() to read each entry (file/directory) in the directory and
prints the name of each entry.
● Finally, it calls closedir() to close the directory stream when done.
2.Main function:
● If no arguments are passed (i.e., the program is run without specifying a
directory), it lists the contents of the current directory (.).
● If one argument is passed (the directory path), it lists the contents of that
directory.
● If more than one argument is provided, it prints a usage message.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
void list_directory(const char *path) {
DIR *dir;
struct dirent *entry;
// Open the directory specified by 'path'
dir = opendir(path);
if (dir == NULL) {
// If opendir() fails, print an error message and exit
perror("opendir failed");
exit(1);
}
// Read the contents of the directory and print the names
while ((entry = readdir(dir)) != NULL) {
// Print the name of each directory entry (file or subdirectory)
printf("%s\n", entry->d_name);
}
// Close the directory stream
closedir(dir);
}
int main(int argc, char *argv[]) {
if (argc == 1) {
// If no directory is specified, list the contents of the current directory
list_directory(".");
} else if (argc == 2) {
// If a directory is specified, list its contents
list_directory(argv[1]);
} else {
// If more than one argument is given, print usage message
printf("Usage: %s [directory]\n", argv[0]);
exit(1);
}
return 0;
}
OUTPUT:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
void list_directory(const char *path) {
DIR *dir;
struct dirent *entry;
// Open the directory specified by 'path'
dir = opendir(path);
if (dir == NULL) {
// If opendir() fails, print an error message and exit
perror("opendir failed");
exit(1);
}
// Read the contents of the directory and print the names
while ((entry = readdir(dir)) != NULL) {
// Print the name of each directory entry (file or subdirectory)
printf("%s\n", entry->d_name);
}
// Close the directory stream
closedir(dir);
}
int main(int argc, char *argv[]) {
if (argc == 1) {
// If no directory is specified, list the contents of the current directory
list_directory(".");
} else if (argc == 2) {
// If a directory is specified, list its contents
list_directory(argv[1]);
} else {
// If more than one argument is given, print usage message
printf("Usage: %s [directory]\n", argv[0]);
exit(1);
}
return 0;
}
OUTPUT:
vitit-01-38@vitit01-38:~$ gedit dir.c
vitit-01-38@vitit01-38:~$ gcc dir.c
vitit-01-38@vitit01-38:~$ ./[Link]
.
..
codegen.c
[Link]
.p2
[Link]
[Link]
[Link]
swathi
1258.c
[Link]
41x
[Link]
char.c
[Link]
[Link]
[Link]
.java
thread...........................
3. Write a Unix C program that demonstrates the usage of fork(), exec(), exit(), and
close() system calls in one execution.
Program Explanation
● The parent process creates a child process using fork().
● The child process:
● Opens a file ([Link]).
● Closes the file descriptor.
● Uses execvp() to replace itself with the ls command.
● Calls exit() after execution.
● The parent process waits for the child process to finish.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
int main() {
pid_t pid = fork(); // Create a child process
if (pid < 0) {
perror("Fork failed");
exit(1);
} else if (pid == 0) {
// Child process
printf("Child: Opening file '[Link]'\n");
int fd = open("[Link]", O_WRONLY | O_CREAT, 0644);
if (fd < 0) {
perror("Child: File open failed");
exit(1);
}
printf("Child: File opened with descriptor %d\n", fd);
close(fd); // Close the file descriptor
printf("Child: File descriptor closed\n");
printf("Child: Executing 'ls -l' command\n");
execl("/bin/ls", "ls", "-l", NULL); // Replace process with ls
perror("Child: exec failed");
exit(1); // Exit if exec fails
} else {
// Parent process
wait(NULL); // Wait for child process to complete
printf("Parent: Child process finished execution\n");
}
return 0;
}
OUTPUT:
vitit-01-38@vitit01-38:~$ gedit syscall.c
vitit-01-38@vitit01-38:~$ gcc syscall.c
vitit-01-38@vitit01-38:~$ ./[Link]
Child: Opening file '[Link]'
Child: File opened with descriptor 3
Child: File descriptor closed
Child: Executing 'ls -l' command
total 205624
drwxrwxr-x 2 vitit-01-38 vitit-01-38 4096 Sep 23 2023 1238
drwxrwxr-x 2 vitit-01-38 vitit-01-38 4096 Feb 18 16:57 1239
drwxrwxr-x 2 vitit-01-38 vitit-01-38 4096 Aug 19 2024 1258
drwxrwxr-x 2 vitit-01-38 vitit-01-38 4096 May 9 2024 1258.c
-rw-rw-r-- 1 vitit-01-38 vitit-01-38 0 Aug 7 2023 [Link]
drwxrwxr-x 2 vitit-01-38 vitit-01-38 4096 Jul 10 2024 1285
drwxrwxr-x 2 vitit-01-38 vitit-01-38 4096 Oct 22 14:57 1289
drwxrwxr-x 2 vitit-01-38 vitit-01-38 4096 Aug 23 2022 12c0
....................
.................
.................
..................
Parent: Child process finished execution