Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Outline

Iseek () system call Program


dup () system call Program
Process Hierachy
Fork() system call Program
wait() system call Program
Create an orphan Process
Create an zombie Process
lseek() system call
lseek() system call repositions the read/write file offset i.e., it changes the positions
of the read/write pointer within the file.
 In every file any read or write operations happen at the position pointed to by the
pointer.
lseek() system call helps us to manage the position of this pointer within a file.
Syntax
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
The first parameter is the file descriptor of the file, which you can get using open()
system call. the second parameter specifies how much you want the pointer to move
and the third parameter is the reference point of the movement i.e., beginning of
file(SEEK_SET), current position(SEEK_CUR) of pointer or end of file(SEEK_END).
lseek() system call
Examples:
lseek(fd,5,SEEK_SET) – this moves the pointer 5 positions ahead starting from the
beginning of the file 12345677788181891
lseek(fd,5,SEEK_CUR) – this moves the pointer 5 positions ahead from the current
position in the file
lseek(fd,-5,SEEK_CUR) – this moves the pointer 5 positions back from the current
position in the file
lseek(fd,-5,SEEK_END) -> this moves the pointer 5 positions back from the end of the
file
On success, lseek() returns the position of the pointer within the file as measured in
bytes from the beginning of the file. But, on failure, it returns -1.
lseek() system call
Program1: Program using lseek() system call that reads 10 characters from file
“seeking” and print on screen. Again read 10 characters and write on screen.
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h >
#include<sys/stat.h>
int main()
{ int n,f; char buff[10];
f=open("seeking",O_RDWR);
read(f,buff,10);
write(1,buff,10); read(f,buff,10);
write(1,buff,10);
lseek() system call
Program2: Program using lseek() system call that reads 10 characters from file
“seeking” and print on screen. Skip next 5 characters and again read 10 characters and
write on screen.
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h >
#include<sys/stat.h>
int main()
{ int n,f;
char buff[10];
f=open("seeking",O_RDWR);
read(f,buff,10);
write(1,buff,10);
lseek(f,5,SEEK_CUR);//skips 5 characters from the current position
read(f,buff,10);
write(1,buff,10);
}
dup() system call
It is used to duplicate a file descriptor. It creates a copy of the old file
descriptor to a new file descriptor.
The new file descriptor can be system-generated or a number of your
choice depending upon either you use dup() or dup2().
Syntax
#include<unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
Note: dup2() system call is same as dup() with the difference that here
you can give the value of new file descriptor of your own choice. If the
new file descriptor is already open, it is silently closed and then
reopened for reuse.
dup() system call
Program 1: Program for dup() system call in C to duplicate a file
descriptor.
//dup.c
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
int old_fd, new_fd;
old_fd=open("test.txt",O_RDWR);
printf("File descriptor is %d\n",old_fd);
new_fd=dup(old_fd);
printf("New file descriptor is %d\n",new_fd);
}
dup() system call
Program 2: Program to use dup2() system call in linux to duplicate a
file descriptor.
//dup2.c
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{ int old_fd, new_fd;
old_fd=open("test.txt",O_RDWR);
printf("File descriptor is %d\n",old_fd);
new_fd=dup2(old_fd,7);
printf("New file descriptor is %d\n",new_fd);
}
fork() system call
fork() is a system call used to create a new process.
The new process is called a child process and the original process is
called the parent process.
The child process by default is a duplicate of the parent process.
By duplicate we mean that the child process has the same code as the
parent process but the memory space of both the processes is separate.
Syntax :
                #include<unistd.h>
                pid_t fork(void);
fork() returns -1 on failure; On success it returns ‘0’ in the child
process and process-id of the child in the parent process.  
fork() system call
Program for fork() system call
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{ pid_t p;
printf("before fork\n");
p=fork();
if(p==0)//child
{
printf("I am child having id %d\n",getpid());
printf("My parent's id is %d\n",getppid());
}
Else//parent
{ printf("My child's id is %d\n",p);
printf("I am parent having id %d\n",getpid());
}
printf("Common\n");
}
wait() system call
 It makes the parent process wait for child process to finish and then
the parent continues its working from the statement after the wait().
 To be exact wait makes the parent wait for the child to change state.
 The state change can be : the child terminated; the child was stopped
by a signal; or the child was resumed by a signal.
Synopsis
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *wstatus);
Note: wait() can be used to make the parent wait for the child to
terminate(finish) but not the other way around.
wait() system call
Program for wait() system call which makes the parent process wait for the child to
finish.
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<sys/wait.h>
int main()
{ pid_t p;
printf("before fork\n");
p=fork();
if(p==0)//child
{ printf("I am child having id %d\n",getpid());
printf("My parent's id is %d\n",getppid()); }
else//parent
{ wait(NULL);
printf("My child's id is %d\n",p);
printf("I am parent having id %d\n",getpid());
}
printf("Common\n"); }
Create an orphan process
An orphan process is a process whose parent has finished. Suppose P1 and P2 are
two process such that P1 is the parent process and P2 is the child process of P1.
Now, if P1 finishes before P2 finishes, then P2 becomes an orphan process.
Program 1: Program to create an orphan process
#include<stdio.h >
#include<unistd.h>
#include<sys/types.h>
int main()
{ pid_t p;
p=fork();
if(p==0)
{ sleep(5); //child goes to sleep and in the mean time parent terminates
printf("I am child having PID %d\n",getpid());
printf("My parent PID is %d\n",getppid());
}
else
{ printf("I am parent having PID %d\n",getpid());
printf("My child PID is %d\n",p); }
}
Create a Zombie process
A zombie is a process which has terminated but its entry still
exists in the process table until the parent terminates normally or
calls wait().
Suppose you create a child process and the child finishes before
the parent process does.
If you run the ps command before the parent gets terminated
the output of ps will show the entry of a zombie process(denoted
by defunct ).
This happens because the child is no longer active but its exit
code needs to be stored in case the parent subsequently
calls wait.
Create a Zombie process
Program: To create a Zombie Process
//zombie.c
#include<stdio.h>
#include<unistd.h>
int main()
{    pid_t t;
   t=fork();
if(t==0)   
{        printf("Child having id %d\n",getpid());    
}
  else     {         printf("Parent having id %d\n",getpid());    
     sleep(15); // Parent sleeps. Run the ps command during this time   
 }

You might also like