You are on page 1of 38

OS Tutorial

BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus

S. H. Islam
hafizul@pilani.bits-pilani.ac.in

Room No. 6120 K (NAB)


UNIX File System

3
BITS Pilani, Pilani Campus
Directory System Calls
 getcwd()
 chdir()
 mkdir()
 rmdir()
 opendir()
 closedir()
 readdir()

4
BITS Pilani, Pilani Campus
mkdir()
• # include<sys/stat.h>
• int mkdir(const char *path, mode_t perms)
const char *path, /* Pathname */
mode_t perms /* file access permissions */

5
BITS Pilani, Pilani Campus
mkdir()
• The argument path is the pathname of the new
directory that is to be created.
• All intermediate directory names in the
pathname must already exist. Only the last
component of the pathname is actually created.
• This function creates a new, empty directory.

6
BITS Pilani, Pilani Campus
mkdir()
• The parameter perms specifies the permissions
to use.
• Upon successful completion, mkdir() shall return
0 (Zero).
• Otherwise, -1 shall be returned and errno is set
appropriately.
• If it is unsuccessful, no directory shall be created.

7
BITS Pilani, Pilani Campus
mkdir()--ERROR
Tag Meaning
EEXIST pathname already exists (not necessarily as a
directory).
ELOOP Too many symbolic links were encountered in
resolving pathname.
ENAMETOOLONG pathname was too long.

8
BITS Pilani, Pilani Campus
rmdir()
• An empty directory is deleted with rmdir()
system call.
• # include<sys/stat.h>
int rmdir(const char *path);
char *path /* Pathname */
• It returns 0 on success and -1 on error .

9
BITS Pilani, Pilani Campus
rmdir()--ERROR

Tag Meaning
EBUSY pathname is currently in use by the system or some
process that prevents its removal.
EFAULT pathname points outside your accessible address
space.
ENAMETOOLONG pathname was too long
ENOENT A directory component in pathname does not exist
ENOTEMPTY pathname, or a component used as a directory
in pathname, is not, in fact, a directory.

10
BITS Pilani, Pilani Campus
#include <dirent.h>#include <stdio.h>#include <unistd.h>#include
<stdlib.h> #include <errno.h>
int main(int argc, char *argv[])
{
int md, rd;
DIR *ds;
struct dirent *dir;
md = mkdir(argv[1], 0777);
if(md == 0)
printf("%s directory is created\n", argv[1]);
else
printf("%s directory is not created\n", argv[1]);
rd = rmdir(argv[2]);
if(rd == 0)
printf("%s directory is removed\n", argv[2]);
else
printf("%s directory is not removed\n", argv[2]);
}
11
BITS Pilani, Pilani Campus
opendir()
• # include<dirent.h>
DIR *opendir(const char *path);
const char *path /* directory pathname */
• It returns a DIR pointer on success.
• It returns NULL pointer on error

12
BITS Pilani, Pilani Campus
opendir()--ERROR

Tag Meaning
EACCES Permission denied.
ENFILE Too many files are currently open in the system.
ENOENT Directory does not exist, or name is an empty string.
ENOTDIR Name is not a directory.

13
BITS Pilani, Pilani Campus
dirfd()
• #include <sys/types.h>
• #include <dirent.h>
• int dirfd(DIR *dirp);
• The function dirfd() returns the file descriptor
associated with the directory stream dirp.
• On success, a nonnegative file descriptor is returned.
• On error, -1 is returned, and errno is set to indicate
the cause of the error.
Tag Meaning
EINVAL dirp does not refer to a valid directory stream. 14
BITS Pilani, Pilani Campus
DIR *dirp; /*ptr to open directory */
int fd; /* fd of open directory */
dirp = opendir("/etc");
if ( !dirp )
{
printf(“* report error *”);
}
else
{
fd = dirfd(dirp); /* Get fd of open directory */
}
15
BITS Pilani, Pilani Campus
closedir()
• # include<dirent.h>
int closedir(DIR *dirp);
DIR *dirp /*DIR pointer from opendir */
• It returns 0 on success
• It returns -1 on error
• Example:
EBADF  Invalid directory stream descriptor dirp.

16
BITS Pilani, Pilani Campus
#include <sys/types.h> #include <dirent.h>
int closedir(DIR *dirp);
void main()
{
DIR *dirp; /*ptr to open directory */
dirp = opendir("/abc");
if ( !dirp )
{
printf(“* report error *”);
}
else
{ /* Close the directory now */
if ( closedir(dirp) == -1 )
{
printf(“* error *”);
} 17
}} BITS Pilani, Pilani Campus
readdir()
• #include <dirent.h>
• struct dirent *readdir(DIR *dirp);
• The function readdir() allows an open directory to
be searched for one directory member at a time.

18
BITS Pilani, Pilani Campus
readdir()
• The input to readdir() is simply a pointer to an open DIR
structure provided by opendir().
• The value returned is a pointer to the structure dirent,
or a NULL pointer if it fails or reaches the end of the
directory.

19
BITS Pilani, Pilani Campus
readdir()--ERROR

Tag Meaning
EFAULT Argument points outside the calling process’s address space.
EINVAL Result buffer is too small.
ENOENT No such directory.

20
BITS Pilani, Pilani Campus
int main(int argc, char *argv[])
{
DIR *ds;
struct dirent *dir;
ds = opendir(argv[1]);
if(ds == NULL)
printf("directory %s is not opened\n",argv[1]);
else
printf("ds = %ld\n",ds);
printf("list of files and directories\n");
while((dir = readdir(ds)) != NULL)
printf("%s\n",dir->d_name);
if((cld = closedir(ds)) == 0)
printf("%s is successfully closed\n",argv[1]);
else
printf("%s is not successfully closed\n",argv[1]); 21
} BITS Pilani, Pilani Campus
#include<stdio.h> #include<dirent.h>
struct dirent *dptr;
int main(int argc, char *argv[])
{
char buff[256];
DIR *dirp;
printf("\n\n Enter directory Name");
scanf("%s",buff);
if((dirp=opendir(buff))==NULL)
{
printf("Error");
exit(1);
}
while(dptr=readdir(dirp))
{
printf("%s\n",dptr->d_name);
}
closedir(dirp);
}
22
BITS Pilani, Pilani Campus
Resources
• http://www.gnu.org/software/libc/manual/html_node/Opening-
a-Directory.html
• http://linux.die.net/man/3/opendir
• http://man7.org/linux/man-pages/man3/dirfd.3.html
• http://www.cs.cf.ac.uk/Dave/C/node20.html
• http://pubs.opengroup.org/onlinepubs/009695399/functions/re
addir.html
• http://www.thegeekstuff.com/2012/06/c-directory/
• http://www.tutorialspoint.com/unix_system_calls/readdir.htm
• http://www.tutorialspoint.com/unix_system_calls/readdir.htm

23
BITS Pilani, Pilani Campus
System calls
on
Process management

24
BITS Pilani, Pilani Campus
System calls: Process
• fork()
• wait()
• exit()
• sleep()
• exec family [execl, execv, execle, execve,
execlp, execvp]

25
BITS Pilani, Pilani Campus
Process
• A process is a program at the time of
execution.
• The fork() system call creates a new process.
• The process that involves fork() is called the
parent process and the newly created process
is called the child process.
pid =fork();
• pid is the process id of child.

26
BITS Pilani, Pilani Campus
Process
The kernel does the following operations for
fork:
• It makes an entry in the process table for the
new process.
• It assign a unique id to the child process.

27
BITS Pilani, Pilani Campus
Process
• It makes a logical copy of the context of the
parent process.
• It returns the id of child process to the parent
process.
• A zero(0) value to the child process on success
or -1 to the parent process on failure.

28
BITS Pilani, Pilani Campus
Process termination
Generally the process terminates when
execution finished.
Some other reasons are:
– Time slot expire.
– Memory violation.
– I/O failure.
– Parent termination.
– Invalid instruction.

29
BITS Pilani, Pilani Campus
fork()
• If fork() returns a negative value, the creation
of a child process was unsuccessful.
• fork() returns a zero to the newly created
child process.
• fork() returns a positive value, the process
id of the child process, to the parent.

30
BITS Pilani, Pilani Campus
fork()
• The returned process id is of
type pid_t defined in sys/types.h.
• A process can use function getpid() to
retrieve the process id assigned to this
process.

31
BITS Pilani, Pilani Campus
fork()
If the call to fork() is executed
successfully, Unix will
• make two identical copies of
address spaces, one for the
parent and the other for the
child.
• Both processes will start
their execution at the next
statement following
the fork() call. 32
BITS Pilani, Pilani Campus
fork()

33
BITS Pilani, Pilani Campus
#define MAX_COUNT 20
void ChildProcess(void); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
void main(void) {
pid_t pid; pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess(); }
void ChildProcess(void)
{ int i;
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d\n", i);
printf(“ * Child process is done *\n"); }
void ParentProcess(void)
{ int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i);
printf("*** Parent is done ***\n");
} 34
BITS Pilani, Pilani Campus
fork()

35
BITS Pilani, Pilani Campus
main()
{
int dd;
printf("%d: I am the parent. Remember my number!\n",
getpid());
printf("%d: I am now going to fork ... \n", getpid());
dd = fork();
if (dd != 0)
{ /* the parent will execute this code */
printf("%d: My child's pid is %d\n", getpid(), dd);
}
else /* dd== 0 */
{ /* the child will execute this code */
printf("%d: Hi! I am the child.\n", getpid());
}
printf("%d: like father like son. \n", getpid());
} 36
BITS Pilani, Pilani Campus
3088: I am the parent. Remember my number!
3088: I am now going to fork ...
3088: My child's pid is 3089
3088: like father like son.
3089: Hi! I am the child.
3089: like father like son.

37
BITS Pilani, Pilani Campus
38
BITS Pilani, Pilani Campus

You might also like