Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Programs for Operating System
1. 1. Shortest job first (non- preemptive):
#include<stdio.h>
#include<unistd.h>
#include<string.h>
void main()
{
float avw=0,avt=0;
struct pro
{
char name[20];
int b,w,ta;
}a[10],temp;
int n,i,j;
printf("number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("nenter name of process %d:",i+1);
scanf("%s",a[i].name);
printf("n enter the brust time of process %d:",i+1);
scanf("%d",&a[i].b);
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(a[i].b>a[j].b)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("nnametB.TtW.TtTAT");
for(i=0;i<n;i++)
{
if(i==0)
{
a[i].w=0;
a[i].ta=a[i].b;
}
else
{
a[i].w=a[i-1].w+a[i-1].b;
a[i].ta=a[i].w+a[i].b;
}
avw+=a[i].w;
avt+=a[i].ta;
printf("n%st%dt%dt%d",a[i].name,a[i].b,a[i].w,a[i].ta);
}
avt/=n;
avw/=n;
printf("naverage waiting time:%fn average true around time:%f",avw,avt);}
2. Shortest job first ( preemptive)
#include<stdio.h>
struct proc
{
int pid;
int at,bt,wt,tat,rbt;
};
struct proc p1[10];
int i,j,k,n,no,m;
float atat=0.0,awt=0.0;
2. int tbt=0;
int minimum1();
int main()
{
int minv,locv,mins,locs;
printf("nenter the number of processes:");
scanf("%d",&n);
printf("nenter the proc information:");
printf("npid at bt");
for(i=0;i<n;i++)
{
p1[i].wt=0;
p1[i].tat=0;
scanf("%d%d%d",&p1[i].pid,&p1[i].at,&p1[i].bt);
tbt+=p1[i].bt;
p1[i].rbt=p1[i].bt;
}
printf("nthe proc information:");
printf("npid at bt");
for(i=0;i<n;i++)
{
printf("n%d %d %d",p1[i].pid,p1[i].at,p1[i].bt);
}
minv=p1[0].at;
locv=0;
for(i=1;i<n;i++)
{
if(p1[i].at<minv)
{
locv=i; //tells min at process in locv
minv=p1[i].at;
}
}
printf("ngantt chart:");
for(i=minv;i<tbt+minv;i++)
{
no=minimum1();
printf("%d p[%d]",i,p1[no].pid);
p1[no].rbt=p1[no].rbt-1;
for(k=0;k<n;k++)
{
if(p1[k].rbt>0&&p1[k].at<=i&&k!=no)
{
p1[k].wt++;
}
}
}
printf("%d",tbt+minv);
for(i=0;i<n;i++)
{
awt+=p1[i].wt;
}
awt=awt/n;
for(i=0;i<n;i++)
{
p1[i].tat=p1[i].wt+p1[i].bt;
atat+=p1[i].tat;
}
atat=atat/n;
printf("n average wt=%f, average tat=%f",awt,atat);
printf("nthe proc information:");
printf("npid at bt wt tat");
for(i=0;i<n;i++)
{
3. printf("n%d %d %d %d
%d",p1[i].pid,p1[i].at,p1[i].bt,p1[i].wt,p1[i].tat);
}
}
int minimum1()
{
int loc,z;
int mini;
mini=99;
loc=-1;
for(z=0;z<n;z++)
{
if(p1[z].rbt>0&&p1[z].at<=i&&p1[z].rbt<mini)
{
mini=p1[z].rbt;
loc=z;
}
}
return loc;
}
3. Priority(non-preemptive)
#include<stdio.h>
#include<unistd.h>
void main()
{
int n,i,j;
float avgw,avgt;
struct process
{
char name[20];
int b,w,ta,p;
}p[10],temp;
printf("nnumber of processes:");
scanf("%d",&n);
printf("nenter the details of the processes");
for(i=0;i<n;i++)
{
printf("nenter the name of process %d:",i+1);
scanf("%s",p[i].name);
printf("nbrust time of the process %d:",i+1);
scanf("%d",&p[i].b);
printf("npriority of the process %d:",i+1);
scanf("%d",&p[i].p);
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(p[i].p>p[j].p)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
p[0].w=0;
p[0].ta=p[0].b;
avgw=p[0].w;
avgt=p[0].ta;
printf("nnamettbrust-time waiting-time priority turnaround-time");
printf("n%stt%dtt%dtt%dtt%d",p[0].name,p[0].b,p[0].w,p[0].p,p[0].ta);
for(i=1;i<n;i++)
4. {
p[i].w=p[i-1].w+p[i-1].b;
p[i].ta=p[i].w+p[i].b;
avgw+=p[i].w;
avgt+=p[i].ta;
printf("n%stt%dtt%dtt%dtt%d",p[i].name,p[i].b,p[i].w,p[i].p,p[i].ta);
}
printf("naverage of waiting time of the processes is:%f",avgw/n);
printf("naverage turn-arount time of the processes is:%f",avgt/n);
}
4. Round-robin
#include<stdio.h>
#include<unistd.h>
void main()
{
struct process
{
char name[20];
int b,w,ta,r,la;
}p[10];
int i,n,q,tbt=0,ct=0;
float avgt=0,avgw=0;
printf("nnumber of processes:");
scanf("%d",&n);
printf("nenter the details of the processes");
for(i=0;i<n;i++)
{
printf("nenter the name of process %d:",i+1);
scanf("%s",p[i].name);
printf("nbrust time of the process %d:",i+1);
scanf("%d",&p[i].b);
tbt+=p[i].b;
p[i].w=0;
p[i].r=p[i].b;
p[i].la=0;
}
printf("enter the value of the quanta:");
scanf("%d",&q);
while(tbt!=ct)
{
for(i=0;i<n;i++)
{
if(p[i].r<q&&p[i].r!=0)
{
printf("%s---->%dn",p[i].name,p[i].r);
p[i].w+=ct-p[i].la;
ct+=p[i].r;
p[i].la=ct;
p[i].r=0;
}
else if(p[i].r!=0)
{
printf("%s---->%dn",p[i].name,q);
p[i].w+=ct-p[i].la;
ct+=q;
p[i].la=ct;
p[i].r-=q;
}
}
}
printf("nnamettbrust-time waiting-time turnaround-time");
for(i=0;i<n;i++)
{
5. p[i].ta=p[i].w+p[i].b;
avgw+=p[i].w;
avgt+=p[i].ta;
printf("n%stt%dtt%dtt%d",p[i].name,p[i].b,p[i].w,p[i].ta);
}
printf("naverage of waiting time of the processes is:%fnaverage turn-arount
time of the processes is:%f",avgw/n,avgt/n);
}
Program:Implenment Deadlock Detection algorithm(Simulation) in C language
*/
#include
#include //We are using rand() function:)
int main()
{
int alloc[10][10],req[10][10],ins[10],avail[10],tp,tr,i,j;
int tmp[10]={0},count=0;
bool finish[10]={false},flag;
printf("Enter total no of processes:");
scanf("%d",&tp);
printf("Enter total no of resources:");
scanf("%d",&tr);
printf("Randomly Generated Allocation Matrix:n"); //itz tp x tr order
matrix
for(i=0;i
{
for(j=0;j
{
alloc[i][j]=rand()%5;
printf("t%d",alloc[i][j]);
}
printf("n");
}
printf("Randomly Generated Request Matrix:n");
for(i=0;i
{
for(j=0;j
{
req[i][j]=rand()%5;
printf("t%d",req[i][j]);
}
printf("n");
}
printf("Randomly Generated Resource Instance Vetctor:n");
for(i=0;i
{
ins[i]=rand()%10+tr+tp; //Just to increase resource instances
printf("t%d",ins[i]);
}
//To calculate resource availability vector
for(i=0;i
{
for(j=0;j
{
tmp[i]+=alloc[j][i]; //sum calculated columnwise for allocation
matrix
}
}
printf("nCalculated Availability Vector:n");
for(i=0;i
6. {
avail[i]=ins[i]-tmp[i];
printf("t%d",avail[i]);
}
//main logic starts:P
while(count
{ //if finish array has all true's(all
processes to running state)
//deadlock not detected and loop stops!
for(i=0;i
{
count=0;
//To check whether resources can be allocated any to blocked process
if(finish[i]==false)
{
for(j=0;j
{
if(req[i][j]<=avail[j])
{
count++;
}
}
flag=false;
if(count==tr)
{
for(j=0;j
{
avail[j]+=alloc[i][j]; //allocated reources are
released and added to available!
}
finish[i]=true;
printf("nProcess %d is transferred to running state and assumed
finished",i+1);
}
else
flag=true;
}
}
count=0;
for(j=0;j
{
if(finish[j]==true)
{
count++;
}
}
}
for(i=0;i
{
if(finish[i]==false)
{
printf("n Oops! Deadlock detected and causing process
is:process(%d)n",i+1);
break;
}
}
i=i-1;
if(finish[i]==true)
printf("nHurray! Deadlock not detected:-)n");
return 0;
}
/*
Sample Output:
Enter total no of processes:7
7. Enter total no of resources:3
Randomly Generated Allocation Matrix:
3 1 2
0 3 0
1 2 4
1 2 2
0 4 3
1 0 1
2 1 1
Randomly Generated Request Matrix:
3 2 4
2 0 2
3 2 0
4 2 2
3 4 2
3 1 1
2 4 3
Randomly Generated Resource Instance Vetctor:
11 19 14
Calculated Availability Vector:
3 6 1
Process 3 is transferred to running state and assumed finished
Process 4 is transferred to running state and assumed finished
Process 5 is transferred to running state and assumed finished
Process 6 is transferred to running state and assumed finished
Process 7 is transferred to running state and assumed finished
Process 1 is transferred to running state and assumed finished
Process 2 is transferred to running state and assumed finished
Hurray! Deadlock not detected:-)*/
FCFS:
#include<stdio.h>
#include<stdlib.h>
main()
{
int n,i,j,sum=0;
int arrv[10],burst[10],start[10];
int finish[10],wait[10],turn[10];
float avgturn=0.0,avgwait=0.0;
start[0]=0;
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Arrival and CPU Burst time of %d process:",i+1);
scanf("%d%d",&arrv[i],&burst[i]);
}
for(i=0;i<n;i++)
{
sum=0;
for(j=0;j<i;j++)
sum=sum+burst[j];
start[i]=sum;
}
for(i=0;i<n;i++)
{
finish[i]=burst[i]+start[i];
wait[i]=start[i];
turn[i]=burst[i]+wait[i];
}
for(i=0;i<n;i++)
11. {
printf("%d",buffer[i]);
}
getch();
}
To implement the c program for shortest job first scheduling algorithm
ALGORITHM
1. Start the process
2. Declare the array size
3. Get the number of elements to be inserted
4. Select the process which have shortest burst will execute first
5. If two process have same burst length then FCFS scheduling algorithm used
6. Make the average waiting the length of next process
7. Start with the first process from it’s selection as above and let other
process to be in
queue
6. Calculate the total number of burst time
7. Display the values
8. Stop the process
PROGRAM:
#include<stdio.h>
int main()
{
int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i;
float att=0,awt=0;
for(i=0;i<10;i++)
{
b[i]=0;w[i]=0;
}
printf("enter the number of process");
scanf("%d",&n);
printf("enter the burst times");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
p[i]=i;
}
for(i=0;i<n;i++)
13. scanf("%d",&n);
pthread_t tid[n];
for(i=0;i<n;i++)
{
res=pthread_create(&tid[i],NULL,multithread,(void *)i);
if(res!=0)
{
perror("Thread creation failedn");
exit(EXIT_FAILURE);
}
sleep(1);
}
printf("nWaiting for thread to finish.......n");
for(i=n-1;i>=0;i--)
{
res=pthread_join(tid[i],&result);
if(res==0)
{
printf("Picked up a thread %dn",(int *)result);
}
else
{
perror("Thread join failed");
}
}
printf("All donen");
exit(EXIT_SUCCESS);
}
void *multithread(void *arg)
{
int *j=(int *)arg;
//int l;
printf("nThread funtion is running argument was %dn",(int *)j);
//l=1+(int)(9.0*rand()/(RAND_MAX+1.0));
//sleep(l);
printf("Bye from %dn",(int *)j);
pthread_exit(j);
}
Thread2:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr );
main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*)
message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*)
message2);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
14. pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %dn",iret1);
printf("Thread 2 returns: %dn",iret2);
exit(0);
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s n", message);
}
C Program to demonstrate dynamic memory allocation using malloc()
#include<stdio.h>
#include<stdlib.h>
int main() {
int* grades;
int sum = 0, i, numberOfStudents;
float average;
printf("Enter the number of students: ");
scanf("%d", &numberOfStudents);
getchar();
if((grades = (int*) malloc(numberOfStudents * sizeof(int))) == NULL) {
printf("nError: Not enough memory to allocate grades arrayn");
exit(1);
}
printf("nEnter the grades of %d students (in separate lines):n",
numberOfStudents);
for(i = 0; i < numberOfStudents; i++) {
scanf("%d", &grades[i]);
getchar();
}
/* calculate sum */
for(i = 0; i < numberOfStudents; i++)
sum += grades[i];
/* calculate the average */
average = (float) sum / numberOfStudents;
printf("nThe average of the grades of all students is %.2f",
average);
getchar();
return(0);
}
To write a C program to implement Semaphore.
15. Algorithm | Source Programming
1. Start the program.
2. Get the no of jobs from the user.
3. When job1 is processing, job 2 is also starts processing.
4. When job 1 enters critical section, next job starts processing.
5. When job1 comes out of the critical section, the other job enters the
critical section.
6. The above 3 steps are performed for various programs.
7. End the program.
Example Source code programming in C Program
#include<stdio.h>
main()
{
int i,a=1,h=2,n;
printf("n Enter the no of jobs");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(a==1)
{
printf("processing %d......! n", i+1);
a++;
}
if(h>1)
{
if(i+2<=n)
{
printf("n processing %d.....! n",i+2);
}
printf("n Process %d Enters Critical section", i+1);
printf("n Process %d Leaves Critical section", i+1);
}
h+1;
}
}
Example Output Result
"semaphore.c" 25L, 359C written
[staff@linux-router staff]$ cc semaphore.c
[staff@linux-router staff]$ gcc semaphore.c
[staff@linux-router staff]$ ./a.out
Enter the no of jobs
2
processing 1......!
processing 2.....!
Process 1 Enters Critical section
Process 1 Leaves Critical section
Process 2 Enters Critical section
Process 2 Leaves Critical section
// fcfs simulation
#include<conio.h>
void main()
{
int n,b[100],w[100],t[100],i;
float aw=0,at=0;
printf("enter no: of processesn");
scanf("%d",&n);
printf("enter processes burst timen");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
20. {
printf("n%d %d %d",p1[i].pid,p1[i].at,p1[i].bt);
}
minv=p1[0].at;
locv=0;
for(i=1;i<n;i++)
{
if(p1[i].at<minv)
{
locv=i; //tells min at process in locv
minv=p1[i].at;
}
}
printf("ngantt chart:");
for(i=minv;i<tbt+minv;i++)
{
no=minimum1();
printf("%d p[%d]",i,p1[no].pid);
p1[no].rbt=p1[no].rbt-1;
for(k=0;k<n;k++)
{
if(p1[k].rbt>0&&p1[k].at<=i&&k!=no)
{
p1[k].wt++;
}
}
}
printf("%d",tbt+minv);
for(i=0;i<n;i++)
{
awt+=p1[i].wt;
}
awt=awt/n;
for(i=0;i<n;i++)
{
p1[i].tat=p1[i].wt+p1[i].bt;
atat+=p1[i].tat;
}
atat=atat/n;
printf("n average wt=%f, average tat=%f",awt,atat);
printf("nthe proc information:");
printf("npid at bt wt tat");
for(i=0;i<n;i++)
{
printf("n%d %d %d %d
%d",p1[i].pid,p1[i].at,p1[i].bt,p1[i].wt,p1[i].tat);
}
}
int minimum1()
{
int loc,z;
int mini;
mini=99;
loc=-1;
for(z=0;z<n;z++)
{
if(p1[z].rbt>0&&p1[z].at<=i&&p1[z].rbt<mini)
{
mini=p1[z].rbt;
loc=z;
}
}
return loc;
21. }
3. Priority(non-preemptive)
#include<stdio.h>
#include<unistd.h>
void main()
{
int n,i,j;
float avgw,avgt;
struct process
{
char name[20];
int b,w,ta,p;
}p[10],temp;
printf("nnumber of processes:");
scanf("%d",&n);
printf("nenter the details of the processes");
for(i=0;i<n;i++)
{
printf("nenter the name of process %d:",i+1);
scanf("%s",p[i].name);
printf("nbrust time of the process %d:",i+1);
scanf("%d",&p[i].b);
printf("npriority of the process %d:",i+1);
scanf("%d",&p[i].p);
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(p[i].p>p[j].p)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
p[0].w=0;
p[0].ta=p[0].b;
avgw=p[0].w;
avgt=p[0].ta;
printf("nnamettbrust-time waiting-time priority turnaround-time");
printf("n%stt%dtt%dtt%dtt%d",p[0].name,p[0].b,p[0].w,p[0].p,p[0].ta);
for(i=1;i<n;i++)
{
p[i].w=p[i-1].w+p[i-1].b;
p[i].ta=p[i].w+p[i].b;
avgw+=p[i].w;
avgt+=p[i].ta;
printf("n%stt%dtt%dtt%dtt%d",p[i].name,p[i].b,p[i].w,p[i].p,p[i].ta);
}
printf("naverage of waiting time of the processes is:%f",avgw/n);
printf("naverage turn-arount time of the processes is:%f",avgt/n);
}
4. Round-robin
#include<stdio.h>
#include<unistd.h>
void main()
{
struct process
{
char name[20];
int b,w,ta,r,la;
22. }p[10];
int i,n,q,tbt=0,ct=0;
float avgt=0,avgw=0;
printf("nnumber of processes:");
scanf("%d",&n);
printf("nenter the details of the processes");
for(i=0;i<n;i++)
{
printf("nenter the name of process %d:",i+1);
scanf("%s",p[i].name);
printf("nbrust time of the process %d:",i+1);
scanf("%d",&p[i].b);
tbt+=p[i].b;
p[i].w=0;
p[i].r=p[i].b;
p[i].la=0;
}
printf("enter the value of the quanta:");
scanf("%d",&q);
while(tbt!=ct)
{
for(i=0;i<n;i++)
{
if(p[i].r<q&&p[i].r!=0)
{
printf("%s---->%dn",p[i].name,p[i].r);
p[i].w+=ct-p[i].la;
ct+=p[i].r;
p[i].la=ct;
p[i].r=0;
}
else if(p[i].r!=0)
{
printf("%s---->%dn",p[i].name,q);
p[i].w+=ct-p[i].la;
ct+=q;
p[i].la=ct;
p[i].r-=q;
}
}
}
printf("nnamettbrust-time waiting-time turnaround-time");
for(i=0;i<n;i++)
{
p[i].ta=p[i].w+p[i].b;
avgw+=p[i].w;
avgt+=p[i].ta;
printf("n%stt%dtt%dtt%d",p[i].name,p[i].b,p[i].w,p[i].ta);
}
printf("naverage of waiting time of the processes is:%fnaverage turn-arount
time of the processes is:%f",avgw/n,avgt/n);
}
BANKERS ALGORITHM
/*<--------------PREPROCESSING STATEMENTS--------------->*/
#include <stdio.h>
#include <stdlib.h>
/*<--------------MAIN FUNCTION--------------->*/
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10],
safeSequence[10];
int p, r, i, j, process, count;
23. count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("nnEnter the no of resources : ");
scanf("%d", &r);
printf("nnEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("nnEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("nnEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf("n Max matrix:tAllocation matrix:n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("tt");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
24. avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}while(count != p && process != -1);
if(count == p)
{
printf("nThe system is in a safe state!!n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">n");
}
else
printf("nThe system is in an unsafe state!!");
}
//Program for full duplex communication
#include <iostream>
using namespace std;
#include<stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int status, pid, pipefds[2],pipefds2[2],status2;
char instring[20],instring2[20];
/* Create the pipe and return 2 file descriptors in the pipefds array */
/* This operation is done before the fork so that both processes will */
/* know about the same pipe, which will allow them to communicate. */
status = pipe(pipefds);
status2=pipe(pipefds2);
if (status == -1)
{
perror("Trouble");
exit(1);
}
if (status2 == -1)
{
perror("Trouble");
exit(1);
}
/* create child process; both processes continue from here */
pid = fork();
if (pid == -1)
{
perror("Trouble");
exit(2);
}
else if (pid == 0) /* child : sends message to parent*/
{
/* close unused end of pipe */
/* because this process only needs to write */
close(pipefds[0]);
close(pipefds2[1]);
/* send 7 characters in the string, including end-of-string */
cout << "About to send a message: " << endl;
25. write(pipefds[1], "Hi Mom!", 7);
read(pipefds2[0], instring2, 7);
cout << "Just received a message that says: " << instring2 << endl;
close(pipefds[1]);
close(pipefds2[0]);
exit(0);
}
else /* parent : receives message from child */
{
/* close unused end of pipe */
/* because this process only needs to read */
close(pipefds[1]);
/* read from the pipe */
write(pipefds2[1], "Hi pop!", 7);
read(pipefds[0], instring, 7);
cout << "Just received a message that says: " << instring << endl;
close(pipefds[0]);
close(pipefds2[1]);
exit(0);
}
return 0;
}
//Program for half-duplex communication
#include <iostream>
using namespace std;
#include<stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int status, pid, pipefds[2],ch,i;
char instring[20];
for(i=0;i<20;i++)
instring[i]='0';
cout<<"-------------Program for half duplex communication-------------n";
cout<<"-------------Press 1 for child to parent communicationn";
cout<<"-------------Press 2 for parent to child communicationn";
scanf("%d",&ch);
/* Create the pipe and return 2 file descriptors in the pipefds array */
/* This operation is done before the fork so that both processes will */
/* know about the same pipe, which will allow them to communicate. */
status = pipe(pipefds);
if (status == -1)
{
perror("Trouble");
exit(1);
}
/* create child process; both processes continue from here */
pid = fork();
if (pid == -1)
{
perror("Trouble");
exit(2);
}
26. switch(ch)
{
case 1:
// child to parent communication
if (pid == 0) /* child : sends message to parent*/
{
/* close unused end of pipe */
/* because this process only needs to write */
close(pipefds[0]);
/* send 7 characters in the string, including end-of-string */
cout << "About to send a message: " << endl;
write(pipefds[1], "Hi father!", 10);
close(pipefds[1]);
exit(0);
}
else /* parent : receives message from child */
{
/* close unused end of pipe */
/* because this process only needs to read */
close(pipefds[1]);
/* read from the pipe */
read(pipefds[0], instring, 10);
cout << "Just received a message that says: " << instring << endl;
close(pipefds[0]);
exit(0);
}
break;
case 2:
// parent to child communication
if (pid > 0)
{
/* close unused end of pipe */
/* because this process only needs to write */
close(pipefds[0]);
/* send 7 characters in the string, including end-of-string */
cout << "About to send a message: " << endl;
write(pipefds[1], "Hi son!", 7);
close(pipefds[1]);
exit(0);
}
else
{
/* close unused end of pipe */
/* because this process only needs to read */
close(pipefds[1]);
/* read from the pipe */
read(pipefds[0], instring, 7);
cout << "Just received a message that says: " << instring << endl;
close(pipefds[0]);
exit(0);
}
break;
default:
cout<<"This is a problem";
}