KEMBAR78
Systemcall1 | PPTX
SYSTEM CALL
PRESENTATION ON
By:
Pavitra Malpani
Kavita Lodhi
ME I Year
Computer Engineering
 What is System Call?
 System call implementation.
 Examples on how system calls are used.
 System call categories.
 Studying of various system calls.
 Copying a file.
 Demo for open system call.
 Creating a process.
What is system call?
• System calls provide an interface between a process
and the operating system.
• All operating systems provides some mechanism to
request services from the kernel.
• This service request points are called ‘system calls’.
System call implementation
• Implementing system calls requires a control transfer
from user space to kernel space.
• System calls are processed in privileged mode and the
current mode is user mode.
• A typical way to implement this is to use a
software interrupt or trap.
Examples on how system calls are
used
• In linux system, there is an interface layer between the
user space and kernel space. This layer consists of
library made up of functions that communicate
between normal user applications and the kernel.
• This is standard C library interface which is called as
libc/glibc.
• This provides wrapper functions for system calls.
• For example, the ‘printf’
statement. The C library
interprets this call and
invokes the necessary
system call(s) in the
operating system in this
instance , the write() system
call.
• The C library takes the
value returned by write()
and passes it back to the
user program.
System call categories
• Process management
• File management
• Device management
• Information maintenance
• Communication
• load, execute
• create process, terminate process
• end, abort
• get / set process attributes
• wait for time, wait event, signal event
• allocate and free memory
• Example:
• Fork()
• Wait()
• Exit()
• create file, delete file
• open, close
• read, write, reposition
• get file attributes, set file attributes
• Example:
• Create()
• Open()
• Write()
• Read()
• request device, release device
• read, write, reposition
• get device attributes, set device attributes
• logically attach or detach devices
• Example :
• read()
• write()
• ioctl()
• get / set date or time
• get / set system data
• get / set process, file, or device attributes
• Number of user currently in is
• Version of os kernel
• Example :
• alarm()
• sleep()
• creat, delete communication connection
• send, receive messages
• transfer status information
• attach or detach remote devices
• Example :
• Pipe()
• Msgnet()
• Msgsnd()
• Msgrcv()
• Shmat()
• Shmat()
• Shmctl()
Now we will start with the study of the following
system calls-
Copying a file:
• Read()
• Write()
• Open()
• Close()
Creating a process:
• Fork()
• Execl()
• Wait()
• Exit()
Studying of various system calls
Copying a file
open() system call: open and possibly create a file or
device
Synopsis
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
Handling open system call
close() system call: close a file descriptor
Synopsis
#include <unistd.h>
int close(int fd);
read() system call: read from a file descriptor
Synopsis
#include <unistd.h>
ssize_t read(int fd, void*buf, size_t count);
write() system call: write to a file descriptor
Synopsis
#include <unistd.h>
ssize_t write(int fd, void*buf, size_t count);
Sample C program
mycopy2.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main (){
int fd, read_bytes;
char buff[256];
fd = open(“demo.txt",
O_RDWR);
if (fd < 0)
printf("Error opening filen");
printf("FD (%d)n", fd);
read_bytes = read(fd,
buff, 15);
if (read_bytes < 0)
printf("Read Errorn");
printf("File
contentsn");
printf("%sn", buff);
strcpy(buff,
"testing_write_system
_call");
read_bytes = write(fd,
buff, 20);
close(fd);
}
DEMO
How does the open system call work ?
Consider the program:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char *argv)
{
int fd = open("test", O_RDONLY);
if fd < 0 {
perror("Opening of the file is
failedn");
}
else {
printf("file sucessfully
openedn");
}
close(fd);
return 0;
}
The open is the function from standard library, but not
system call. The standard library will call related system
call for us. The open call will return a file descriptor
which is just a unique number within our process which
is associated with the opened file.
Definition of the open system call
System calls are defined with the help
of SYSCALL_DEFINE macro. Definition of the open system
call is located in the fs/open.c source code file.
SYSCALL_DEFINE3(open, const char __user * filename, int
flags, umode_t mode)
{
if (force_o_largefile())
flags |= O_LARGEFILE;
return do_sys_open(AT_FDCWD, filename, flags, mode);
}
long do_sys_open(int dfd, char
*filename, int flags, umode_t
mode)
{
struct open_flags op;
int fd = build_open_flags(flags,
mode, &op);
struct filename *tmp;
if (fd)
return fd;
tmp = getname(filename);
if (IS_ERR(tmp))
return PTR_ERR(tmp);
fd =
get_unused_fd_flags(flags);
if (fd >= 0) {
struct file *f =
do_filp_open(dfd, tmp,
&op);
if (IS_ERR(f)) {
put_unused_fd(fd);
fd = PTR_ERR(f);
} else {
fsnotify_open(f);
fd_install(fd, f);
}
}
putname(tmp);
return fd;
}
build_open_flags function is defined in the same kernel file
and takes three arguments:
flags - flags that control opening of a file.
mode - permissions for newly created file.
op- is represented with the open_flags structure:
struct open_flags
{
int open_flag;
umode_t mode;
int acc_mode;
int intent;
int lookup_flags;
};
This is defined in the fs/internal.h.
Actual opening of a file
tmp = getname(filename);
if (IS_ERR(tmp))
return PTR_ERR(tmp);
The getname function is defined in the fs/namei.c source
code file and looks:
struct filename *
getname(const char __user * filename)
{
return getname_flags(filename, 0, NULL);
}
The next step after a filename will be copied to kernel
space is getting of new non-busy file descriptor:
fd = get_unused_fd_flags(flags);
The get_unused_fd_flags function takes table of open
files of the current process, minimum (0) and maximum
(RLIMIT_NOFILE) possible number of a file descriptor
in the system and flags that we have passed to the open
system call and allocates file descriptor and mark it busy
in the file descriptor table of the current process.
The last and main step in the do_sys_open is the
do_filp_open function. This function is defined in the
fs/namei.c linux kernel source code file and starts from
initialization of the nameidata structure. This structure
will provide a link to a file inode. After the nameidata
structure will be initialized, the path_openat function will
be called:
filp = path_openat(&nd, op, flags | LOOKUP_RCU);
if (unlikely(filp == ERR_PTR(-ECHILD)))
filp = path_openat(&nd, op, flags);
if (unlikely(filp == ERR_PTR(-ESTALE)))
filp = path_openat(&nd, op, flags |
LOOKUP_REVAL);
• The path_openat function calls path_init funtion. This
function performs some preporatory work before actual
path lookup. Like search of start position of path traversal.
• The next step after the path_init is the loop which
executes the link_path_walk and do_last.
• Link_path_walk function starts process of walking along a
given path.
• After the link_path_walk will be executed,
the do_last function will populate a file structure based on
the result of the link_path_walk.
• As we reached last component of the given file path
the vfs_open function from the do_last will be called.
• This function is defined in the fs/open.c linux kernel
source code file and the main goal of this function is to
call an open operation of underlying filesystem.
 Four system calls are provided for creating a process, ending
a process, and waiting for a process to complete.
 These system calls are:
1. execl()
2. fork()
3. wait()
4. exit().
 Process is identified and managed via a process identifier
(pid).
Process Creation
 The UNIX system calls that transform a executable binary file
into a process are the "exec" family of system calls.
 Synopsis:
int execl( const char *path, const char *arg, ...);
 execl- takes the path name of a binary executable as its first
argument, the rest of the arguments are the command line
arguments ending with a NULL.
 Example: execl("./a.out", NULL)
 A call to fork() will create a completely separate sub-process
which will be exactly the same as the parent.
 The UNIX system does not create a new process in response
to an exec system call.
 To create a new process, you must use the fork() system call.
 Synopsis:
#include <unistd.h>
pid_t fork(void);
Wait()
wait()- wait for a child process to stop or terminate
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
Exit()
System function exit allows termination of
a process.
Synopsis :
#include <stdlib.h>
void exit(int status);
https://www.softprayog.in/programming/cre
ating-processes-with-fork-and-exec-in-
linux
Sample C Program
// parent.c: the parent program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
int main (int argc, char **argv)
{
int i = 0,status,ret,pid;
printf ("Parent: Hello, World!n");
pid = fork ();
if (pid == 0) {
// I am the child
execl ("./child", NULL);
}
// I am the parent
printf ("Parent: Waiting
for Child to complete.n");
if ((ret = waitpid (pid,
&status, 0)) == -1)
printf
("parent:errorn");
if (ret == pid)
printf ("Parent: Child
process waited for.n");
}
// child.c: the child program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char **argv)
{
int i, j;
/* Some arbitrary work
done by the child*/
printf ("Child: Hello,
World!n");
printf ("Child: Work
completed!n");
printf ("Child: Bye
now.n");
exit (0);
}
Executing the parent (and child)
$ # compile parent
$ gcc parent.c -o parent
$ #compile child
$ gcc child.c -o child
$ # run parent (and child)
$ ./parent
Parent: Hello, World!
Parent: Waiting for Child to complete.
Child: Hello, World!
Child: Work completed!
Child: Bye now.
Parent: Child process waited for.
$
Systemcall1
Systemcall1

Systemcall1

  • 1.
    SYSTEM CALL PRESENTATION ON By: PavitraMalpani Kavita Lodhi ME I Year Computer Engineering
  • 2.
     What isSystem Call?  System call implementation.  Examples on how system calls are used.  System call categories.  Studying of various system calls.  Copying a file.  Demo for open system call.  Creating a process.
  • 3.
    What is systemcall? • System calls provide an interface between a process and the operating system. • All operating systems provides some mechanism to request services from the kernel. • This service request points are called ‘system calls’.
  • 4.
    System call implementation •Implementing system calls requires a control transfer from user space to kernel space. • System calls are processed in privileged mode and the current mode is user mode. • A typical way to implement this is to use a software interrupt or trap.
  • 5.
    Examples on howsystem calls are used • In linux system, there is an interface layer between the user space and kernel space. This layer consists of library made up of functions that communicate between normal user applications and the kernel. • This is standard C library interface which is called as libc/glibc. • This provides wrapper functions for system calls.
  • 6.
    • For example,the ‘printf’ statement. The C library interprets this call and invokes the necessary system call(s) in the operating system in this instance , the write() system call. • The C library takes the value returned by write() and passes it back to the user program.
  • 7.
    System call categories •Process management • File management • Device management • Information maintenance • Communication
  • 8.
    • load, execute •create process, terminate process • end, abort • get / set process attributes • wait for time, wait event, signal event • allocate and free memory • Example: • Fork() • Wait() • Exit()
  • 9.
    • create file,delete file • open, close • read, write, reposition • get file attributes, set file attributes • Example: • Create() • Open() • Write() • Read()
  • 10.
    • request device,release device • read, write, reposition • get device attributes, set device attributes • logically attach or detach devices • Example : • read() • write() • ioctl()
  • 11.
    • get /set date or time • get / set system data • get / set process, file, or device attributes • Number of user currently in is • Version of os kernel • Example : • alarm() • sleep()
  • 12.
    • creat, deletecommunication connection • send, receive messages • transfer status information • attach or detach remote devices • Example : • Pipe() • Msgnet() • Msgsnd() • Msgrcv() • Shmat() • Shmat() • Shmctl()
  • 13.
    Now we willstart with the study of the following system calls- Copying a file: • Read() • Write() • Open() • Close() Creating a process: • Fork() • Execl() • Wait() • Exit() Studying of various system calls
  • 14.
    Copying a file open()system call: open and possibly create a file or device Synopsis #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode);
  • 15.
  • 16.
    close() system call:close a file descriptor Synopsis #include <unistd.h> int close(int fd); read() system call: read from a file descriptor Synopsis #include <unistd.h> ssize_t read(int fd, void*buf, size_t count);
  • 17.
    write() system call:write to a file descriptor Synopsis #include <unistd.h> ssize_t write(int fd, void*buf, size_t count);
  • 18.
    Sample C program mycopy2.c #include<sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <string.h> int main (){ int fd, read_bytes; char buff[256]; fd = open(“demo.txt", O_RDWR); if (fd < 0) printf("Error opening filen"); printf("FD (%d)n", fd); read_bytes = read(fd, buff, 15); if (read_bytes < 0) printf("Read Errorn"); printf("File contentsn"); printf("%sn", buff); strcpy(buff, "testing_write_system _call"); read_bytes = write(fd, buff, 20); close(fd); }
  • 20.
    DEMO How does theopen system call work ? Consider the program: #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> int main(int argc, char *argv) { int fd = open("test", O_RDONLY); if fd < 0 { perror("Opening of the file is failedn"); } else { printf("file sucessfully openedn"); } close(fd); return 0; }
  • 21.
    The open isthe function from standard library, but not system call. The standard library will call related system call for us. The open call will return a file descriptor which is just a unique number within our process which is associated with the opened file.
  • 22.
    Definition of theopen system call System calls are defined with the help of SYSCALL_DEFINE macro. Definition of the open system call is located in the fs/open.c source code file. SYSCALL_DEFINE3(open, const char __user * filename, int flags, umode_t mode) { if (force_o_largefile()) flags |= O_LARGEFILE; return do_sys_open(AT_FDCWD, filename, flags, mode); }
  • 23.
    long do_sys_open(int dfd,char *filename, int flags, umode_t mode) { struct open_flags op; int fd = build_open_flags(flags, mode, &op); struct filename *tmp; if (fd) return fd; tmp = getname(filename); if (IS_ERR(tmp)) return PTR_ERR(tmp); fd = get_unused_fd_flags(flags); if (fd >= 0) { struct file *f = do_filp_open(dfd, tmp, &op); if (IS_ERR(f)) { put_unused_fd(fd); fd = PTR_ERR(f); } else { fsnotify_open(f); fd_install(fd, f); } } putname(tmp); return fd; }
  • 24.
    build_open_flags function isdefined in the same kernel file and takes three arguments: flags - flags that control opening of a file. mode - permissions for newly created file. op- is represented with the open_flags structure: struct open_flags { int open_flag; umode_t mode; int acc_mode; int intent; int lookup_flags; }; This is defined in the fs/internal.h.
  • 25.
    Actual opening ofa file tmp = getname(filename); if (IS_ERR(tmp)) return PTR_ERR(tmp); The getname function is defined in the fs/namei.c source code file and looks: struct filename * getname(const char __user * filename) { return getname_flags(filename, 0, NULL); }
  • 26.
    The next stepafter a filename will be copied to kernel space is getting of new non-busy file descriptor: fd = get_unused_fd_flags(flags); The get_unused_fd_flags function takes table of open files of the current process, minimum (0) and maximum (RLIMIT_NOFILE) possible number of a file descriptor in the system and flags that we have passed to the open system call and allocates file descriptor and mark it busy in the file descriptor table of the current process.
  • 27.
    The last andmain step in the do_sys_open is the do_filp_open function. This function is defined in the fs/namei.c linux kernel source code file and starts from initialization of the nameidata structure. This structure will provide a link to a file inode. After the nameidata structure will be initialized, the path_openat function will be called: filp = path_openat(&nd, op, flags | LOOKUP_RCU); if (unlikely(filp == ERR_PTR(-ECHILD))) filp = path_openat(&nd, op, flags); if (unlikely(filp == ERR_PTR(-ESTALE))) filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
  • 28.
    • The path_openatfunction calls path_init funtion. This function performs some preporatory work before actual path lookup. Like search of start position of path traversal. • The next step after the path_init is the loop which executes the link_path_walk and do_last. • Link_path_walk function starts process of walking along a given path. • After the link_path_walk will be executed, the do_last function will populate a file structure based on the result of the link_path_walk. • As we reached last component of the given file path the vfs_open function from the do_last will be called. • This function is defined in the fs/open.c linux kernel source code file and the main goal of this function is to call an open operation of underlying filesystem.
  • 29.
     Four systemcalls are provided for creating a process, ending a process, and waiting for a process to complete.  These system calls are: 1. execl() 2. fork() 3. wait() 4. exit().  Process is identified and managed via a process identifier (pid).
  • 30.
  • 31.
     The UNIXsystem calls that transform a executable binary file into a process are the "exec" family of system calls.  Synopsis: int execl( const char *path, const char *arg, ...);  execl- takes the path name of a binary executable as its first argument, the rest of the arguments are the command line arguments ending with a NULL.  Example: execl("./a.out", NULL)
  • 32.
     A callto fork() will create a completely separate sub-process which will be exactly the same as the parent.  The UNIX system does not create a new process in response to an exec system call.  To create a new process, you must use the fork() system call.  Synopsis: #include <unistd.h> pid_t fork(void);
  • 33.
    Wait() wait()- wait fora child process to stop or terminate #include <sys/wait.h> pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); Exit() System function exit allows termination of a process. Synopsis : #include <stdlib.h> void exit(int status); https://www.softprayog.in/programming/cre ating-processes-with-fork-and-exec-in- linux
  • 34.
    Sample C Program //parent.c: the parent program #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> int main (int argc, char **argv) { int i = 0,status,ret,pid; printf ("Parent: Hello, World!n"); pid = fork (); if (pid == 0) { // I am the child execl ("./child", NULL); } // I am the parent printf ("Parent: Waiting for Child to complete.n"); if ((ret = waitpid (pid, &status, 0)) == -1) printf ("parent:errorn"); if (ret == pid) printf ("Parent: Child process waited for.n"); }
  • 35.
    // child.c: thechild program #include <stdio.h> #include <stdlib.h> #include <string.h> int main (int argc, char **argv) { int i, j; /* Some arbitrary work done by the child*/ printf ("Child: Hello, World!n"); printf ("Child: Work completed!n"); printf ("Child: Bye now.n"); exit (0); }
  • 36.
    Executing the parent(and child) $ # compile parent $ gcc parent.c -o parent $ #compile child $ gcc child.c -o child $ # run parent (and child) $ ./parent Parent: Hello, World! Parent: Waiting for Child to complete. Child: Hello, World! Child: Work completed! Child: Bye now. Parent: Child process waited for. $