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
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
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.