right you have given me alot to take in i have had the weekend off and tomorrow i will continue reading and coding. I would like to thank all of your input and help and i will keep you posted on my progress
thanks alot
right you have given me alot to take in i have had the weekend off and tomorrow i will continue reading and coding. I would like to thank all of your input and help and i will keep you posted on my progress
thanks alot
i dont understand why have you got 3 different pipes?? and how does the priority link with the process id pipe.
i have been messing around with my first process and well im not sure if its working because i dont really know how to read from the fifo soo far i have this code
from my output it seems to be working but i dont know whats going on in the background i need to make another .c file which will read from the other end of the fifo and output it on screen in the order with the prioirt and pid the same as goin inCode:#include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <limits.h> #include <fcntl.h> #include <string.h> #define FIFO_NAME "my_fifo" int myfork(); int main(void) { int numofpro, i; printf("How many child processes? (max5) >"); scanf("%d", &numofpro); printf("\n%d Child Process Being Created...please wait...\n\n",numofpro); for(i=0;i<numofpro;i++) { myfork(); } printf("Process 1 is sucsessfully running please start process 2\n\n "); } int myfork(void) { int random, pipePrioity[2], pipePID[2]; pid_t pid; int myfifoNum; int fifoint2; char fifochar1[80]; if (access(FIFO_NAME, F_OK) == -1) { /* check if fifo already exists*/ fifoint2 = mkfifo(FIFO_NAME, 0777); /* if not then, create the fifo*/ if (fifoint2 != 0) { fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME); exit(EXIT_FAILURE); } } random = rand()%50; sleep(2); pid = fork(); if (pid == 0) { printf("\nChild Prioity = %d", random); printf("\nChild PID = %d\n\n", getpid()); char printTemp[10]; sprintf(printTemp, "%i", random); write(pipePrioity[1], printTemp, 5); sprintf(printTemp, "%i", getpid()); write(pipePID[1], printTemp, 5); } else if (pid > 0) { sprintf( fifochar1, "%d|%d", random, getpid() ); fifoint2 = open(FIFO_NAME, O_WRONLY); if ((myfifoNum = write(fifoint2, fifochar1, strlen(fifochar1))) == -1) { perror("write"); printf("\nERROR WRITING TO PIPE\n"); } else { printf("\nPrint Job Sent to the Killer (DeSpooler)."); } exit(0); } else { perror("myfork"); printf("Error Creating Print Job. Print Job Not Created"); exit(1); } }
this is my output :
why am i getting all these random numbers comming up is it something to do with the getpid();Code:How many child processes? (max5) >5 5 Child Process Being Created...please wait... Child Prioity = 83 Child PID = 7651 83��7651 Child Prioity = 86 Child PID = 7652 8617652 Child Prioity = 77 Child PID = 7653 7727653 Child Prioity = 15 Child PID = 7654 1537654 Child Prioity = 93 Child PID = 7655 9347655Process 1 is successfully running please start process 2
am i on the right track?.. sorry for pestering its just that im hitting a brick wall and well i cant find nothing on the net about fifo
thankyou
Last edited by fortune2k; 03-03-2009 at 10:07 AM.
Technically, there are no pipes at all in that code. I was just reading through your assignment specs, and it looks to me like you might want need to get a grip on this (and yes, sorry, I think you are off to a rough start).
The point of pidqueue[3] was to collect the process ids of the children doing the printing. Let me flesh it out a bit and get rid of the unnecessary return value stuff:
I am not telling you to just copy my idea, of course. But you should get the general flow of your program worked out BEFORE you add the fifo, because from the looks of your last code you are just becoming confused by it. You need to decide what the pipe is for in more concrete terms.Code:#include <stdio.h> #include <stdlib.h> /* needed for exit() */ #include <unistd.h> int printfunc(char *what, int ID) { pid_t pid=fork(); if (pid<0) { perror("fork"); return -1; } /* fork() failed */ if (pid==0) { printf("\tprinting %s (task %d)...\n",what,ID); /* simulate unique printing task */ exit (0); } else return (int)pid; } int main() { char tasks[3][16]={"une","deux","trois"}; /* 3 tasks for 3 children */ int pidqueue[3],i,tmp; for (i=0; i<3; i++) { if ((tmp=printfunc(tasks[i],i))==-1) return 0; /* in this example, just quit if fork() fails */ else pidqueue[i]=tmp; /* add child's pid to our list of pids */ printf("task %d is process %d\n",i,pidqueue[i]); /* you would send to a named pipe (fifo) here instead of just using printf */ } /* now the other "de-spooling" program has the pids and can cancel/kill (or re-nice) them */ return 0; }
If you are having trouble just using mkfifo, etc. write two very short programs WITHOUT ANY FORK(), where one opens a pipe and the two exchange messages, because that is what you are going to use it for, right?
Here's sample output for that:
Code:printing une (task 0)... task 0 is process 2347 task 1 is process 2348 printing deux (task 1)... printing trois (task 2)... task 2 is process 2349
Last edited by MK27; 03-03-2009 at 10:57 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I think I was a little rough with my criticism actually. Your program will work, I think, but the communication with the other program will be more complicated if you do not give it the pids.
Focus on writing some small simple experiments with fifo pipes and leave the rest until you have the hang of that. When I was learning Inter Process Communication using sockets last year I ended up having to write an entire ncurses server and various clients before I felt comfortable enough to include any of it in a larger project.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
yea its ok , im looking around for info about fifos i dont suppose you know any good sites/books where i can learn about fifo and maybe get a little program goin ??
The reason you are not finding much is because a fifo works like a file, so all you need to do is read the mkfifo() documentation, if you know how to read and write to a stream or descriptor.
There is a complication, however, in that there is no EOF in effect! That means when you try to read from the fifo, execution will pause permanently because the fifo has not ended. So you need some method to decide when to stop reading:
I also tried using two file handles (a read and a write), then closing one hoping this would create an EOF, but that doesn't work (because the fifo really still ain't over).Code:#include <stdio.h> #include <unistd.h> #include <sys/stat.h> int main() { int i=0; char byte, buffer[256]; FILE *fifo; if (mkfifo("pipefile", S_IRUSR | S_IWUSR)!=0) perror("mkfifo"); fifo=fopen("pipefile", "r+"); /* read and write to filehandle */ fprintf(fifo, "hello there!\n"); fflush(fifo); /* notice the newline */ while ((fread(&byte,1,1,fifo))) { /* testing for truth, extra brackets */ buffer[i]=byte; i++; if (byte=='\n') break; /* message is finished */ } buffer[i]=0; /* need to null terminate string */ printf("pipe said %s",buffer); fclose(fifo); unlink("pipefile"); /* erase it */ return 0; }
Anyway, the while ((fread)) is a handy one for lots of C stuff -- you could use while (1) with the fread inside here, since it will always be true (the only real way out of this loop is thru the break, so make sure you newline terminate your messages!)
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
hmmm i have been looking for hours and guessing code to try come up with somthing right i shall have a play around
right after a little play with the code you posted up i have managed to have 2 seperate C files 1wrting and 1 reading and it seems to work thanks now i need to build upon this i suppose and get the pids and que numbers sent across ect ect. Thankyou sooo much for the help i really appreciate this
1.c THE WRITER
2.c READERCode:#include <stdio.h> #include <unistd.h> #include <sys/stat.h> int main() { int i=0; char byte, buffer[256]; FILE *fifo; if (mkfifo("pipefile", S_IRUSR | S_IWUSR)!=0) perror("mkfifo"); //write to the pipe printf("Message sent :)\n"); fifo=fopen("pipefile", "r+"); /* read and write to filehandle */ fprintf(fifo, "hello there!\n"); fflush(fifo); /* notice the newline */ fclose(fifo); unlink("pipefile"); /* erase it */ return 0; }
Code:#include <stdio.h> #include <unistd.h> #include <sys/stat.h> int main() { printf("Waiting.........\n"); int i=0; char byte, buffer[256]; FILE *fifo; if (mkfifo("pipefile", S_IRUSR | S_IWUSR)!=0) perror("mkfifo"); //read from the pipe fifo=fopen("pipefile", "r+"); /* read and write to filehandle */ while ((fread(&byte,1,1,fifo))) { /* testing for truth, extra brackets */ buffer[i]=byte; i++; if (byte=='\n') break; /* message is finished */ } buffer[i]=0; /* need to null terminate string */ printf("pipe said %s",buffer); fclose(fifo); unlink("pipefile"); /* erase it */ return 0; }
ouput of 1.c
./1
mkfifo: File exists
Message sent
output of 2.c
./2
Waiting.........
pipe said hello there!
You don't have to mkfifo() in both programs -- you only need it in the first one. Then the file will exist, and all you have to do is open it in the second one.
Good luck.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
why is my program looping ?
i run it and get a spam of
and well that doesnt stop i have to terminate the terminal to stop itCode:mkfifo: file exists mkfifo: file exists mkfifo: file exists mkfifo: file exists
i have no loops set this only happens when i put the fifo and fork stuff in its own function which i need
Code:int fork(); int main(void) { fork(); return 0; } int fork(void) { int i=0, random; FILE *fifo; pid_t pid; random = rand()%20; /* Make Fifo and open it */ if (mkfifo("pipefile", S_IRUSR | S_IWUSR)!=0) perror("mkfifo"); fifo=fopen("pipefile", "r+"); /* read and write to filehandle */ pid = fork(); // fork if (pid == 0) /* child*/ { fprintf(fifo, "Prio : %d PID : %d \n",random,getpid()); fflush(fifo); /* notice the newline */ printf("Message sent ..\n"); } else { /*PARENT*/ } fclose(fifo); return 0; }
:O
Last edited by fortune2k; 03-03-2009 at 03:48 PM.
It is correct. chmod takes mode values in octal. 0777 is an octal constant. This is masked by the umask to produce the actual file mode. Passing 0777 as the initial mode is, in almost all circumstances, the right thing to do, since it allows the user to control his/her default file permissions via the umask.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
my program is all over the place atm i have alot of reading to do and understanding im way over my head with this
right this is what im trying to do
i have been around in circles and tried various ways and well im stuck now sooo i dont suppose you guys have a simple way of dealing with thisCode:First process This process will be required to run to create a series of child processes that will represent each of the printer jobs. The PID number of each child is to be inserted / spooled within a priority queue. The queue will be made available to the second process to allow de-spooling. The process should be configurable using command line arguments. Second Process This process will be separate from the first and running within a separate console window and will simulate the de-spooling process. The process should be configurable using command line arguments. It should perform the following functions: Find and delete a number of printer jobs by priority order. Print a message to notify the user of the deletion. Kill the corresponding process. It will be necessary to develop a communication mechanism to transfer data between the two main processes. It should be possible to configure the maximum number of jobs allowed within the whole system. It should also be possible to configure the consumer and / or the producer to specify how many jobs they will de-spool or spool during each cycle. You should also include a very brief set of user instructions that will allow an assessor to run your code in your absence.
im also willing to pay for code/help im just fed up of trying to get this working ive spent days on end and still in the same spot
Last edited by fortune2k; 03-03-2009 at 06:47 PM.
In case you're still looking, here's a great chapter about pipes and fifos. It's a really good read if you're doing work with that kind of program
http://www.linuxhq.com/guides/LPG/node7.html
thankyou this is the kind of thing i have been looking for something to explain about fifos
i have a problem tho is this and i dont understand why the loop is happening and ideas? the program needs to make a number of child processes and send their info across the pipe and another terminal running another .c will have to read the process and id the other side sooo a loop is needed . The pipe seems to be the problem
Last edited by fortune2k; 03-04-2009 at 09:30 AM.
Last edited by MK27; 03-04-2009 at 10:16 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge