0% found this document useful (0 votes)
46 views9 pages

Operating System CSP244

Practical file format for lab manual btech second year
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)
46 views9 pages

Operating System CSP244

Practical file format for lab manual btech second year
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/ 9

Laboratory Report

PRINCIPLES OF OPERATING SYSTEMS

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SCHOOL OF ENGINEERING & TECHNOLOGY

Submitted By
Student Name Faizan Hassan
Student Id 2023486998
Section/Group CS-P
Programme B.Tech (CS&P)
Department Computer Science and Engineering
Session/Semester 2023-24/2303
Submitted To
Faculty Name Mr. Kanderp Narayan Mishra

SHARDA UNIVERSITY
Plot No. 32-34, Knowledge Park III,
Greater Noida, Uttar Pradesh 201310
TABLE OF CONTENT

Sl No. Date Aim of the Experiment Signature Grade


/date
1. Write programs using the following system calls of LINUX
operating system: fork, exec, getpid, exit, wait, close, stat,
opendir, readdir.
2. Write programs using the I/O system calls of LINUX
operating system (open, read, write, etc)
3. Write C programs to simulate LINUX commands like ls, grep,
etc.
4. Write a program to create processes and threads.
5. Write a program solving the Producer-Consumer problem
using semaphores.
6. Write a program to implement the solution for the dining
philosopher’s problem.
7. Write a program to develop an application using Inter
process communication using shared Memory.
8. Write a program to implement process scheduling
mechanisms using FCFS & SJF.
9. Write a program to implement process scheduling
mechanisms using Priority & round-robin scheduling.
10. Write a program to implement the banker's algorithm.
11. Write a program to implement memory allocation using first
fit algorithm.
12. Write a program to implement memory allocation using best
fit algorithm.

Sl No. Date Value added Experiments Signature Grade


/date
1. Implement any file allocation technique (Linked, Indexed or
Contiguous)
2. Simulate Paging Technique of Memory Management

Name - Faizan Hassan


Sys ID - 2023486998
EXPERIMENT NO: 1
DATE:9/01/24
AIM OF THE EXPERIMENT:
Write programs using the following system calls of LINUX operating system: fork, exec, getpid,
exit, wait, close, stat, opendir, readdir.

Theory :

In computing, a system call (commonly abbreviated to syscall) is the programmatic way in


which a computer program requests a service from the operating system[a] on which it is
executed.

I. fork(): The use of the fork() system call is to create a new process by duplicating the
calling process. The fork() system call is made by the parent process, and if it is
successful, a child process is created.

PROGRAM FOR “FORK()” SYSTEM CALL:


1. #include <stdio.h>
2. #include <unistd.h>
3.
4. int main() {
5. pid_t child_pid;
6. child_pid = fork();// Create a child process
7. if (child_pid == 0) {
8. printf("Child process\n");
9. } else if (child_pid > 0) {
10. printf("Parent process\n");
11. } else {
12. perror("Fork failed");
13. }
14. return 0;
15. }

OUTPUT:

II. exec(): The exec system call in Linux is used to replace the current running process with
a new process. It is often used in conjunction with process creation functions like fork,
where a child process is created.

PROGRAM FOR “EXEC()” SYSTEM CALL:


1. #include <stdio.h>
2. #include <unistd.h>
3.
4. int main() {
5. pid_t process_id = getpid();// Get the process ID
6. printf("The process ID is %d\n", process_id);// Print the process ID
7. return 0;
8. }
Name - Faizan Hassan
Sys ID - 2023486998
OUTPUT FOR “EXEC()” SYSTEM CALL:

III. getpid(): The getpid() function returns the process ID of the calling process.

PROGRAM FOR “GETPID()” SYSTEM CALL:


1. #include <stdio.h>
2. #include <unistd.h>
3.
4. int main() {
5. pid_t process_id = getpid();// Get the process ID
6. printf("The process ID is %d\n", process_id);// Print the process ID
7. return 0;
8. }

OUTPUT FOR “GETPID()” SYSTEM CALL:

IV. exit():The exit command in Linux terminates the current shell session and logs the user
out of their current session.

OUTPUT FOR “EXIT()” SYSTEM CALL:

Name - Faizan Hassan


Sys ID - 2023486998
V. wait(): The wait() command pauses execution until one or more child processes finish.

PROGRAM FOR “FORK()” SYSTEM CALL:


1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <unistd.h>
4. #include <sys/wait.h>
5.
6. int main() {
7. pid_t pid = fork();
8.
9. if (pid < 0) {
10. // Fork failed
11. perror("Fork failed");
12. exit(EXIT_FAILURE);
13. } else if (pid == 0) {
14. // Child process
15. printf("Child process: My PID is %d\n", getpid());
16. printf("Child process: Sleeping for 2 seconds...\n");
17. sleep(2); // Simulate some work with sleep
18. printf("Child process: Exiting...\n");
19. exit(EXIT_SUCCESS);
20. } else {
21. // Parent process
22. printf("Parent process: My PID is %d\n", getpid());
23. printf("Parent process: Waiting for child to finish...\n");
24.
25. int status;
26. wait(&status); // Wait for the child process to complete
27.
28. if (WIFEXITED(status)) {
29. printf("Parent process: Child exited with status %d\n",
WEXITSTATUS(status));
30. } else {
31. printf("Parent process: Child did not exit normally\n");
32. }
33.
34. printf("Parent process: Exiting...\n");
35. }
36.
37. return 0;
38. }

OUTPUT:

VI. close(): The close system call closes a file descriptor that was previously obtained by the
open system call.
Name - Faizan Hassan
Sys ID - 2023486998
PROGRAM FOR “CLOSE()” SYSTEM CALL:
1. ##include <stdio.h>
2. #include <stdlib.h>
3. #include <unistd.h>
4. #include <fcntl.h>
5.
6. int main() {
7. // Open a file for writing (create it if it doesn't exist)
8. int file_descriptor = open("example.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
9. if (file_descriptor < 0) {
10. perror("Error opening file");
11. exit(EXIT_FAILURE);
12. }
13. // Write some data to the file
14. const char *data = "Hello, World!\n";
15. ssize_t bytes_written = write(file_descriptor, data, 14); // 14 is the length of
the data
16.
17. if (bytes_written < 0) {
18. perror("Error writing to file");
19. close(file_descriptor); // Close the file before exiting
20. exit(EXIT_FAILURE);
21. }
22. printf("Data written successfully.\n");
23. // Close the file
24. if (close(file_descriptor) < 0) {
25. perror("Error closing file");
26. exit(EXIT_FAILURE);
27. }
28. printf("File closed successfully.\n");
29. return 0;
30. }

OUTPUT:

VII. stat():The stat system call in Linux is used to obtain information about a file, such as its
size, permissions, last access time, and more.
PROGRAM FOR “STAT()” SYSTEM CALL:
Name - Faizan Hassan
Sys ID - 2023486998
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <sys/stat.h>
4. #include <time.h>
5.
6. int main() {
7. struct stat fileStat;
8. // Specify the file to get the information
9. const char *filename = "example.txt";
10. // Call stat() to get file information
11. if (stat(filename, &fileStat) < 0) {
12. perror("stat");
13. return 1;
14. }
15. // Print file information
16. printf("File: %s\n", filename);
17. printf("File size: %lld bytes\n", (long long)fileStat.st_size);
18. printf("Number of links: %hu\n", fileStat.st_nlink);
19. printf("File inode: %llu\n", (unsigned long long)fileStat.st_ino);
20. printf("File permissions: ");
21. printf((S_ISDIR(fileStat.st_mode)) ? "d" : "-");
22. printf((fileStat.st_mode & S_IRUSR) ? "r" : "-");
23. printf((fileStat.st_mode & S_IWUSR) ? "w" : "-");
24. printf((fileStat.st_mode & S_IXUSR) ? "x" : "-");
25. printf((fileStat.st_mode & S_IRGRP) ? "r" : "-");
26. printf((fileStat.st_mode & S_IWGRP) ? "w" : "-");
27. printf((fileStat.st_mode & S_IXGRP) ? "x" : "-");
28. printf((fileStat.st_mode & S_IROTH) ? "r" : "-");
29. printf((fileStat.st_mode & S_IWOTH) ? "w" : "-");
30. printf((fileStat.st_mode & S_IXOTH) ? "x" : "-");
31. printf("\n");
32. // Print the last access time
33. printf("Last access time: %s", ctime(&fileStat.st_atime));
34. // Print the last modification time
35. printf("Last modification time: %s", ctime(&fileStat.st_mtime));
36. // Print the last status change time
37. printf("Last status change time: %s", ctime(&fileStat.st_ctime));
38. return 0;
39. }

OUTPUT:

VIII. opendir(): The opendir system call in Linux is used to open a directory for reading. It
returns a pointer to a DIR structure, which represents the directory stream.

PROGRAM FOR “OPENDIR()” SYSTEM CALL:


1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <dirent.h>

Name - Faizan Hassan


Sys ID - 2023486998
4.
5. int main() {
6. // Specify the directory to open
7. const char *dirname = "OS"; // Replace "OS" with the directory you want to list
8. // Open the directory
9. DIR *dir = opendir(dirname);
10. if (dir == NULL) {
11. perror("opendir");
12. return EXIT_FAILURE;
13. }
14. // Read and print the directory contents
15. struct dirent *entry;
16. printf("Contents of directory %s:\n", dirname);
17. while ((entry = readdir(dir)) != NULL) {
18. printf("%s\n", entry->d_name);
19. }
20. // Close the directory
21. if (closedir(dir) < 0) {
22. perror("closedir");
23. return EXIT_FAILURE;
24. }
25. return EXIT_SUCCESS;
26. }

OUTPUT:

IX. readdir():The readdir system call in Linux is used to read directory entries from an open
directory stream.

PROGRAM FOR “READDIR()” SYSTEM CALL:


1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <dirent.h>
4.
5. int main() {
6. // Specify the directory to open
7. const char *dirname = "."; // "." means the current directory
8. // Open the directory
9. DIR *dir = opendir(dirname);
10. if (dir == NULL) {
11. perror("opendir");
12. return EXIT_FAILURE;
13. }
14. // Read and print the directory contents
15. struct dirent *entry;
Name - Faizan Hassan
Sys ID - 2023486998
16. printf("Contents of directory %s:\n", dirname);
17. while ((entry = readdir(dir)) != NULL) {
18. printf("%s\n", entry->d_name);
19. }
20. // Close the directory
21. if (closedir(dir) < 0) {
22. perror("closedir");
23. return EXIT_FAILURE;
24. }
25. return EXIT_SUCCESS;
26. }

OUTPUT:

Name - Faizan Hassan


Sys ID - 2023486998

You might also like