Results 1 to 2 of 2
The father creates three procedures P1,P2,P3. P1 writes the char a and P2 char b in the same pipe.It is required the use of sematophores for synchronizing P1,P2 and they ...
- 12-05-2007 #1Just Joined!
- Join Date
- Dec 2007
- Posts
- 1
problem:father not print what clildren write
The father creates three procedures P1,P2,P3. P1 writes the char a and P2 char b in the same pipe.It is required the use of sematophores for synchronizing P1,P2 and they write ababababab....
The father writes the data of P1,P2(non blocking) in a pipe where P3 uses the pipe that uses the father.P3 prints the data. The father finishes when he has readed all the data.
Note:For the print we must use putchar.
My problem is that while i have used sematophores and the children print ababab... (i check it with putchar(buffer) in the children) the father would must print the data like the children have written them.(i check it with putchar(buffer) in the father).Can anybody help me.Thanks.
The file for sematophores
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
static char dummy;
typedef struct {
int READ;
int WRITE;
} SEMAPHOR;
SEMAPHOR sopen (int count)
{
int pd[2];
SEMAPHOR s;
if (pipe(pd)<0) {
printf("pipe error");
exit (1);}
s.READ=pd[0];
s.WRITE=pd[1];
if (count>0)
write(s.WRITE,&dummy,count*sizeof(dummy));
return (s);
}
WAIT (SEMAPHOR s)
{
read(s.READ, &dummy, sizeof(dummy));
}
SIGNAL (SEMAPHOR s)
{
write(s.WRITE, &dummy, sizeof(dummy));
}
sclose (SEMAPHOR s)
{
close (s.WRITE);
close (s.READ);
}
The main program
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
#include<fcntl.h>
#include "semaphores.c"
void paidi(int i);
void pateras();
void P3();
int pd1[2];
int pd2[2];
char buffer;
SEMAPHOR x,y;
int main(void)
{
int i,dummy;
x=sopen (1);
y=sopen (0);
pipe(pd1);
pipe(pd2);
for(i=0;i<3;i++)
{
if (fork()==0)
{
paidi(i);
exit(0);
}
}
wait(&dummy);
wait(&dummy);
wait(&dummy);
sclose(x);
sclose(y);
pateras();
return 0;
}
void paidi(int i)
{
int j;
if (i==0) {
fcntl(pd1[0],F_SETFL,O_NONBLOCK);
close(pd1[0]);
buffer='a';
for(j=0;j<3;j++){
write(pd1[1],&buffer,1);
WAIT(x);
putchar(buffer);
fflush(stdout);
usleep(20000);
SIGNAL (y);}}
else if(i==1){
fcntl(pd1[0],F_SETFL,O_NONBLOCK);
close(pd1[0]);
buffer='b';
for(j=0;j<3;j++){
WAIT(y);
write(pd1[1],&buffer,1);
putchar(buffer);
fflush(stdout);
usleep(30000);
SIGNAL (x);}}
else if(i==3) P3();
return;
}
void pateras()
{
int count=0;
close(pd1[1]);
putchar('\n');
fcntl(pd2[0],F_SETFL,O_NONBLOCK);
while(count<6){
read(pd1[0],&buffer,1);
write(pd2[1],&buffer,1);
putchar(buffer);
fflush(stdout);
P3();
count++;}
return;
}
void P3(){
close(pd2[1]);
read(pd2[0],&buffer,1);
//putchar(buffer);
//fflush(stdout);
exit;
}
- 12-06-2007 #2
If the program's behavior for your homework assignment is incomprehensible, you should break the problem down into smaller parts. First write a small program which demonstrates the correct use of semaphores. Then write a small program which demonstrates the correct use of a pipe. Then write a small program which does a piece of what you want. Perhaps no P2 or P3, for example. When that works, add more to your program until you have what you want.
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote