How to make the parent process wait till the completion of execution of child process. The following C program makes the parent process to wait till the completion of its child process.
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("%d\n",pid);
if(pid==0)
{
printf("From child process \n");
}
else
{
wait(0);
printf("From parent process\n");
}
}
Search This Blog
How to make Child process an Orphan Process
C Program to Show Process ID in Linux
This program is to show the process id (pid) in UNIX or Linx
The system call getpid() returns the process id of current process.
#include<stdio.h>
int main()
{
printf("\n Parent Process ID %d",getppid());
}
The system call getpid() returns the process id of current process.
#include<stdio.h>
int main()
{
printf("\n Parent Process ID %d",getppid());
}
Inter Process Communication using Named Pipes - Chat Program in C
In this post, we will see a chat program using named pipes (fifo). Pipes are used for communication between processes. The named pipes are fifos. They enable two way communication unlike ordinary pipes. But they are half duplex, i.e. communication can take place only in one direction at a time. The program is in 3 parts: pipe_creation.c, leftTerminal.c and rightTerminal.c.
pipe_creation.c
rightTerminal.c
How to run:
Run pipe_creation.c first. Then close it. Then run leftTerm.c. Without closing it, open a new terminal window and run rightTerm.c. Start typing from leftTerm.c and then in rightTerm.c.
Output :
pipe_creation.c:
pipeA created
pipeB is created
leftTerminal.c
Enter text:
hi
Hai
Enter text:
fine?
Ya.Sure
Enter text:
end
rightTerminal.c
hi
Enter text:
Hai
fine?
Enter text:
Ya.Sure
end
Enter text:
end
C Program to Simulate Round Robin CPU Scheduling Algorithm
C Program to Simulate Priority CPU Scheduling Algorithm
C Program to Simulate First Come First Serve (FCFS) CPU Scheduling Algorithm
C Program to Simulate rmdir Command or Delete Directory
C Program to Open, Read and Write Files
C Program to Create Process and Display Process ID - OS Lab Program
How to Use Fork and Exec System Calls
How to Use Exit System Call
C Program to make Parent Process Wait for Child to Terminate
C Program to Simulate GREP Command in Linux
C Program to make Child Process an Orphan Process
C Program to Show Process ID in Linux
C Program to Create a Process Using Fork System Call
pipe_creation.c
#include<stdio.h>
void main()
{
int f1,f2;
f1 = mkfifo("pipeA",0666);
if(f1<0)
printf("\npipeA was not created");
else
printf("\npipeA created");
f2 = mkfifo("pipeB",0666);
if(f2<0)
printf("\npipeB was not created");
else
printf("\npipeB is created\n");
}
leftTerminal.c
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char str[256]="start";
int fifo_write,fifo_read;
while(strcmp(str,"end")!=0)
{
fifo_write= open("pipeA",O_WRONLY);
if(fifo_write<0)
printf("\nError opening pipe");
else
{
printf("\nEnter text:\n");
scanf("%s",str);
write(fifo_write,str,255*sizeof(char));
close(fifo_write);
}
fifo_read=open("pipeB",O_RDONLY);
if(fifo_read<0)
printf("\nError opening write pipe");
else
{
read(fifo_read,str,255*sizeof(char));
close(fifo_read);
printf("\n%s",str);
}
}
rightTerminal.c
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char str[256]="start";
int fifo_read,fifo_write;
while(strcmp(str,"end")!=0)
{
fifo_read=open("pipeA",O_RDONLY);
if(fifo_read<0)
printf("\nError opening read pipe");
else
{
read(fifo_read,str,255*sizeof(char));
close(fifo_read);
printf("\n%s",str);
}
fifo_write=open("pipeB",O_WRONLY);
if(fifo_write<0)
printf("\nError opening write pipe");
else
{
printf("\nEnter text:\n");
scanf("%s",str);
write(fifo_write,str,255*sizeof(char));
close(fifo_write);
}
}
}
Run pipe_creation.c first. Then close it. Then run leftTerm.c. Without closing it, open a new terminal window and run rightTerm.c. Start typing from leftTerm.c and then in rightTerm.c.
Output :
pipe_creation.c:
pipeA created
pipeB is created
leftTerminal.c
Enter text:
hi
Hai
Enter text:
fine?
Ya.Sure
Enter text:
end
rightTerminal.c
hi
Enter text:
Hai
fine?
Enter text:
Ya.Sure
end
Enter text:
end
Related Posts:
C Program to Simulate ls Command in LinuxC Program to Simulate Round Robin CPU Scheduling Algorithm
C Program to Simulate Priority CPU Scheduling Algorithm
C Program to Simulate First Come First Serve (FCFS) CPU Scheduling Algorithm
C Program to Simulate rmdir Command or Delete Directory
C Program to Open, Read and Write Files
C Program to Create Process and Display Process ID - OS Lab Program
How to Use Fork and Exec System Calls
How to Use Exit System Call
C Program to make Parent Process Wait for Child to Terminate
C Program to Simulate GREP Command in Linux
C Program to make Child Process an Orphan Process
C Program to Show Process ID in Linux
C Program to Create a Process Using Fork System Call
Creating A Process in Linux (UNIX) - fork() Example Program
The system call fork() is used to create a new process in UNIX based operating systems and Linux systems. The fork() system call creates a child process when called from a parent process. Unix will make an exact copy of the parent's address space and give it to the child. Therefore, the parent and child processes have separate address spaces. Here is a C program which uses the fork() system call to create a process during execution. The program is commented well for better understanding.
Program
/*The program (parent process) reads an array from the user.
It sorts it in descending order.
The child process created by parent process sorts it ascending order
*/
#include<stdlib.h>
#include<stdio.h>
int main()
{
int cpid;
int temp,a[100],n,i,j;
printf("\nEnter number of elements : ");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\n");
cpid=fork(); /*Creating child process using fork() system call */
if(cpid<0)
{
printf("Cannot create a new process\n");
exit(0);
}
if(cpid != 0)
{
/*fork returns processid of newly created process.
Therefore, the value of integer variable cpid in parent process is the process id of child process.
This block (if block) is executed only by the parent because the value of cpid is zero in child process. We use the value of variable cpid to decide which process (parent or child) should execute which part of code*/
printf("\nThis is parent with id: %d\n",getpid());
/* The UNIX system call getpid() return the process id of process being executed.*/
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]<a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Parent sorted the array in descending order\n");
}else
{
/*This block (else block) is executed only by the child process because value of cpid is zero only for child process.*/
printf("\nThis is child with id: %d \n",getpid());
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Child sorted the array in ascending order\n");
}
/*The following part of code is executed by both the processes because it is not restricted using the value of cpid.*/
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
return 0;
}
Output:
Enter number of elements : 5
Enter elements
8
6
10
2
12
This is parent with id: 4261
Parent sorted the array in descending order
12
10
8
6
2
This is child with id: 4262
Child sorted the array in ascending order
2
6
8
10
12
Program
/*The program (parent process) reads an array from the user.
It sorts it in descending order.
The child process created by parent process sorts it ascending order
*/
#include<stdlib.h>
#include<stdio.h>
int main()
{
int cpid;
int temp,a[100],n,i,j;
printf("\nEnter number of elements : ");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\n");
cpid=fork(); /*Creating child process using fork() system call */
if(cpid<0)
{
printf("Cannot create a new process\n");
exit(0);
}
if(cpid != 0)
{
/*fork returns processid of newly created process.
Therefore, the value of integer variable cpid in parent process is the process id of child process.
This block (if block) is executed only by the parent because the value of cpid is zero in child process. We use the value of variable cpid to decide which process (parent or child) should execute which part of code*/
printf("\nThis is parent with id: %d\n",getpid());
/* The UNIX system call getpid() return the process id of process being executed.*/
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]<a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Parent sorted the array in descending order\n");
}else
{
/*This block (else block) is executed only by the child process because value of cpid is zero only for child process.*/
printf("\nThis is child with id: %d \n",getpid());
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Child sorted the array in ascending order\n");
}
/*The following part of code is executed by both the processes because it is not restricted using the value of cpid.*/
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
return 0;
}
Output:
Enter number of elements : 5
Enter elements
8
6
10
2
12
This is parent with id: 4261
Parent sorted the array in descending order
12
10
8
6
2
This is child with id: 4262
Child sorted the array in ascending order
2
6
8
10
12
![]() |
| Output of the program |
Computer Programming Fifth semester previous year Question Paper for Civil
Free download Computer Programming (CP) 2014 Previous Year Question Paper as pdf for B.Tech Civil engineering.
Course : BTech Engineering course (degree)
University: MG University (Mahatma Gandhi University)
Semester: Fifth Semester (5th) (s5)
Subject: CE 010 502- Computer programming (CP) (C programming)
Department or Branch : Civil Engineering (CE)
Click to view file online
Click to download
Course : BTech Engineering course (degree)
University: MG University (Mahatma Gandhi University)
Semester: Fifth Semester (5th) (s5)
Subject: CE 010 502- Computer programming (CP) (C programming)
Department or Branch : Civil Engineering (CE)
Click to view file online
Click to download
C Program Using 8086 Interrupts to Restrict Mouse Pointer Into a Circle of Given Center and Radius
C Program Using 8086 Interrupts to Restrict Mouse Pointer Into a Circle of Given Center and Radius. The mouse ponter will be restricted to a circle of user specified center and radius using the interrupt 33 of 8086 in c complier. I have tested this program in Turbo C compiler.
![]() |
| Mouse pointer restricted into a circle in Turbo C |
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int x,y,tx,ty,r;
union REGS inreg, outreg;
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
clrscr();
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode,"C:\\TC\\BGI\\" );
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
setcolor(getmaxcolor());
printf("\nEnter the center of circle. x and y");
scanf("%d%d",&x,&y);
printf("\nEnter the radius of circle");
scanf("%d",&r);
clrscr();
circle(x,y,r);
inreg.x.ax=0x1;
int86(0x33,&inreg,&outreg);
inreg.x.ax=0x7;
inreg.x.cx=x-r;
inreg.x.dx=x+r;
int86(0x33,&inreg,&outreg);
inreg.x.ax=0x8;
inreg.x.cx=y-r;
inreg.x.dx=y+r;
int86(0x33,&inreg,&outreg);
do
{
inreg.x.ax=0x3;
int86(0x33,&inreg,&outreg);
tx=outreg.x.cx;
ty=outreg.x.dx;
if((tx-x)*(tx-x)+(ty-y)*(ty-y)>r*r)
{
if(tx<=x)
{
if(ty<y)
do{tx++; ty++;}while((tx-x)*(tx-x)+(ty-y)*(ty-y)>=r*r);
else
do{tx++; ty--;}while((tx-x)*(tx-x)+(ty-y)*(ty-y)>=r*r);
}
else
{
if(ty<=y)
do{tx--; ty++;}while((tx-x)*(tx-x)+(ty-y)*(ty-y)>=r*r);
else
do{tx--; ty--;}while((tx-x)*(tx-x)+(ty-y)*(ty-y)>=r*r);
}
inreg.x.ax=0x4;
inreg.x.cx=tx;
inreg.x.dx=ty;
int86(0x33,&inreg,&outreg);
}
}while(1);
}
Subscribe to:
Posts (Atom)

