System Calls: A Beginner's Guide to OS

Introduction:

In the amazing world of computers, system calls are like secret passwords that allow programs to talk to the operating system and ask for special favors. Just like you might ask your parents for permission to do something, programs use system calls to request services from the powerful operating system. Today, we're going to dive into the world of system calls, explore their different types, and see how they make our computers work seamlessly.

Code Example 1: Creating and Running New Programs



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
int pid;
pid = fork();
if (pid < 0) {
printf("fork failed");
exit(1);
} else if (pid == 0) {
execlp("whoami", "whoami", NULL);
exit(0);
} else {
printf("\n Process id is-%d\n", getpid());
wait(NULL);
exit(0);
}
return 0;
}


In this example, we use the 'fork()' system call to create a new program, called a child process. It's like making a copy of the original program (the parent process). The child process then uses the 'execlp()' system call to run the 'whoami' command, which tells us the username of the current user.

Meanwhile, the parent process waits for the child process to finish using the `wait()` system call. This ensures that the parent process doesn't exit too early, giving the child process time to complete its task.

Output:


The output shows the process ID of the parent process ('Process id is-8258'), followed by the output of the 'whoami' command run by the child process, which displays the username.

Code Example 2: Working with Files


#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

int main() {
int fd[2];
char buf1[25] = "just a test\n";
char buf2[50];
fd[0] = open("file1", O_RDWR);
fd[1] = open("file2", O_RDWR);
write(fd[0], buf1, strlen(buf1));
printf("\n Enter the text now....");
scanf("\n %s", buf1);
write(fd[0], buf1, strlen(buf1));
lseek(fd[0], SEEK_SET, 0);
read(fd[0], buf2, sizeof(buf1));
write(fd[1], buf2, sizeof(buf2));
close(fd[0]);
close(fd[1]);
printf("\n");
return 0;
}


This code shows how system calls can help programs work with files. It creates two files, 'file1' and 'file2', using the 'open()' system call. It then writes some text to 'file1' using the 'write()' system call and asks you to enter some more text, which it also adds to 'file1'.

Next, the program uses the 'lseek()' system call to go back to the beginning of 'file1'. It then reads the contents of 'file1' into a buffer using the 'read()' system call. Finally, it writes the contents of this buffer to 'file2' using another 'write()' system call, effectively copying the data from 'file1' to 'file2'.


Output:




When you run this program and enter "vinodh" as the input, the program creates two files, 'file1' and 'file2'. The contents of 'file2' will be "just a test\nvinodh", as the program copies the contents of 'file1' (which includes the initial string and the user input) to 'file2'.


Exploring Different Types of System Calls:

System calls come in different flavors, each with its own special purpose:


1. Process Control: These system calls help create, manage, and control programs (processes). Examples include 'fork()', 'exec()', 'wait()', and 'exit()'. They allow programs to create new programs, run other programs, wait for programs to finish, and exit programs.


2. File Management: These system calls help programs work with files. Examples include 'open()', 'read()', 'write()', and 'close()'. They allow programs to create, open, read from, write to, and close files.


3. Device Management: These system calls help programs communicate with devices like printers, scanners, or cameras. Examples include 'ioctl()', 'read()', and 'write()'. They allow programs to send instructions to devices, read data from devices, and write data to devices.


4. Information Maintenance: These system calls provide programs with important information about the computer system. Examples include 'getpid()', 'gettimeofday()', and 'uname()'. They can give programs information like process IDs, system time, and details about the operating system.


5. Communication: These system calls help programs talk to each other or communicate over a network. Examples include 'pipe()', 'shmget()', and 'socket()'. They allow programs to share data, coordinate their activities, or interact with other computers.


Conclusion:

In this beginner's guide, we've explored the fascinating world of system calls and how they act as secret passwords that allow programs to communicate with the operating system. From creating new programs to working with files and devices, system calls are the building blocks that make our computers work smoothly.


By understanding system calls, we can appreciate the intricate mechanisms that power our programs and unlock new possibilities for creating amazing software. Whether you're a curious learner or aspiring programmer, diving into system calls is an exciting journey that reveals the inner workings of computers and empowers you to harness their full potential.

Post a Comment

Previous Post Next Post