I'm trying to put a buffer to the pipe but the PARSER() can't find the fd[] array? Do I have a scope issue? Any help.. BTW my first post
Code:
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include <time.h>
#define MAX_LENGTH 80
int main( int argc, char **argv )
{
pid_t c_pid;
int fd[2];
if ( argc != 2 ) {
fprintf(stderr, "Usage: %s time_unit\n", argv[0]);
exit(EXIT_FAILURE);
}
/* you have to write this routine */
if ( STRING_TO_POS_INT(argv[1]) <= 0 ) {
fprintf(stderr, "Invalid Round Robin time quantum \n");
exit(EXIT_FAILURE);
}
if ( pipe(fd) == -1 ) { /*create a pipe*/
perror("Cannot create a pipe\n");
exit(EXIT_FAILURE);
}
if ( (c_pid = fork()) == -1 ) {
perror("Cannot spawn a child process\n");
exit(EXIT_FAILURE);
}
else if ( c_pid == 0 ) { /* child process */
char pipestring[10];
int flag = fcntl(fd[0], F_GETFL, 0);
close(fd[1]);
fcntl(fd[0], F_SETFL, flag|O_NONBLOCK); /*child reads pipe asynchronously*/
sprintf(pipestring, "%d", fd[0]);
execl("./scheduler", "./scheduler", argv[1], pipestring, NULL);
exit(EXIT_SUCCESS);
}
else { /* parent process */
close(fd[0]);
PARSER();
exit(EXIT_SUCCESS);
}
}
PARSER(void)
{
char inputbuf[MAX_LENGTH];
for ( ; ; )
{
gets(inputbuf);
if (strncmp(inputbuf, "run", 3) == 0)
{
// pipe a run command
write(fd[1], inputbuf)
}
else if (strncmp(inputbuf, "pause", 5) == 0)
{
// call the sleep function
int num;
sscanf(inputbuf,"%*s %d", num);
sleep(num);
}
else
break;
}
wait(NULL);
exit(0);
}