System Calls examples - aryanjoshi0823/5143-Operating-System GitHub Wiki

Types of System Calls:

1. File System Operations

System calls under this category enable manipulation of files, such as creation, reading, writing, and deletion.

Common System Calls:

  • open(): Opens a file for reading or writing (e.g., text, audio).
  • read(): Reads data from a file into memory.
  • write(): Writes or updates data in a file.
  • close(): Closes an open file to release resources.
  • seek(): Moves the file pointer to a specific position within a file.

Algorithm for Reading a File:

Input: File descriptor, buffer address in user process, bytes to read  
Output: Bytes copied into user space  

1. Get file table entry from user file descriptor.  
2. Check file accessibility.  
3. Set parameters for user address, byte count, and I/O.  
4. Lock the file's inode.  
5. Set file byte offset from file table.  
6. While the read count is not satisfied:  
   a. Convert file offset to disk block.  
   b. Calculate offset within the block and number of bytes to read.  
   c. Read block data using breada or bread algorithms.  
   d. Copy data from buffer to user address.  
   e. Update offsets and counters.  
7. Unlock inode and update file table offset.  
8. Return total bytes read.  

2. Process Control

These system calls manage processes, including creation, termination, and synchronization.

Common System Calls:

  • fork(): Creates a new child process by duplicating the parent process.
  • exec(): Replaces the current process with a new program.
  • wait(): Suspends the parent process until child processes finish execution.
  • exit(): Terminates the current process.
  • kill(): Sends signals to processes (e.g., to quit, reload, or terminate).

3. Memory Management

These calls handle memory allocation, deallocation, and management.

Common System Calls:

  • brk(): Sets the end of the process's heap to a specific address.
  • sbrk(): Adjusts the heap size incrementally (+ve/-ve).
  • mmap(): Maps files/devices into memory for efficient I/O.
  • munmap(): Unmaps memory-mapped files.
  • mlock()/munlock(): Locks/unlocks pages in memory to prevent swapping.

4. Interprocess Communication (IPC)

IPC system calls enable communication between processes through shared resources or messaging.

Common System Calls:

  • pipe(): Creates a unidirectional communication channel.
  • socket(): Creates a network socket for interprocess communication.
  • shmget(): Allocates shared memory for processes.
  • semget(): Allocates semaphores to manage shared resource access.
  • msgget(): Creates or accesses a message queue for structured communication.

5. Device Management

These calls interact with hardware devices or manage device states.

Common System Calls:

  • SetConsoleMode(): Configures console input/output modes (Windows-specific).
  • WriteConsole(): Writes output to the console.
  • ReadConsole(): Reads input from the console.
  • open(): Opens a device or file for access.
  • close(): Closes a device or file.