0% found this document useful (0 votes)
47 views17 pages

Aim: Write A Program Using Fork and Vfork Program (A)

The document discusses implementations of various inter-process communication mechanisms in C programming including fork(), vfork(), signals, pipes, FIFOs, message queues, and semaphores. For each mechanism, it provides sample code to demonstrate its usage, and sample output when the code is executed. The code examples show how to use the mechanisms for communication between related processes like a parent and child process.

Uploaded by

Musthafa Mufa
Copyright
© © All Rights Reserved
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)
47 views17 pages

Aim: Write A Program Using Fork and Vfork Program (A)

The document discusses implementations of various inter-process communication mechanisms in C programming including fork(), vfork(), signals, pipes, FIFOs, message queues, and semaphores. For each mechanism, it provides sample code to demonstrate its usage, and sample output when the code is executed. The code examples show how to use the mechanisms for communication between related processes like a parent and child process.

Uploaded by

Musthafa Mufa
Copyright
© © All Rights Reserved
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/ 17

FORK AND VFORK FUNCTION

AIM:
Write a program using fork() and vfork()
PROGRAM(A):
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
int a=10,b=20;
int main()
{
int pid;
printf("\n fork");
pid=fork();
if(pid==0)
{
a++;
b++;
printf("\n child process:");
printf("%d %d",a,b);
}
else
{
a--;
b--;
printf("\n parent process:");
printf("%d %d",a,b);
}
return 0;
}

PROGRAM(B):
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
int a=10,b=20;
int main()
{
int pid;
printf("\n vfork");
pid=vfork();
if(pid==0)
{
a++;
b++;
printf("\n child process:");
printf("%d %d",a,b);
}
else
{
a--;
b--;
printf("\n parent process:");
printf("%d %d",a,b);
}
return 0;
}

RESULT:
Program is executed and output is verified

OUTPUT(A):

$ cc fork.c
$ ./a.out
fork
parent process:9 19 fork
child process:11 21

OUTPUT(B):
$ cc fork.c
$ ./a.out
vfork
child process:11 21
parent process:10 20
vfork
child process:11 21
parent process:10 20

IMPLEMENTATION OF SIGNAL

AIM:
Write a program using signal
PROGRAM:
#include<unistd.h>
#include<stdio.h>
#include<signal.h>
#include<string.h>
void add();
void mul();
void stop();
int a,b;
int main()
{
printf("\n enter the 2 values");
scanf("%d %d",&a,&b);
if(signal(SIGINT,add)==SIG_ERR)
printf("\n cannot catch the signal");
if(signal(SIGQUIT,mul)==SIG_ERR)
printf("\n cannot catch the signal");
if(signal(SIGSTOP,stop)==SIG_DFL)
printf("\n stop signal error");
for(;;);
}
void add()
{
printf("\n SIGINT caught");
printf("\n sum=%d\n",a+b);
}
void mul()
{
printf("\n SIGQUIT caught");
printf("\n mul=%d\n",a*b);
}
void stop(int sig)
{
printf("\n stop the signal");
}

RESULT:
Program is executed and output is verified

OUTPUT:
$ cc signal.c
$ ./a.out
enter the 2 values 10 20
^C
SIGINT caught
sum=30
^\
SIGQUIT caught
mul=200
^Z
[2]+ Stopped
./a.out

ALARM FUNCTION
AIM:
Write a program using alarm()
PROGRAM:
#include<unistd.h>
#include<stdio.h>
#include<signal.h>
#include<string.h>
void sig_alrm();
int main()
{
int sec;
int sleep1(int);
printf("enter no of second");
scanf("%d",&sec);
sleep1(sec);
return(0);
}
int sleep1(int sec)
{
if(signal(SIGALRM,sig_alrm)==SIG_ERR)
{
return(sec);
}
alarm(sec);
pause();
return(0);
}
void sig_alrm()
{
printf("alarm set");
}

RESULT:
Program is executed and output is verified

OUTPUT:
$ cc alarm.c
$ ./a.out
enter no of second 5
alarm set

IMPLEMENTATION OF PIPE

AIM:
Write a program using pipe
PROGRAM:
#include<unistd.h>
#include<stdio.h>
int main()
{
int n,fd[2],pid;
char b='a',d;
pipe(fd);
pid=fork();
if(pid==0)
{
close(fd[0]);
write(fd[1],&b,1);
printf("\n child writes %c",b);
}
else
{
close(fd[1]);
read(fd[0],&d,1);
printf("\n parent reads %c",d);
}
return(0);
}

RESULT:
Program is executed and output is verified

OUTPUT:
$ cc pipe.c
$ ./a.out
parent reads a
child writes a

IMPLEMENTATION OF FIFO
AIM:
write a program using FIFO
PROGRAM:
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
char b='a',d;
int wfd,rfd;
mkfifo("sample.txt",O_CREAT,0777);
if(fork()==0)
{
wfd=open("sample.txt",O_WRONLY);
if(write(wfd,&b,1)<0)
{
printf("\n write error");
}
else
{
printf("child writes");
}
}
else
{
rfd=open("sample.txt",O_RDONLY);
if(read(rfd,&d,1)<0)
{
printf("\n read error");
else
{
printf("parent reads:%c\n",d);
}
}
return(0);
}

RESULT:
Program is executed and output is verified

OUTPUT:
$ cc fifo.c
$ ./a.out
parent reads:a
child writes

MESSAGE QUEUE IMPLEMENTATION


AIM:
Write a program using message queue
PROGRAM:
#include<sys/msg.h>
#include<sys/ipc.h>
#include<malloc.h>
#include<string.h>
#include<stdio.h>
int main()
{
struct msg
{
long mtype;
char mtext[20];
}*mbuf;
key_t msgid;
char temp[20];
int no,ch,t;
msgid=msgget((key_t)051,IPC_CREAT|0776);
mbuf=malloc(sizeof(struct msg));
do
{
printf("\n 1.sendmsg\n 2.receiving\n 3.exit");
printf("enter the choice");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1:
printf("enter the msg");
scanf("%s",temp);
strcpy(mbuf->mtext,temp);
fflush(stdin);
printf("enter the type of msg");
scanf("%ld",&mbuf->mtype);
t=msgsnd(msgid,mbuf,10,IPC_NOWAIT);
if(t<0)
printf("\n error");
break;
case 2:
printf("enter the msgno");
scanf("%d",&no);

t=msgrcv(msgid,mbuf,20,0,IPC_NOWAIT);
if(t<0)
printf("error in receiving");
else
printf("msg received:%s\n",mbuf->mtext);
break;
case 3:
return 0;
}
}while(ch!=3);
return 0;
}

RESULT:
Program is executed and output is verified

OUTPUT:
$ cc mqueue.c
$ ./a.out
1.sendmsg
2.receiving
3.exit
enter the choice 1
enter the msg welcome
enter the type of msg 3
1.sendmsg
2.receiving
3.exit
enter the choice 2
enter the msgno 3
msg received:welcome
1.sendmsg
2.receiving
3.exit
enter the choice 3

IMPLEMENTATION OF SEMAPHORE
AIM:
Write a program using semaphore
PROGRAM:
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdio.h>
void prod(int);
void cons(int);
int main()
{
int pid,semid;
semid=semget((key_t)6,2,IPC_CREAT|0766);
pid=fork();
if(pid==0)
{
while(1)prod(semid);
}
else
{
while(1)cons(semid);
}
return(0);
}
void prod(int semid)
{
int val,pcount=0;
printf("consumer is waiting");
while(pcount<2)
{
val=random()%100;
semctl(semid,pcount,SETVAL,val);
printf("producer produces:%d\n",val);
pcount++;
}
sleep(5);
}
void cons(int semid)
{
int val,ccount=0;

printf("producer is waiting\n");
while(ccount<2)
{
val=semctl(semid,ccount,GETVAL);
printf("consumer consumes:%d\n",val);
ccount++;
}
sleep(5);
}

RESULT:
Program is executed and output is verified

OUTPUT:
$ cc sem.c
$ ./a.out
producer is waiting
consumer is waiting
producer produces:83
producer produces:86
consumer consumes:83
consumer consumes:86

You might also like