0% found this document useful (0 votes)
10 views3 pages

Experiment 2 Hassan

The document outlines an experiment focused on executing UNIX system calls related to process management, file management, and input/output operations. It provides code examples for system calls such as fork(), getpid(), wait(), exit() for process management, open(), write(), close() for file operations, and read(), write() for input/output handling. The document also includes instructions for compiling and executing the programs on a UNIX/Linux system.

Uploaded by

itmgida186
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)
10 views3 pages

Experiment 2 Hassan

The document outlines an experiment focused on executing UNIX system calls related to process management, file management, and input/output operations. It provides code examples for system calls such as fork(), getpid(), wait(), exit() for process management, open(), write(), close() for file operations, and read(), write() for input/output handling. The document also includes instructions for compiling and executing the programs on a UNIX/Linux system.

Uploaded by

itmgida186
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/ 3

DATE:7 MAY 2025

EXPERIMENT-2
Objective:To understand and execute different UNIX system calls related to:

1. Process Management
2. File Management
3. Input/Output Operations

1. Process Management System Calls


These system calls control the execution and management of processes.

Example 1.1: fork(), getpid(), wait(), and exit()

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

int main() {
pid_t pid = fork(); // Create child process

if (pid == 0) {
// Child process
printf("Child process: PID = %d\n", getpid());
exit(0); // Child exits
} else {
// Parent process
wait(NULL); // Wait for child to finish
printf("Parent process: PID = %d, child completed.\n",
getpid());
}

return 0;
}

2. File Management System Calls


These are used to create, open, read, write, and manipulate files.

Example 2.1: open(), write(), and close()

#include <stdio.h>
#include <fcntl.h>

2301200100111 2CSE-B Page 1


DATE:7 MAY 2025

#include <unistd.h>

int main() {
int fd = open("sample.txt", O_CREAT | O_WRONLY, 0644);

if (fd < 0) {
perror("File creation failed");
return 1;
}

write(fd, "Hello UNIX!\n", 12); // Write to file


close(fd); // Close file

return 0;
}

3. Input/Output System Calls


These are low-level calls for reading from or writing to files, terminals, etc.

Example 3.1: read() and write() from standard input/output

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

int main() {
char buffer[100];
int n;

write(1, "Enter a string: ", 16); // 1 is STDOUT


n = read(0, buffer, 100); // 0 is STDIN

write(1, "You entered: ", 13);


write(1, buffer, n);

return 0;
}

Compilation and Execution


To compile and run these programs on a UNIX/Linux system:

gcc filename.c -o program


./program

2301200100111 2CSE-B Page 2


DATE:7 MAY 2025

Result:
Successfully executed various UNIX system calls:

 fork(), getpid(), wait(), exit() for process management


 open(), write(), close() for file operations
 read(), write() for input/output handling

2301200100111 2CSE-B Page 3

You might also like