The document contains code snippets demonstrating inter-process communication (IPC) techniques in Unix/Linux like message queues, pipes, semaphores, and shared memory. It includes examples of message passing between processes using message queues and pipes. It also shows implementations of wait/signal and counting semaphores. The last part demonstrates solving the producer-consumer problem using shared memory and semaphores with a limited buffer size.
9. countsignal();
countsignal();
countwait();
countsignal();
}
}
void countwait()
{
semop(semid,&wait3,1);
semop(semid,&wait1,1);
count = count -1;
printf("n Counting Semaphore After Wait is : %d",count);
if(count<0)
{
printf("n Counting Semaphore less than zero ");
printf("n sSem1 : Signal1");
semop(semid,&signal1,1);
printf("n sSem2 : wait");
}
else
{
semop(semid,&signal1,1);
semop(semid,&signal1,1);
}
}
void countsignal()
{
semop(semid,&wait1,1);
count = count +1;
printf("n Value of Counting Semaphore After Signal is : %d",count);
if(count<=0)
{
semop(semid,&signal2,1);
printf("n Semaphore Count less than zero signaling sem1 &sem3");
}
semop(semid,&signal1,1);
semop(semid,&signal1,1);
}
10. Output :
vinhai@vinhai-desktop$ cc csem.c
vinhai@vinhai-desktop$ ./a.out
Counting Semaphore After Wait is : 2
Counting Semaphore After Wait is : 1
Counting Semaphore After Wait is : 0
Value of Counting Semaphore After Signal is : 1
Value of Counting Semaphore After Signal is : 1
Value of Counting Semaphore After Signal is : 2
Value of Counting Semaphore After Signal is : 3
Counting Semaphore After Wait is : 2
Value of Counting Semaphore After Signal is : 3
vinhai@vinhai-desktop$
11. SIGNALING PROCESS
#include<stdio.h>
#include<signal.h>
void s()
{
printf("n This is my signal");
}
main()
{
printf("n Before signal");
signal(SIGALRM,s);
alarm(2);
pause();
printf("n After Signal n ");
}
OUTPUT :
vinhai@vinhai-desktop$ cc si.c
vinhai@vinhai-desktop$ ./a.out
Before signal
This is my signal
After Signal
vinhai@vinhai-desktop$
12. DEADLOCK DETECTION
#include<stdio.h>
#include<stdlib.h>
main()
{
int i,n,j,al[10],re[10];
printf("enter the no process : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter the resourse allocated and need for process : %d ",i," ");
scanf("%d%d",&re[i],&al[i]);
}
for(i=1;i<=n;i++)
{
for(i=j;j<=n;j++)
{
if(re[i]==al[j])
{
printf("p%d is deadlock because r%d allocated for : p%d",i,re[i],j);
exit(0);
}
}
}
}
OUTPUT :
vinhai@vinhai-desktop$ cc deadlock.c
vinhai@vinhai-desktop$ ./a.out
enter the no process : 2
enter the resourse allocated and need for process : 1 1
2
enter the resourse allocated and need for process : 2 2
1
Segmentation fault
vinhai@vinhai-desktop$
13. PROCESS SCHEDULING (FCFS)
#include<stdio.h>
main()
{
int i,n,t=0,s=0,b[10],t1;
printf("n enter the no of values : ");
scanf("%d",&n);
printf("n enter the times : ");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
printf("ntprocesstttimettstartttendttwait");
for(i=0;i<n;i++)
{
s=s+b[i];
printf("nt%dtt%dtt%dtt%dtt%d",i+1,b[i],t,s,t);
t1=t;
t=s;
}
printf("n avg time%dn",s/n);
printf("b avg waiting time %d n",t1/n);
}
OUTPUT :
vinhai@vinhai-desktop$ cc fcfs.c
vinhai@vinhai-desktop$ ./a.out
enter the no of values : 2
enter the times : 1
3
process time start end wait
1 1 0 1 0
2 3 1 4 1
avg time 2
avg waiting time 0
vinhai@vinhai-desktop$
14. PROCESS SCHEDULING (LEAST FREQUENTLY USED)
#include<stdio.h>
main()
{
int i,j,n,t,a[10],s=0,e=0;
printf("n Enter the n value : ");
scanf("%d",&n);
printf("n Enter the s value : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
} }}
printf("n Process Time Start End Wait ");
for(i=0;i<n;i++)
{
s=s+a[i];
printf("n %dt%dt%dt%dt%d n ",i+1,a[i],e,s,e);
e=s;
}
}
OUTPUT :
vinhai@vinhai-desktop$ gcc i.c
vinhai@vinhai-desktop$ ./a.out
Enter the n value3
Enter the s value2
3
4
Process Time Start End Wait
1 1 0 1 0
2 2 1 3 1
3 3 3 6 3
vinhai@vinhai-desktop$
15. PROCESS SCHEDULING (ROUND ROBIN)
#include<stdio.h>
main()
{
int i,a[10],n,ts,t=0;
printf("n Enter the Process & Time-Slice : ");
scanf("%d %d",&n,&ts);
printf("n Enter the Time ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("n Process Time : ");
st:
t=0;
for(i=0;i<n;i++)
{
if(a[i]>=ts)
{
a[i]=a[i]-ts;
printf("n %d %d n",i+1,ts);
t++;
} else if(a[i]!=0) {
printf("n %d %d n ",i+1,a[i]);
a[i]=0;
}}
if(t>0)
goto st;
}
OUTPUT :
vinhai@vinhai-desktop$ cc rr.c
vinhai@vinhai-desktop$ ./a.out
Enter the Process & Time-Slice : 2
2
Enter the Time 3
1
Process Time :
1 2
2 1
1 1
vinhai@vinhai-desktop$
18. OUTPUT :
vinhai@vinhai-desktop$ cc prod.c
vinhai@vinhai-desktop$ ./a.out
Prodced Element : a
Consumed Element : a
Prodced Element : a
Consumed Element : a
Prodced Element : a
Consumed Element : a
Prodced Element : a
Consumed Element : a
Prodced Element : a
Consumed Element : a
vinhai@vinhai-desktop$
19. DINING PHILOSOPHER PROBLEM
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define N 3
#define EATING 1
#define THINKING 1
#define left(p) (p)
#define right(p) ((P)+1%N)
typedef int *Semaphore;
Semaphore spoon[N];
void philosopher(int me);
void pick_up(int me);
void pick_down(int me);
void signal(Semaphore s)
int main()
{
system("clear");
int i,n;
for(i=0;i<N;i++)
{
spoon[i]=make_sema();
signal(spoon[i]);
}
for(i=0;i<n;i++)
{
if(fork()==0)
break;
philosopher(i);
}
return 0;
}
void philosopher(int me)
{
char *s;
int i;
for(i=1;i<2;i++)
{
pick_up(me);
printf("Philosopher %d eating for th time n",me,i);
20. sleep(EATING);
pick_down(me);
printf("Philosopher %d eating for th time n",me,i);
sleep(THINKING);
}
}
void pick_up(int me)
{
if(me==0)
{
wait(spoon[right(me)]);
printf("Philosopher %d picks up right spoon n",me);
sleep(1);
wait(spoon[left(me)]);
printf("Philosopher %d picks up right spoon n",me);
}
else
{
wait(spoon[left(me)]);
printf("Philosopher %d picks up right spoon n",me);
}
}
void pick_down(int me)
{
signal(spoon[left(me)]);
signal(spoon[right(me)]);
}
void signal(Semaphore s)
{
if(write(s[1],"x",1)<=0)
{
printf("Error ! Write() failed, check semaphore creation n");
exit(1);
}
}
21. void wait(Semaphore s)
{
int junk;
if(read(s[0],&junk,1)<=0)
{
printf("Error ! Write() failed, check semaphore creation n");
exit(1);
}
}
Semaphore make_sema(void)
{
int *sema;
sema = (int *)calloc(2,siyeof(int));
pipe(sema);
return sema;
}
OUTPUT :
vinhai@vinhai-desktop$ cc phil.c
vinhai@vinhai-desktop$ ./a.out
Philosopher 0 picks up right spoon
Philosopher 0 picks up left spoon
Philosopher 0 eating for the 1 time
Philosopher 0 thnking for the 1 time
Philosopher 1 picks up right spoon
Philosopher 1 picks up left spoon
Philosopher 1 eating for the 1 time
Philosopher 1 thnking for the 1 time
Philosopher 2 picks up right spoon
Philosopher 2 picks up left spoon
Philosopher 2 eating for the 1 time
Philosopher 2 thnking for the 1 time
vinhai@vinhai-desktop$
22. Reader-Writer Problem
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/sem.h>
#include<sys/msg.h>
#define N 10
void writer();
void reader();
void quit(int,int);
int semid,msgid;
main()
{
int num,ch;
semid=semget((key_t)0x43,1,IPC_CREAT|0666);
printf("Semaphore value is %d n",semid);
msgid=msgget((key_t)43,IPC_CREAT|0666);
for(;;)
{
printf("1. WRITER n");
printf("2. READER n");
printf("3. EXit n");
printf("Enter the choice n");
scanf("%d",&ch);
switch(ch)
{
case1:
writer();
break;
case2:
reader();
break;
case3:
quit(semid,msgid);
break;
default:
printf("Wrong Choice n");
}
}
}
23. void writer()
{
char item[20];
int num,i;
struct sembuf sop;
printf("Writer is in critical section n");
printf("Enter the num of items to be writtenn");
scanf("%d",&num);
if(num<(N-semctl(semid,0,GETVAL,0)))
{
sop.sem_num-0;
sop.sem_op=num;
sop.sem_flg=0;
semop(semid,&sop,1);
for(i=0;i<num;i++)
{
printf("Print message[%d]n",i+1);
scanf("%s",item);
msgsnd(msgid,&item,strlen(item),IPC_CREAT);
}
}
else
{
printf("Cannot write %d message n",num);
}
}
void reader()
{
char item[20];
int num,i,n1;
struct sembuf sop;
printf("Writer is in critical section n");
printf("Enter the num of items to be written n");
scanf("%d",&num);
if(semctl(semid,0,GETVAL,0))>0)
{
n1=semctl(semid,0,GETVAL,0);
printf("The queue has %d items n",n1);
25. Output :
vinhai@vinhai-desktop$ gcc rw.c
vinhai@vinhai-desktop$ ./a.out
Semaphore value is 819203
1. WRITER
2. READER
3. EXIT
Enter the choice
1
Writer is in critical section
Enter the num of items to written
2
Enter Message[1] : hai
Enter Message[2] : cst
1. WRITER
2. READER
3. EXIT
Enter the choice
2
reader is in critical section
Enter the num of items to written
1
The queue has 2 items
The item recived is hai
The item recived is cst
1. WRITER
2. READER
3. EXIT
Enter the choice
3
vinhai@vinhai-desktop$
26. Two Process Mutual Exclusion
#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
#include<string.h>
#define key 200
main(0
{
int msgqid,pid;
struct
{
int mtype;
char mtext[30];
}buf;
msgqid=msgget(key,IPC_CREAT|0666);
if(msgqid==-1)
{
printf("Error in the message queue creation");
exit(0);
}
pid=fork();
if(pid==-1)
{
printf("Error in process creation");
exit(1);
}
if(pid==0)
{
strcpy(buf.mtext,"Two process mutual exclusion");
buf.mtype=2;
msgsnd(msgqid,&buf,sizeof(buf.mtext),IPC_NOWAIT);
strcpy(buf.mtext,"Unix program");
buf.mtype=1;
msgsnd(msgqid,&buf,sizeof(buf,mtext),IPC_NOWAIT);
}