Function Calls for Interaction Between C Language and Operating Systems

Function Calls for Interaction Between C Language and Operating Systems

When developing applications, the C language is often used for direct interaction with the operating system. This capability makes C a preferred language for many system-level programming and embedded development tasks. This article will introduce how to call relevant operating system functions using C to achieve basic file reading and writing, process management, and memory management functionalities.

1. File Operations

In C, the standard library provides a series of functions for file operations, allowing us to open, read, write, and close files. The most commonly used functions include:

  • <span>fopen</span>: Opens a file
  • <span>fread</span>: Reads data
  • <span>fprintf</span>: Formats output to a file
  • <span>fclose</span>: Closes an opened file

Example Code

Below is a simple example demonstrating how to use these functions to read from and write to a text file.

#include <stdio.h>
int main() {    // Open or create a text file for writing    FILE *file = fopen("example.txt", "w");    if (file == NULL) {        perror("Unable to open file");        return -1;    }
    // Write content to the file    fprintf(file, "Hello, World!\n");
    // Close the file    fclose(file);
    // Open the text file for reading     file = fopen("example.txt", "r");
    if (file == NULL) {        perror("Unable to open file");         return -1;      }
     char buffer[100];
     // Read a line from the file      while (fgets(buffer, sizeof(buffer), file)) {          printf("%s", buffer);     } 
     // Close the file again     fclose(file); 
   return 0;}

Compile and Run

You can compile and run the above code using the following command:

gcc -o file_example file_example.c
./file_example

After executing this code, a text file named <span>example.txt</span> will be created, into which a line of text will be written, and then read and displayed from this text file.

2. Process Management

The C language also provides some system calls for process management, such as <span>fork()</span> and <span>exec()</span> functions, which can be used to create new processes and execute different programs. Generally, a child process is first created using <span>fork()</span>, and then the exec function is called in the child process to replace its current image.

Example Code

Here is a simple example that uses <span>fork()</span> to create a new process:

#include <stdio.h>
#include <unistd.h>
int main() {    pid_t pid;
    pid = fork(); // Create a new process
    if (pid < 0) { // Error handling, if fork fails it returns a negative value         perror("Fork failed");       return -1; 
   } else if (pid == 0) { // Logic in the child process (pid is 0)        printf("This is the child process\n");
   } else { // Logic in the parent process (pid is greater than zero)       printf("This is the parent process, child process ID is %d\n", pid);   }
   return 0;}

Compile and Run

Similarly, you can compile and run this program using the following command:

gcc -o process_example process_example.c
./process_example

Depending on the situation, this program may output different messages from either the parent or child process.

3. Memory Management

Using some functions from the C standard library, we can dynamically allocate and free memory, which is a typical method of interacting with the operating system. In this process, we mainly use the following functions:

  • <span>malloc</span>: Allocates a block of memory of a specified size in bytes.
  • <span>free</span>: Frees the space previously allocated to the pointer.

Example Code

Below is a small example of dynamic memory allocation, allocating memory using malloc and freeing it afterward.

#include <stdio.h>
#include <stdlib.h>
int main() {
   int *arr; 
   arr = malloc(5 * sizeof(int)); 
   if (arr == NULL) {         fprintf(stderr, "Memory allocation failed.\n");             return EXIT_FAILURE;               }
for(int i=0;i<5;i++){arr[i]=i;}  
for(int i=0;i<5;i++){printf("%d ",arr[i]);}  
free(arr);           
return EXIT_SUCCESS;}

Compile and Run

As mentioned earlier, you can compile this example as follows:

gcc -o memory_example memory_example.c
./memory_example

This emphasizes the importance of dynamic allocation and cleanup; always remember to free memory when you no longer need it to avoid potential leak issues.

Conclusion

Through this article, we have learned how to use C language functions to interact with the operating system for file control, process management, and memory management. We hope this helps you understand the C language and its underlying principles better.

Leave a Comment