-
write() with pipe()
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 :D
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);
}
-
Of course it has scope issues. Your variables are local ones, and you aren't passing them to the function that you're trying to use them in.
On a side note, stop using gets unless you like having people crash your program.
Quzah.
-
So can I just move the fd[] outside of main? or will I have to pass it in order to write to the pipe? Or like could I say write(1,inputbuf);?
Thanks,
-Luke
-
thanks, what does dupe() do again? I thought it made an exact copy of the file descriptor? if I'm using the write(fd[1],inputbufer) i don't need it right?
-
Ohh that would make sense that way no matter who I stick in the exec it will have access to the pipe? for this case it doesn't matter, but thanks for the tip!
-Luke
-
NEXT question. I'm in my scheduler program, how do I get the string I just stuffed into the pipe out without knowing the size?
-Luke
-
Well yeah I had to change the write, but I did write(fd[1], inputbuffer, strlen(inputbuffer)+1) won't that give me the string plus the NULL written to the pipe? BTW where did you pick this stuff up so I don't keep pestering you!
-Luke
-
so if I try and read(stream, buffer, buffersize) from a pipe where the string in the pipe is of size 4 and the buffersize is 128? What happens, won't the read fail?
-Luke
-
Geez no need to get fiesty:eek: , I'm just looking at your code! There is no good reference on READ() that I've found.
-Luke
-
Real problems or problems you consider real. A real problem is reading from a pipe! esp when there is Nilch on the man pages (BTW I'm not that new)! Help or don't, but don't be an ass!
-Luke
-
-
Wow, really I wish I could be as awesome a contributor as you!(I read your *nix stuff in the FAQ section) *sigh* Now come on, I'm not whining about anything other than your manners. Hammer was nice enough to provide me with some decent help (Love to know where you got that page from!) Using google is one thing but getting information from a "c community" about an interesting topic such as IPC is another. I'm trying to elimante a problem here NOT asking for a handout! read() isn't exactly the most documented thing (and YES I did look at the man page). There was plenty on it's usage, but what really happens when reading into a buffer array of specified length. Will it read up to it's limit, will it only read until the end of the array. Stuff like matters for some people! And I was only asking because some of you "more expirenced" programers might have already fought through the trials and tribulations of funky read syntax. While I thank you for your code example I'd much rather have some more information on using read with a pipe in an async fashion. Thanks
-Luke
** Man, you unix guys hafta chill! **