1
1
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
main()
{
int pid1,pid2,pid3,pid4;
pid1=fork();
if(pid1==0)
{
printf(first child id is%d,getpid());
printf(first parent id is%d,getppid());
pid2=fork();
if(pid2==0)
{
printf(2nd child id is%d,getpid());
printf(2nd parent id is %d,getppid());
pid3=fork();
if(pid3==0)
{
printf(3rd child id is%d,getpid());
printf(3rd parent id is%d,getppid());
pid4=fork();
if(pid4==0)
{
printf(4th child id is%d,getpid());
printf(4th parent id is%d,getppid());
}
}
}
}
else
{
printf(child id is%d,getpid());
printf(parent id is%d,getppid());
}
}
Output
First child id is 4545
First parent id is 4544
2nd child id is 4546
2nd parent id is 4545
3rd child id is 4547
3rd parent id is 4546
4th child idis 4548
4th parent id is 4547
Child id is 4544
Parent id is 24650
P5 31502
Parent of p5 31499
//
dp = opendir ("/home/cs4015/oslab");
if (dp != NULL)
{
while (ep = readdir (dp))
puts (ep->d_name);
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
return 0;
An example of wait():
#include <sys/types.h>
#include <sys/wait.h>
Void main()
{
int status;
pid_t pid;
pid = fork();
if(pid == -1)
printf(\nERROR child not created );
else if (pid == 0) /* child process */
{
printf("\n I'm the child!");
exit(0);
}
else /* parent process */ {
wait(&status);
printf("\n I'm the parent!")
printf("\n Child returned: %d\n", status)
}
}
EXIT PROGRAM
#include<unistd.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
int main()
{
int g;
g=creat("datafile.dat",S_IREAD/S_WRITE);
if(g==-1)
{
printf("error in opening datafile.dat");
}
else
{
printf("datafile.dat opened for read/write access");
printf("datafile.dat is currently empty");
}
close(g);
exit(0);
}
OUTPUT:
Thus datafile.dat opened for read or write access.
datafile.dat is currently empty.