0% found this document useful (0 votes)
170 views

1

This document discusses several Linux system calls including fork(), getpid(), execvp(), opendir(), readdir(), stat(), wait(), and exit(). It provides code examples and output demonstrating how each system call works. The fork() and getpid() example creates a process tree with multiple child processes, each printing their process ID and parent process ID. The execvp() example replaces the current process with another program. The opendir() and readdir() example prints the names of files in a directory. The stat() example prints metadata about a file. The wait() example demonstrates the parent waiting for the child process. And the exit() example closes a file and terminates the process.

Uploaded by

sanjeev v
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views

1

This document discusses several Linux system calls including fork(), getpid(), execvp(), opendir(), readdir(), stat(), wait(), and exit(). It provides code examples and output demonstrating how each system call works. The fork() and getpid() example creates a process tree with multiple child processes, each printing their process ID and parent process ID. The execvp() example replaces the current process with another program. The opendir() and readdir() example prints the names of files in a directory. The stat() example prints metadata about a file. The wait() example demonstrates the parent waiting for the child process. And the exit() example closes a file and terminates the process.

Uploaded by

sanjeev v
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

The fork () & getpid () System Call

#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

The fork () & getpid () System Call


#include<unistd.h>
#include<stdio.h>
main()
{
int p1,p2,p3,p4,p5;
p1=fork();
if(p1==0)
{
printf("p2 %d\n",getpid());
printf("parent of p2 %d \n",getppid());
p2=fork();
if(p2==0)
{
printf("p4 %d\n",getpid());
printf("parent of p4 %d\n",getppid());
}
else
{
p2=fork();
if(p2==0)
{
printf("p5 %d\n",getpid());
printf("parent of p5 %d\n",getppid());
}
}
}
else
{
p1=fork();
if(p1==0)
{
printf("p3 %d\n",getpid());
printf("parent of p3 %d\n",getppid());
}
else
{
printf("p1 %d\n",getpid());
printf("parent of p1%d",getppid());
}
}
}
OUTPUT:
P2=31499
Parent of p2 31498
P3 31501
Parent of p3 31498
P1 31498
Parent of p1 23031
P4 31500
Parent of p4 31499

P5 31502
Parent of p5 31499

The fork () & getpid () System Call


#include <stdio.h>
#include <string.h>
#include <sys/types.h>
void main()
{
Pid_t pid;
fork();
pid=getpid();
if(pid == -1)
printf(\n Error in creating process );
else if(pid == 0)
printf("\nExecuting in child process, pid=%d and its parent pid = %d ", getpid(),getppid());
else
printf("\nExecuting in parent process,pid=%d \n",getppid());
}

Exec System Call


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
argv[0] = "ls"; argv[1] = "-l";
argv[2] = NULL;
execvp(argv[0], &argv[0]);
perror("exec failure");
exit(1);
}
OUTPUT:
[2csea21@localhost ~]$ cc exec.c
[2csea21@localhost ~]$ ./a.out
[2csea21@localhost ~]$ ./a.out

Open Directory, Read Directory


Here's a simple program that prints the names of the files in the current working directory:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int
main (void)
{
DIR *dp;
struct dirent *ep;
dp = opendir ("./");

//

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;

Stat system call


#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
int main(int argc,char *argv[])
{
if(argc!=2)
return 1;
struct stat filestat;
if(stat(argv[1],&filestat)<0)
return 1;
printf("\n information for %s\n",argv[1]);
printf(".......\n");
printf("file size:\t\t%d bytes\n",filestat.st_size);
printf("no of links:\t%d",filestat.st_nlink);
printf("file inode:\t\t%d\n",filestat.st_ino);
} output:
information for msg.c
.......
file size: 1066 bytes
no of links: 1file inode: 24384720

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.

You might also like