0% found this document useful (0 votes)
152 views4 pages

Programs Using System Calls EX - NO: 2a Fork System Call Date: AIM

This document describes two programs that use UNIX system calls. The first program uses the fork() system call to create a child process from a parent process. The second program simulates the fork(), getpid(), and wait() system calls. It uses fork() to create a child process, getpid() to retrieve the process IDs, and wait() to wait for the child process to complete generating a Fibonacci series. Both programs execute successfully to demonstrate the use of these system calls.

Uploaded by

Akash G S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views4 pages

Programs Using System Calls EX - NO: 2a Fork System Call Date: AIM

This document describes two programs that use UNIX system calls. The first program uses the fork() system call to create a child process from a parent process. The second program simulates the fork(), getpid(), and wait() system calls. It uses fork() to create a child process, getpid() to retrieve the process IDs, and wait() to wait for the child process to complete generating a Fibonacci series. Both programs execute successfully to demonstrate the use of these system calls.

Uploaded by

Akash G S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAMS USING SYSTEM CALLS

EX.NO: 2a FORK SYSTEM CALL


DATE:

AIM

To write a UNIX C program to create a child process from parent process using fork()
system call.

ALGORITHM

Step 1: Start the program.


Step 2: Invoke a fork() system call to create a child process that is a duplicate of parent process.
Step 3: Display a message.
Step 4: Stop the program.

PROGRAM 1

#include<stdio.h>
#include<unistd.h>
main()
{
fork();
printf("Hello World\n");
}

OUTPUT

[it1@localhost ~]$ vi ex1a.c


[it1@localhost ~]$ cc ex1a.c
[it1@localhost ~]$ ./a.out
Hello World
Hello World
PROGRAM 2

#include<stdio.h>
#include<unistd.h>
main()
{
fork();
fork();
fork();
printf("Hello World\n");
}

OUTPUT

[it1@localhost ~]$ cc ex1aa.c


[it1@localhost ~]$ ./a.out
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

RESULT

Thus a UNIX C program to create a child process from parent process using fork()
system call is executed successfully.
EX.NO: 2b SIMULATION OF FORK, GETPID AND WAIT SYSTEM CALLS
DATE:

AIM
To write a UNIX C program simulate fork(),getpid() and wait() system call.

ALGORITHM

Step 1: Invoke a fork() system call to create a child process that is a duplicate of parent process.
Step 2: Retrieve the process id of parent and child process.
Step 3: If return value of the fork system call=0(i.e.,child process),generate the fibonacii series.
Step 4: If return value of the fork system call>0(i.e.,parent process),wait until the child
completes.

PROGRAM

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<conio.h>
main()
{
int a=-1,b=1,i,c=a+b,n,pid,cpid;
printf("\nenter no. of terms ");
scanf("%d",&n);
pid=getpid();
printf("\nparent process id is %d",pid);
pid=fork();
cpid=getpid();
printf("\nchild process:%d",cpid);
if(pid==0)
{
printf("\nchild is producing fibonacci series ");
for(i=0;i<n;i++)
{
c=a+b;
printf("\n%d",c);
a=b;
b=c;
}
printf("\nchild ends");
}
else
{
printf("\nparent is waiting for child to complete");
wait(NULL);
printf("\nparent ends");
}
}

OUTPUT
[it27@mm4 ~]$ cc fork.c
[it27@mm4 ~]$ a.out

enter no. of terms 5

parent process id is 8723


child process:8723
parent process id is 8723
child process:8732
child is producing fibonacci series
0
1
1
2
3
child endsparent is waiting for child to complete

RESULT

Thus a UNIX C program to simulate fork(),getpid() and wait() system call is executed
successfully.

You might also like