Unix Programming - Multiple children read write data

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2006 > Multiple children read write data





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Multiple children read write data
rchieng@gmail.com

2006-10-18, 7:28 am

Hello i've created multiple child processes by forking them for n
times.
These process will then read a shared variable from the parent and
then decrement the variable one at a time.
The problem i've been having is when i should open the read part of the
pipe and the write part of the pipe.
Could anyone help me with my problem?
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int seats_available = 36;
FILE *fdopen();

void dojob(int pipe1[],int pipe2[]){
int pids;
int rc;
pids = getpid();
printf("pids = %d\n",pids);
do{
close(pipe1[1]);
close(pipe2[0]);
fdopen(pipe1[0],"r");

read(pipe1[0],& seats_available,sizeof(seats_available))
;

rc =close(pipe1[0]);

if(rc==-1){ printf("close error"); exit(-1);}
printf("child %d seats available = %d\n",pids,seats_available);
seats_available--;
fdopen(pipe2[1],"w");

write(pipe2[1],& seats_available,sizeof(seats_available))
;
fclose(ptr);
}while(seats_available>0);
rc=close(pipe2[1]);

if(rc==-1){ printf("close error1"); exit(-1);}

}
void sendseatdata(int pipe1[],int pipe2[]){
close(pipe1[0]);
close(pipe2[1]);
int rc;
fdopen(pipe1[1],"w");

write(pipe1[1],& seats_available,sizeof(seats_available))
;
fclose(ptr);
rc=close(pipe1[1]);

if(rc==-1){ printf("close error2"); exit(-1);}
fdopen(pipe2[0],"r");
read(pipe2[0],& seats_available,sizeof(seats_available))
;
printf("seats available = %d\n",seats_available);
fclose(ptr);
rc=close(pipe2[0]);

if(rc==-1){ printf("close error3"); exit(-1);}

}
int main(int argc, char* argv[]){
int pipe1[2];
int pipe2[2];
int pid;
int rc;
int i;
int status;
srand(time(NULL));
rc =pipe(pipe1);
if(rc==-1){
perror("main: pipe1");
exit(-1);
}
rc =pipe(pipe2);
if(rc==-1){
perror("main: pipe2");
exit(-1);
}

for(i=0;i<6;i++){
pid=fork();

if(pid==-1){
perror("main: fork");
exit(-1);
}
else if(pid==0){
printf("child time\n");
dojob(pipe1,pipe2);
break;
}
else{
sendseatdata(pipe1,pipe2);
}
}

return 0;
}

I wrote this but it doesn't work.

David Schwartz

2006-10-19, 1:26 am


rchieng@gmail.com wrote:

> close(pipe1[1]);
> close(pipe2[0]);
> fdopen(pipe1[0],"r");
>
> read(pipe1[0],& seats_available,sizeof(seats_available))
;


What is the purpose of the 'fdopen' call here?

DS

rchieng@gmail.com

2006-10-19, 1:26 am

oops that was a line i forgot to comment out or remove please ignore
that

David Schwartz wrote:
> rchieng@gmail.com wrote:
>
>
> What is the purpose of the 'fdopen' call here?
>
> DS


David Schwartz

2006-10-19, 7:23 pm


rchieng@gmail.com wrote:[vbcol=seagreen]
> oops that was a line i forgot to comment out or remove please ignore
> that
>
> David Schwartz wrote:

Can you post your code again with the obvious bugs removed? You have
several 'fdopen'/'fclose' calls that don't make sense, and you have two
'close' calls inside a 'do' loop that doesn't make sense.

DS

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com