c - two blocking operations in one process -
i have 2 simple jobs. 1st reading pipe. , 2nd operations timeout. problem make work in 1 process (i khow way in 2 process unsuitable me..).
and there reasons dont use cron. 2 jobs should runned run asyncronically (non blocking each other).
any ideas?
#include<stdio.h> #include<stdlib.h> void someanotherjob(); main(){ printf ("hello!\n"); int c; file *file, *file2; file = fopen("/dev/ttyusb0", "r"); file2 = fopen("out.txt", "a"); if (file) { while ((c = getc(file)) != eof){ fputc(c, file2); fflush(file2); } fclose(file); } while (1) { someanotherjob(); sleep(10); } } void someanotherjob() { printf("yii\n"); }
you can use select nonblocking i/o many descriptors:
fd_set rfds; fd_zero(&rfds); file* files[2]; if( !( files[0] = fopen( "/dev/ttyusb0", "r")) // error if( !( files[1] = fopen( "out.txt", "a")) // error // each file opened fd_set( fileno( files[i]), &rfds); int returned = select( highfd + 1, &rfds, null, null, null); if ( returned) { // each file opened if ( fd_isset( fileno( files[i]), &rfds)) { // read printf( "descriptor %d ready read", i); } } }
Comments
Post a Comment