Sunday, 29 September 2013

Here are two program that is opening the two file in the two different process in two different window. Both the process are communicating with each other. Signal concept is used, with pause() signal. Both programs are accessing the same file and interchanging the Process ID. For executing this program, in a directory save these two program with ".c" extension. Along with it make a file name "test.txt" by writing following commands.

# or $(*already present) cat > test.txt
(then press ctrl+D).

this will create empty file with name test of extension txt.(text).
********************************************************************************
/* Program 1
 process1.c */

/* Program to generate the process ID and write it on file and communicate with other process*/
/* Programmer : Raj Prajapati */

#include<stdio.h>
#include<fcntl.h>
#include<signal.h>
const int buff=100;
void abc();
int main()
{
int pid,fd,pid2,i;
char data[100];
fd=open("test.txt",O_RDWR,0777);
printf("\n Process 1 Running ");

pid=getpid();
printf("\n Process ID of process 1 : %d\n ",pid);
write(fd,&pid,4);
close(fd);
  signal(SIGUSR1,abc);
pause();
printf("\n Process 1 resume ");
fd=open("test.txt",O_RDWR);
read(fd,&pid2,4);
printf("\n Process ID of process 2 : %d\n ",pid2);
printf("\n Killing Process 2 : ");
kill(pid2,SIGUSR2);
return 0;
}
void abc()
{
sleep(6);
}
******************************************************************************
/*  process2.c  */
/* Program to generate the process ID and write it on file and communicate with other process */
/* Programmer : Raj Prajapati*/

#include<stdio.h>
#include<fcntl.h>
#include<signal.h>
const int buff=100;
void ab();
int main()
{
int pid2,pid1,fd,i;
char data2[buff];
printf("\n Process 2 Running ");
pid2=getpid();
signal(SIGUSR2,ab);
printf("\n Process ID of process 2 : %d\n ",pid2);
fd=open("test.txt",O_RDWR,0777);
read(fd,&pid1,4);
printf("\n Process ID of process 1 : %d\n ",pid1);
printf("\n KILLING process 1 %d\n ");
close(fd);
fd=open("test.txt",O_RDWR);
write(fd,&pid2,4);
kill(pid1,SIGUSR1);
pause();
return 0;
}
void ab()
{
sleep(6);
printf("\n Process 2 is resumed and now terminating \n");
}
*******************************************************************************
Open both program in terminal window separately. (open tags in same window by pressing ctrl+shift+T).
Compile both program separately, run the process1.c first and then on other terminal run processs2.c and examine the Result.

0 comments:

Post a Comment

Blogroll

Copyright © 2013 RAJ PRAJAPATI (raj@electronicsrj.com)

Comments

About Projects

We also Provide support in making Electronics Projects at different level at Cheapest price. Contact Us for the further help.