Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Ex .No.

3 Process Management using System Calls : Fork, Exit, Getpid, Wait, Close

(a) SYSTEM CALLS OF UNIX OPERATING SYSTEM- fork (), getpid (), exit ()
AIM
To write a simple program using fork, getpid, exit, exec commands

fork (), getpid (), exit ()


fork () system call is used to create processes. When a process (a program in
execution) makes a fork() call, an exact copy of the process is created. Now there are two
processes, one being the parent process and the other being the child process. getpid()
returns the process ID (PID) of the calling process. exit() terminates process execution. It
does clean up, release of resources and child process.
ALGORITHM
Step 1. Start
Step 2. Print the process id using getpid () system call.
Step 3. Call the fork () system call to create processes and it takes no arguments and
returns a process ID.
Step 4. If the process is equal to zero then goto Step 5 else Step 6
Step 5. Print the child process id and its parent process.
Step 6. Print the parent process id and its fork process.
PROGRAM

#include<stdio.h>
#include<unistd.h>
int main()
{
int pid,pid1,pid2;
pid=fork();
if(pid==-1)
{
printf(“ERROR IN PROCESS CREATION \n”);
exit(1);
}
if(pid!=0)
{
pid1=getpid();
printf(“\n the parent process ID is %d\n”, pid1);
}
else
{
pid2=getpid();
printf(“\n the child process ID is %d\n”, pid2);
}
}

OUTPUT
[exam100@LINUXSVR2 ~]$ cc fork.c
[exam100@LINUXSVR2 ~]$. /a.out

the child process ID is 2952

the parent process ID is 2951

RESULT
Thus the program for the system calls using fork, exec commands is created and
executed.

(b) SYSTEM CALLS OF UNIX OPERATING SYSTEM- wait ()

AIM
To perform wait () system call using c program.
wait()
The wait() causes the parent to wait for any child process to complete its execution.
ALGORITHM
Step 1: Start the execution
Step 2: Create process using fork and assign it to a variable
Step 3: Check for the condition pid is equal to 0
Step 4: If it is true print the value of i and terminate the child process
Step 5: If it is not a parent process has to wait until the child terminate
Step 6: Stop the execution
PROGRAM

#include<stdio.h>
int main()
{
int i=10;
int pid=fork();
if(pid==0)
{
printf("initial value of i %d \n ",i);
i+=10;
printf("value of i %d \n ",i);
printf("child terminated \n");
exit(0);
}
else
{
wait(0);
printf("value of i in parent process %d",i);
}
}
OUTPUT
[iicse80@LINUXSVR2 ~]$ vi wait1.c
[iicse80@LINUXSVR2 ~]$ cc wait1.c
[iicse80@LINUXSVR2 ~]$ ./a.out
initial value of i 10
value of i 20
child terminated
value of i in parent process 10

RESULT
Thus the program was executed and verified successfully.
(c) SYSTEM CALLS OF UNIX OPERATING SYSTEM: close
AIM
To write a C program using system calls - opendir, closedir, readdir.
opendir, readdir
opendir () function opens a directory stream corresponding to the directory name, and
returns a pointer to the directory stream. The stream is positioned at the first entry in
the . readdir() reads one dirent structure from the directory pointed at by fd into the
memory area pointed to by dirp. The parameter count is ignored; at most one dirent
structure is read. directory. closedir() function closes the directory stream associated
with dir. The directory stream descriptor dir is not available after this call.
ALGORITHM
Step 1: Start
Step 2: Enter directory Name
Step 3: Open the directory by using opendir()
Step 4: if there is no directory then display “Error Message”
Step 5: By using readdir() to display the data present in the directory.
Step 7: Close the directory by using closedir()
Step 8: Stop.
PROGRAM
#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);
}

OUTPUT
[iicse80@LINUXSVR2 ~]$ mkdir CSE
[iicse80@LINUXSVR2 ~]$ cd CSE
[iicse80@LINUXSVR2 CSE]$ cat>f1
os lab
[iicse80@LINUXSVR2 CSE]$ cd..
[iicse80@LINUXSVR2 ~]$ cc open.c
[iicse80@LINUXSVR2 ~]$ ./a.out
Enter directory Name CSE
f1
...

RESULT
Thus the program using opendir (), closedir (), readdir () has been successfully executed.
SAMPLE VIVA-VOCE QUESTIONS
1. Which system call does not return control to the calling point, on termination?
2. Which system call transforms executable binary file into a process?
3. Which of the following system call is used for opening or creating a file?
4. Which mode is used for opening a file in both reading and writing?
5. How open system call returns the file descriptor?

You might also like