Experiment No.5
Explore the system calls in Linux.
Date of Performance: 13/05/2025
Date of Submission: 13/05/2025
Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25 (Even Semester)
Aim: Explore the system calls open, read, write, close, getuid, getgid, getegid, geteuid of
Linux.
Objective: When a program in user mode requires access to RAM or a hardware resource,
it must ask the kernel to provide access to that resource. This is done via a system call.
Theory:
getuid, geteuid - get user identity getgid,
getegid - get group identity
• getuid() returns the real user ID of the calling process.
• geteuid() returns the effective user ID of the calling process.
• getgid() returns the real group ID of the calling process.
• getegid() returns the effective group ID of the calling process.
All four functions shall always be successful and no return value is reserved to indicate an
error.
Unix-like operating systems identify a user within the kernel by a value called a user
identifier, often abbreviated to user ID or UID. The UID, along with the group identifier
(GID) and other access control criteria, is used to determine which system resources a user
can access.
Effective user ID
The effective UID (euid) of a process is used for most access checks. It is also used as the
owner for files created by that process. The effective GID (egid) of a process also affects
access control and may also affect file creation, depending on the semantics of the specific
kernel implementation in use and possibly the mount options used.
Open: Used to Open the file for reading, writing or both. Open() returns file descriptor 3
because when main process created, then fd 0, 1, 2 are already taken
by stdin, stdout and stderr. So first unused file descriptor is 3 in file descriptor table.
int open(const char *pathname, int flags);
Parameters
Path : path to file which you want to use
use absolute path begin with “/”, when you are not work in same directory of file.
Use relative path which is only file name with extension, when you are work in same
directory of file.
flags : How you like to use
O_RDONLY: read only, O_WRONLY: write only,O_RDWR: read and write,
O_CREAT: create file if it doesn’t exist
Close: Tells the operating system you are done with a file descriptor and Close the file
which pointed by fd. int close (int fd);
Parameter
fd :file descriptor
Return 0
on success.
-1 on error.
read: Read data from one buffer to file descriptor, Read size bytes from the file specified by
fd into the memory location.
size_t read (int fd, void* buf, size_t cnt);
Parameters
fd: file descripter
buf: buffer to read data from
cnt: length of buffer
Returns: How many bytes were actually read
return Number of bytes read on success
return 0 on reaching end of file
return -1 on error
return -1 on signal interrupt
Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25 (Even Semester)
Write data from file descriptor into buffer, Writes the bytes stored in buf to the file
specified by fd. The file needs to be opened for write operations size_t write (int fd, void*
buf, size_t cnt);
Parameters
fd: file descripter
buf: buffer to write data to
cnt: length of buffer
Returns: How many bytes were actually written
return Number of bytes written on success
return 0 on reaching end of file
return -1 on error
return -1 on signal interrupt
Code and Output:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
int main() {
int fd;
char buffer[100];
struct utsname sys_info;
// 1. Using open() to create a file
fd = open("example.txt", O_CREAT | O_WRONLY, 0644);
if (fd == -1) {
perror("Error opening file");
return 1;
}
// 2. Using write() to write to file
write(fd, "Hello, Linux System Calls!\n", 27);
// 3. Using close() to close the file
close(fd);
// 4. Using fork() to create a child process
pid_t pid = fork();
if (pid == 0) {
printf("Child Process: PID = %d\n", getpid());
exit(0);
} else {
printf("Parent Process: PID = %d, Child PID = %d\n", getpid(), pid);
}
// 5. Using uname() to get system information
Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2024-25 (Even Semester)
if (uname(&sys_info) == 0) {
printf("System Name: %s\n", sys_info.sysname);
printf("Kernel Version: %s\n", sys_info.release);
}
return 0;
}
Conclusion: Comment on the kernel mode and user mode of the operating system.
Kernel mode provides the operating system full access to system resources, allowing it to
manage hardware and perform critical tasks. User mode, on the other hand, is where
applications run with limited access to resources, ensuring security and system stability.
The transition between these modes occurs through system calls, ensuring that user
programs can safely interact with the operating system without compromising the system's
integrity.