Library Functions vs. System Calls
Library Functions vs. System Calls
Library Functions vs. System Calls
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
System Calls
When a program makes a system call, the arguments are packaged up and
handed to the kernel, the kernel then, takes over execution of the program
until the call completes.
A system call isnt an ordinary function call, and a special procedure is
required to transfer control to the kernel.
The GNU C library (the implementation of the standard C library provided
with
ith GNU/Linux
GNU/Li
systems)
t
) wraps Linux
Li
system
t
calls
ll with
ith functions
f ti
so that
th t
you can call them easily.
The set of Linux system calls forms the most basic interface between
programs and the Linux kernel. Each call presents a basic operation or
capability.
Note that a library function may invoke one or more other library functions
or system calls as part of its implementation.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
System Calls
When an error occurs in a system call:
The return value is generally set to -1
A global variable errno is set to a positive integer
This integer
Is associated with an error message
g
Is represented by a symbolic constant
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
access
stat
chmod
chown
chdir
chroot
And more
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
access
Can check
any combination of read, write, and execute permission,
files existence.
S
Syntax:
int access(const char *pathname, int mode);
Arguments
1. Path to the file to check
2. Bitwise or of R_OK, W_OK, and X_OK, corresponding to
read, write, and execute permission
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
access
return value
0 if the process has all the specified permissions.
-1 if the file exists but the calling process does not have the specified
permissions. The global variable, errno is set to EACCES (or
EROFS, if write permission was requested for a file on a read-only
file system).
If the second argument is F_OK, access simply checks for the files
existence.
If the file exists, the return value is 0; if not, the return value is 1
and errno is set to ENOENT
errno may instead be set to EACCES if a directory in the file path
is inaccessible
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
stat
Provides information regarding a file contained on its i-node, such
as
The file size,
The last modification time,
Permissions,
The owner.
Syntax:
int stat (const char * filename, struct stat *buf);
Parameters:
The path to the file and
A pointer to a variable of type struct stat. Set the second
parameter to a valid stat structure containing the following
information:
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
struct stat {
dev_t
ino_t
mode_t
nlink_t
uid_t
gid t
gid_t
off_t
blksize_t
blkcnt_t
time_t
time_t
time_t
};
st_dev;
/* device ID, for device files*/
st_ino;
/* inode number*/
st_mode; /* type & rights */
st_nlink; /* number of hard links */
st_uid;
/* user ID of owner */
st_gid;
st gid;
//* group ID of owner *//
st_size; /* total size, in bytes */
st_blksize; /* blocksize for filesystem I/O */
st_blocks; /* number of blocks allocated */
st_atime; /* time of last access */
st_mtime; /* time of last modification */
st_ctime; /* time of last change in inode */
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
stat
Return:
0: If the call to stat is successful and the fields of the structure are
filled with information about that file
-1 on failure
Few useful fields in struct stat:
st_mode contains the files access ppermissions and encodes the
type of the file in higher-order bits.
st_uid and st_gid contain the IDs of the user and group,
respectively, to which the file belongs.
st_size contains the file size, in bytes.
st_atime contains the time when this file was last accessed
(read or written).
st_mtime contains the time when this file was last modified
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
st_mode Macros
These macros check the value of the st_mode field value
to figure out what kind of file stat was invoked on.
A macro evaluates to true if the file is of that type.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
U1.
#
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
chmod
Purpose: change mode of file
The file permission bits of the file named specified by path
or referenced by the file descriptor fd are changed to mode.
The chmod() system call verifies that the process owner
(user) either owns the file specified by path (or fd), or is the
super-user.
The chmod() system call follows symbolic links to operate
on the target of the link rather than the link itself.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
chmod
Header: sys/types.h , sys/stat.h
Syntax
int chmod(const char *path, mode_t mode);
Parameters
Path: of the file whose mode is to be changed
Mode: created from or'd permission bit masks defined in
<sys/stat.h>
Return Value
0: successful completion; -1: unsuccessful; the global variable
errno is set to indicate the error.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
U1.
#
chown
Purpose: change owner and group of a file
The chown() system call changes the user and group
ownership of a file.
Only a user having appropriate privileges can change the
ownership of a file.
To get UID corresponding to a user name, use
struct password getpwnam (const char *name)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
chown
Header: unistd.h
Syntax:
int chown(const char *path, uid_t owner, gid_t
group);
Parameters:
path points to the path name of a file
owner ID and group ID of new owner & group
Return Value:
0: Successful completion.
-1: Failure. The owner and group of the file remain unchanged.
errno is set to indicate the error.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
chdir
Purpose: change working directory
Header: unistd.h
Syntax: int chdir(const char *path);
Parameters: the new directory path
Return Value:
0: On success
-1: on failure, errno is set appropriately.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
chroot
changes the apparent disk root directory for the current
running process and its children
This directory will be used for pathnames beginning with /.
The root directory is inherited by all children of the current
process.
A program that is re-rooted to another directory cannot
access or name files outside that directory
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
chroot
Header: unistd.h
Syntax: int chroot(const char *path);
Parameters: path of the new root directory
Return Value:
0: Successful completion.
-1: Failure. The owner and group of the file remain
unchanged. errno is set to indicate the error.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Additional
Try working with these additional File System
related System Calls corresponding to simple
operations
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Process Control
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
system
Header: stdlib.h
Syntax: int system (const char *command);
Parameters:
The command is executed by issuing /bin/sh -c
c command,
command
Return
returns the exit status of the shell command
If the shell itself cannot be run, system returns 127; if
another error occurs, system returns 1.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
U1.
#
fork()
When a program calls fork, a duplicate process, called the child
process, is created.
The parent process continues executing the program from the point
that fork was called.
The child process, too, executes the same program from the same
place.
Differentiation between parent and child can be done on basis of:
PID of the current process
Return value of fork.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Child / Parent
The child process is a new process and therefore has a new process ID,
distinct from its parents process ID.
One way for a program to distinguish whether its in the parent
process or the child process is to call getpid.
However, the fork function provides different return values to the parent
and child processes
one process goes in to the fork call, and two processes come out,
with different return values.
The return value in the parent process is the process ID of the child.
The return value in the child process is zero.
This can be used by the program to tell whether it is now running as
the parent or the child process.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Example
int main()
{
int pid;
printf(Before fork...\n);
pid= fork();
printf(Process Created\n);
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
10
{
int pid;
printf(Before fork:
\n);
\n
);
pid= fork();
if (pid==0)
printf(Child \n);
-1 in case
of Error
else
printf(Parent \n);
}
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
11
U1.
#
execl
int execl(const char *path, const char *arg0,
..., const char *argn, (char *)0);
int execle(const char *path, const char *arg0,
..., const char *argn, (char *)0, char
*const envp[]);
int execlp(const char *file, const char *arg0,
..., const char *argn, (char *)0);
int execlpe(const char *file, const char
*arg0, ..., const char *argn, (char *)0,
char *const envp[]);
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
12
execv
int execv(const char *path, char *const
argv[]);
int execve(const char *path, char *const
argv[], char *const envp[]);
int
i
t execvp(const
(
t char
h
*fil
*file, char
h
*
*const
t
argv[]);
int execvpe(const char *file, char *const
argv[], char *const envp[]);
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Process States
Child dies before parent =>
Child becomes (Z)ombie till it is not removed from process
table
Parent dies before parent =>
Child becomes (O)rphan temporarily
Inherited by PID 1
Can use (S)leep (n) to make a process wait for n seconds.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
13
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
wait
Used by a parent process to wait till a child process doesn't terminate.
Behaviour:
If the process calling wait doesn't have any child processes
wait returns -1
If the calling process has a Zombie child
PID of the Zombie is returned to the parent
Zombie child is removed from the process table
If the calling process has a child that hasn't terminated yet
Parent process is suspended till the child doesn't terminate
When the child terminates, a signal is received by the parent
that resumes its execution.
A zombie child, if present, is also removed at this point.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
14
Threads
Threads, like processes are mechanism to allow a program to do
more than one thing at a time.
As with processes,
threads appear to run concurrently;
the Linux kernel schedules them asynchronously,
asynchronously
interrupting each thread from time to time to give others a
chance to execute.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Threads Basics
Conceptually, a thread exists within a process.
Threads are a finer-grained unit of execution than processes. When
a program is invoked,
Linux creates a new process
thread which runs the
and in that process creates a single thread,
program sequentially.
This thread can create additional threads;
all these threads run the same program in the same process,
but each thread may be executing a different part of the
program at any given time.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
15
Threads Dont
When a program creates another thread, though, nothing is copied. The
creating and the created thread share
the same memory space,
file descriptors, and
other system resources as the original.
If one thread changes the value of a variable, for instance, the other thread
subsequently will see the modified value.
Similarly, if one thread closes a file descriptor, other threads may not read
from or write to that file descriptor.
Because a process and all its threads can be executing only one program at
a time, if any thread inside a process calls one of the exec functions, all the
other threads are ended (the new program may, of course, create new
threads).
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
pthreads
GNU/Linux implements the POSIX standard thread API (known
as pthreads).
All thread functions and data types are declared in the header file
<pthread.h>.
The pthread functions are not included in the standard C
library. Instead, they are in libpthread,
Thus, one needs to add -lpthread to the command line while
linking a program that creates threads.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
16
Thread Argument
On GNU/Linux, thread functions take a single parameter,
of type void*, and have a void* return type. The
parameter is the thread argument:
GNU/Linux passes the value along to the thread without
looking at it.
Your program can use this parameter to pass data to a
new thread.
Similarly, your program can use the return value to pass
data from an exiting thread back to its creator.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
pthread_create
The pthread_create function creates a new thread. The
parameters are:
1. A pointer to a pthread_t variable, in which the thread ID of
the new thread is stored.
2. A pointer to a thread attribute object. This object controls
details of how the thread interacts with the rest of the program.
If you pass NULL as the thread attribute, a thread will be
created with the default thread attributes. (Thread attributes are
beyond the scope of current discussion)
3. A pointer to the thread function. This is an ordinary function
pointer, of this type: void* (*) (void*)
4. A thread argument value of type void*. Whatever you pass
is simply passed as the argument to the thread function when
the thread begins executing.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
U1.
#
17
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
can
exit
i
explicitly
li i l
b
by
calling
lli
U1.
#
Further reading
clone to create threads / processes
And decide what is shared.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63, by Shalini Singh Jaspal
U1.
#
18