0% found this document useful (0 votes)
3 views

unix_system_calls_example

This document presents a C program that demonstrates the use of various Unix system calls including fork, exec, and wait to create a child process that lists directory contents. The program shows how the parent process waits for the child to complete and captures its exit status. Key functions like opendir, readdir, and closedir are utilized to handle directory operations.

Uploaded by

cse4671
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

unix_system_calls_example

This document presents a C program that demonstrates the use of various Unix system calls including fork, exec, and wait to create a child process that lists directory contents. The program shows how the parent process waits for the child to complete and captures its exit status. Key functions like opendir, readdir, and closedir are utilized to handle directory operations.

Uploaded by

cse4671
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Unix System Calls Example

This document provides an example of a C program that utilizes several Unix system calls such as

fork, exec, getpid, exit, wait, close, opendir, and readdir. The program demonstrates how a parent

process can create a child process to list the contents of a directory, while the parent waits for the

child to complete.

Code Explanation:

1. getpid: Retrieves the process ID of the calling process.

2. fork: Creates a new child process by duplicating the calling process.

3. opendir: Opens a directory stream corresponding to the directory name.

4. readdir: Reads the next directory entry from the directory stream.

5. closedir: Closes the directory stream.

6. exec: Not used directly in this example but could replace the child process image with a new

program.

7. exit: Terminates the calling process.

8. wait: Waits for the child process to change state, and retrieves its exit status.

9. status: Captures the exit status of the child process, used in conjunction with wait.

Sample Code:

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <unistd.h>

#include <dirent.h>

#include <errno.h>
void list_directory(const char *path) {

DIR *dir = opendir(path);

if (dir == NULL) {

perror("opendir");

exit(EXIT_FAILURE);

struct dirent *entry;

while ((entry = readdir(dir)) != NULL) {

printf("%s\n", entry->d_name);

if (closedir(dir) == -1) {

perror("closedir");

exit(EXIT_FAILURE);

int main() {

pid_t pid;

int status;

printf("Parent process ID: %d\n", getpid());

pid = fork();
if (pid == -1) {

perror("fork");

exit(EXIT_FAILURE);

if (pid == 0) { // Child process

printf("Child process ID: %d\n", getpid());

list_directory(".");

exit(EXIT_SUCCESS);

} else { // Parent process

if (wait(&status) == -1) {

perror("wait");

exit(EXIT_FAILURE);

if (WIFEXITED(status)) {

printf("Child exited with status: %d\n", WEXITSTATUS(status));

} else {

printf("Child terminated abnormally\n");

return 0;

You might also like