Exploring Basic Linux System Calls
Classified in Computers
Written at on English with a size of 521.58 KB.
Problem Statement
Explore the usage of basic Linux system calls.
Process Management System Calls
The system calls to manage processes are:
fork()
: Used to create a new process.exec()
: Executes a new program.wait()
: Waits until the process finishes execution.exit()
: Exits from the process.
System calls used to get process IDs are:
getpid()
: Gets the unique process ID of the process.getppid()
: Gets the parent process's unique ID.
Stat System Call
The stat
system call retrieves information about files, specifically from their inodes. For instance, to get a file's size and name, you would typically use stat
. Let's illustrate with a C code snippet (src/ls1.c
):
#include<stdio.h>
#include<dirent.h>
#include<cstdlib>
struct dirent *dptr;
int main(int argc, char *argv[])
{
char buff[100];
DIR*dirp;
printf("\n\nANUBHAV-VERMA_SE-15_50\n\n");
printf("ENTER DIRECTORY NAME \n\n");
scanf("%s", buff);
if((dirp=opendir(buff))==NULL)
{
printf("The given directory does not exist");
exit(1);
}
while(dptr=readdir(dirp))
{
printf("%s\n",dptr->d_name);
}
closedir(dirp);
}
In this code:
opendir()
opens the directory.readdir()
reads each directory entry.closedir()
closes the directory.
Deleting Directories
Two commands delete directories in Linux:
rmdir
: Deletes an empty directory.$ rmdir <directory name>
rm -rf
: Deletes a directory and its contents recursively.$ rm -rf <directory name>
Caution: Use
rm -rf
with extreme care, as it permanently deletes data.