#include #include #include #include int main(void) { pid_t pid; int pfd[2]; int i, status; FILE * sd; int x[]= {20,275,240,5,20,45,170,205,240,5,20,60,60,70,70,70,65,75,70,70,95,95}; int y[]= {50,50,25,25,25,12,12,25,25,25,50,50,70,70,85,80,80,80,80,70,70,50}; /* * Create a pipe. */ if (pipe(pfd) < 0) { perror("pipe"); exit(1); } /* * Create a child process. */ if ((pid = fork()) < 0) { perror("fork"); exit(1); } /* * The child process executes "matlab" with -nodesktop option. */ if (pid == 0) { /* * Attach standard input of this child process to read from the pipe. */ dup2(pfd[0], 0); close(pfd[1]); /* close the write end of the pipe */ execlp("scilab", "scilab", "-nw", 0); perror("exec"); exit(-1); } /* * We won't be reading from the pipe. */ close(pfd[0]); sd = fdopen(pfd[1], "w"); /* to use fprintf instead of just write */ fprintf(sd, "x= ["); for ( i= 0; i<22; i++) fprintf(sd, "%d ", x[i]); fprintf(sd, "];\n"); fprintf(sd, "y= ["); for ( i= 0; i<22; i++) fprintf(sd, "%d ", y[i]); /* other data could be sent here */ fprintf(sd, "];\n");fflush(sd); fprintf(sd, "subplot(2,1,1);\n"); fprintf(sd, "plot2d(x,y);\n"); fprintf(sd, "subplot(2,1,2);\n"); fprintf(sd, "plot2d(y,x);\n"); fflush(sd); sleep(5); fprintf(sd, "subplot(2,1,1);\n"); fprintf(sd, "plot(x);\n"); fprintf(sd, "subplot(2,1,2);\n"); fprintf(sd, "plot(abs(fft(x,-1)));\n");fflush(sd); sleep(10); fprintf(sd, "\n exit\n"); fflush(sd); /* Wait for the child to exit and * close de pipe. */ waitpid(pid, &status, 0); fclose(sd); /* * Exit with a status of 0, indicating that * everything went fine. */ exit(0); }