Concurrent processes and I/O multiplexing



Hey everybody,
and first of all excuse me for my english, very very poor and simple.
At the bottom of the post you will find a simple source of C file.
I hope it will be auto-explaining.

In short: I want that
1) processo_2 take input from processo_1 AND stdinput
2) when processo_1 finishes its data transmission to processo_2,
processo_2 stay again tuned on input from stdinput.

This program is a semplification of a comunication between processo_1
and processo_2 and an interaction via console between user and
processo_2. User would like to ask to processo_2 some information
about what's going on, some report of some kind about elaborated data,
number of data and so on, even when processo_2 end to
catch data from processo_1.

The question is: why, after that processo_1 stop transmission of data
to processo_2, processo_2 don't stay blocked on select waiting for the
unique and possible input that can come only from standard input?

processo_2 cross the select, but WHY?

Thanks in advance, and excuse my english
Openyourmind

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <stdio.h>
#include <stdlib.h>

void handle_signal(int );
void sem_init (int* );
void sem_signal (int* );
void sem_wait (int* );
int random(float );
int random2(int, float );

int pipe2[2];
int sem1[2];
int sem2[2];

int main (int argc, char *argv[]) {

int pid, i, status;
pipe(pipe2);

sem_init(sem1);
sem_init(sem2);

if(fork() == 0) {
processo_1();
exit(EXIT_SUCCESS);
}
if(fork() == 0) {
processo_2();
exit(EXIT_SUCCESS);
}

for(i=0; i<2; i++)
pid = wait(&status);i, status;

exit(EXIT_SUCCESS);

}

int processo_1(void) {

int i=1,val=0;

close(pipe2[0]);

while(1) {
if ( i<4 ){
val = random2(3, 5);
sleep(val);
sem_wait(sem2);
//wait for "OK FOR NEW DATA" from processo_2
write(pipe2[1], /*&buf*/&val, sizeof(int));
i++;
}
}//never die
}

//-----------------------------------------
int processo_2 (void) {

int elab_time_2 = 0, buf = 0, num_dati_ric = 0;
int dato_ricev;
fd_set rfds;

FD_ZERO(&rfds);
FD_SET (pipe2[0], &rfds);
FD_SET (STDIN_FILENO, &rfds);

sem_signal(sem2);//OK: PROCESSO_1,GIVE ME NEW DATA

while(1) {

signal(SIGALRM, handle_signal);

select (pipe2[0]+1, &rfds, NULL, NULL, NULL);

printf("\n\tSELECT DONE\n");

if (FD_ISSET(pipe2[0], &rfds)) {
//NEW DATA FROM PROCESSO_1 HAS ARRIVED
read(pipe2[0], &dato_ricev, sizeof(int));

elab_time_2 = random2(1, 4);

num_dati_ric++;
printf("Received from processo_1 %d° data: -
%d-
\n",num_dati_ric,dato_ricev);
alarm(elab_time_2);
// SOUNDS LIKE
// sleep(elab_time);
// sem_signal(sem2);
// BUT IT DOES NOT BLOCK PROCESSO_2
//SO IT CAN TAKE INPUT FROM STDIN

FD_SET (STDIN_FILENO, &rfds);
}

else{
read(STDIN_FILENO, &buf, 2);

if (buf != 2680){ // 'x' + return
printf ("---Received from
stdin %d \n",buf);
////////return(0);
}
else{
printf("---Received from stdin
\"x+RETURN\"\n");
}
FD_SET(pipe2[0], &rfds);
}
}//never die
}

//-------Definizione delle funzioni--------

void sem_signal (int* s){
write (s[1], "X", 1);
}

//-----------------------------------------
void sem_wait (int* s){
char b; read (s[0], &b, 1);
}

//-----------------------------------------
void sem_init (int *s){
pipe (s);
}

//-----------------------------------------
void handle_signal(int signum){
sem_signal(sem2);
printf("signal done\n");
}

//-----------------------------------------
int random(float max){
struct timeb tb;

ftime (&tb);
srand (tb.millitm);
return (int) (max*rand()/(RAND_MAX+1.0));
}

//-----------------------------------------
int random2(int min, float max){
int x;
struct timeb tb;
max = max+1;
ftime (&tb);
srand (tb.millitm);

x = max*rand()/(RAND_MAX+1.0);
while (x < min)
x = max*rand()/(RAND_MAX+1.0);
return (x);
}

//-----------------------------------------
.



Relevant Pages

  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • [PATCH,RFC 2.6.14 09/15] KGDB: SuperH-specific changes
    ... This adds basic support for KGDB on SuperH as well as adding some architecture ... -static int kgdb_uart_getchar ... -static void kgdb_uart_putchar ... * The command-line option can include a serial port specification ...
    (Linux-Kernel)
  • problem in java swings
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • ToolTips in a View and TTN_NEEDTEXT
    ... extern int g_minWorkPeriod; ... void CDayView::CreateAllFonts ... void CDayView::DrawDayLog(CDC* pDC) ... BOOL CDayView::TimeToY ...
    (microsoft.public.vc.mfc)
  • [PATCH 6/20] FRV: Fujitsu FR-V CPU arch implementation part 4
    ... +static void register_irq_proc (unsigned int irq); ...
    (Linux-Kernel)