Assignments on Linux System Calls (Difficulty Level: Beginner)
Here are assignments related to each category of Linux system calls to help understand their usage and functionality. These exercises will also give you practical experience with interacting with Linux system calls from user space.
1. Process Management
Assignment:
-
Task: Write a C program that:
-
Creates a child process using
fork(). -
In the child process, call
execve()to run a command (e.g.,lsorpwd). -
In the parent process, wait for the child to finish using
wait(). -
Print the process IDs of both the parent and the child.
Objective: Understand process creation, execution, and synchronization between parent and child processes.
2. File Operations
Assignment:
-
Task: Write a C program that:
-
Opens a file (you can create a new text file or use an existing one) with
open(). -
Writes some data to the file using
write(). -
Reads the data from the file using
read(). -
Closes the file using
close().
Objective: Learn how to open, write, read, and close files using system calls.
3. Memory Management
Assignment:
-
Task: Write a C program that:
-
Allocates a memory region using
mmap()and maps a file into memory. -
Modify the content of the mapped file.
-
Unmaps the file using
munmap().
Objective: Understand memory mapping and the mapping/unmapping process.
4. Signal Handling
Assignment:
-
Task: Write a C program that:
-
Creates a signal handler using
signal()to catchSIGINT(Ctrl+C). -
Prints a message whenever
SIGINTis received. -
In the main function, use
sleep()and wait for the signal.
Objective: Learn how to handle signals and write signal handlers.
5. Inter-Process Communication (IPC)
Assignment:
-
Task: Write a C program that:
-
Creates a pipe using
pipe(). -
Forks a child process.
-
The parent sends a message to the child process through the pipe using
write(). -
The child receives the message using
read()and prints it.
Objective: Understand basic IPC using pipes.
6. Networking
Assignment:
-
Task: Write a C program that:
-
Creates a server and client using
socket(). -
The server binds to a port and listens for incoming connections.
-
The client connects to the server and sends a message.
-
The server receives the message and sends a response back to the client.
-
Both server and client close the socket after communication.
Objective: Get hands-on experience with socket programming and basic client-server communication.
7. File System Management
Assignment:
-
Task: Write a C program that:
-
Creates a new directory using
mkdir(). -
Changes the current working directory to the new directory using
chdir(). -
Verifies the current directory using
getcwd(). -
Deletes the directory using
rmdir().
Objective: Understand basic file system management system calls like directory creation and removal.
8. User and Group Management
Assignment:
-
Task: Write a C program that:
-
Uses
getuid()andgetgid()to get the user and group IDs of the current process. -
Changes the user ID and group ID using
setuid()andsetgid()(for this, your program may need to run as a superuser). -
Prints the new user and group IDs.
Objective: Learn how user and group management system calls work.
9. Time Management
Assignment:
-
Task: Write a C program that:
-
Prints the current time using
time()orgettimeofday(). -
Makes the process sleep for 2 seconds using
sleep()ornanosleep(). -
Prints the current time again and calculates the difference.
Objective: Understand time-based system calls and how to work with time in Linux.
10. Filesystem and Disk Operations
Assignment:
-
Task: Write a C program that:
-
Creates a file and writes some data into it.
-
Calls
fsync()to ensure data is written to disk. -
Verifies that data is synced correctly by checking the contents before and after the
fsync()call.
Objective: Learn about file synchronization using fsync().
Bonus Challenge:
Assignment:
-
Task: Create a simple Linux shell that:
-
Reads user input as a command.
-
Uses
fork()to create a child process. -
Uses
execve()to run the command entered by the user. -
The shell should continue to prompt the user for commands until
exitis typed.
Objective: Combine various system calls like fork(), execve(), wait(), etc., into a practical project that mimics a basic shell.
Conclusion:
Each assignment is designed to focus on one aspect of system calls in Linux, from process management to networking, file operations, memory management, and signal handling. Completing these exercises will deepen your understanding of how user-space programs interact with the kernel through system calls. You can extend these assignments or modify them to explore more advanced use cases and features.