what OS level calls can be made in C language
In C programming, particularly when working in a Unix-like environment (such as Linux or macOS), there are several types of OS-level calls that you can make to interact with the operating system. These calls are typically provided through system libraries such as the C standard library (libc
) or directly through system calls. Here are some common categories of OS-level calls available in C:
1. File and Directory Operations
- Opening and Closing Files:
open()
: Opens a file descriptor.close()
: Closes a file descriptor.
- Reading and Writing Files:
read()
: Reads data from a file descriptor.write()
: Writes data to a file descriptor.
- File Control Operations:
lseek()
: Repositions the offset of a file descriptor.fcntl()
: Performs various control operations on a file descriptor.
- File Metadata Operations:
stat()
,fstat()
,lstat()
: Retrieves file status information.chmod()
,fchmod()
: Changes file permissions.chown()
,fchown()
: Changes file ownership.
2. Process Management
- Creating and Terminating Processes:
fork()
: Creates a new process by duplicating the calling process.execve()
: Replaces the current process image with a new one._exit()
,exit()
: Terminates the calling process.
- Waiting for Processes:
wait()
,waitpid()
: Waits for state changes in a child process.
- Process Control:
getpid()
: Retrieves the process ID of the calling process.getppid()
: Retrieves the parent process ID of the calling process.kill()
: Sends a signal to a process.
3. Memory Management
- Dynamic Memory Allocation:
malloc()
,calloc()
,realloc()
: Allocates memory on the heap.free()
: Deallocates previously allocated memory.
- Memory Mapping:
mmap()
: Maps files or devices into memory.munmap()
: Unmaps previously mapped memory.
4. Inter-process Communication (IPC)
- Pipes:
pipe()
: Creates a pair of file descriptors for inter-process communication.
- Message Queues:
msgget()
,msgsnd()
,msgrcv()
: Manages message queues.
- Semaphores:
semget()
,semop()
,semctl()
: Manages semaphore sets.
- Shared Memory:
shmget()
,shmat()
,shmdt()
,shmctl()
: Manages shared memory segments.
- Sockets:
socket()
,connect()
,bind()
,listen()
,accept()
,send()
,recv()
: Manages network communication.
5. Thread Management
- Creating and Managing Threads:
pthread_create()
: Creates a new thread.pthread_join()
: Waits for a thread to terminate.pthread_exit()
: Terminates the calling thread.
- Thread Synchronization:
pthread_mutex_init()
,pthread_mutex_lock()
,pthread_mutex_unlock()
,pthread_mutex_destroy()
: Manages mutexes.pthread_cond_init()
,pthread_cond_wait()
,pthread_cond_signal()
,pthread_cond_destroy()
: Manages condition variables.
6. Signals
- Signal Handling:
signal()
: Sets a signal handler for a specific signal.sigaction()
: Examines and changes a signal action.
- Signal Management:
raise()
: Sends a signal to the calling process.sigprocmask()
: Examines and changes blocked signals.
7. Time and Date
- Retrieving and Setting Time:
time()
,gettimeofday()
: Retrieves the current time.settimeofday()
: Sets the system time.
- Timers:
alarm()
: Sets an alarm clock for signal delivery.sleep()
: Suspends execution for an interval.nanosleep()
: Suspends execution for an interval (with nanosecond precision).
8. User and Group Management
- Retrieving User and Group Information:
getuid()
,geteuid()
: Retrieves the real and effective user IDs.getgid()
,getegid()
: Retrieves the real and effective group IDs.getpwnam()
,getpwuid()
: Retrieves password file entry.getgrnam()
,getgrgid()
: Retrieves group file entry.
Example Program Using Some OS-Level Calls
Here is a simple C program that demonstrates some of these OS-level calls, such as opening a file, reading from it, and writing to standard output:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
ssize_t bytesRead;
char buffer[100];
// Open the file
fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
// Read from the file
bytesRead = read(fd, buffer, sizeof(buffer) - 1);
if (bytesRead == -1) {
perror("Error reading file");
close(fd);
exit(EXIT_FAILURE);
}
// Null-terminate the buffer and print it
buffer[bytesRead] = '\0';
printf("Read from file: %s\n", buffer);
// Close the file
if (close(fd) == -1) {
perror("Error closing file");
exit(EXIT_FAILURE);
}
return 0;
}
This program demonstrates file operations such as open()
, read()
, and close()
, as well as error handling with perror()
and standard output using printf()
. These are just a few examples of the many OS-level calls available in C for interacting with the operating system.