Program
Program
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
#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
RESULT
Thus the program for the system calls using fork, exec commands is created and
executed.
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?