Thread: helping a m8 fifo/pipes

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    147

    helping a m8 fifo/pipes

    hi my friend gave me some code and when comiling i get errors. The code contains fifo and i dont really have any idea about fifo and well i cant find any good tutorials/reading material about fifo i dont suppose anyone could tell me what i need to add to get this code working or link me to some stuff so i can learn fifo.

    Code:
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int myfork();
    
    int main(void)
    {
    myfork();
    }
    
    
    int myfork(void)
    {
    int random, pipePrioity[2], pipePID[2];
    pid_t pid;
    
    printf("Child Process Being Created...please wait...\n");
    
    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)
    {
    	int myfifoNum;
    	int fifoint2;
    	char fifochar1[80];
    
    	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)."); 
    	}
    
    	close(FIFO_NAME);
    }
    else
    {
    	perror("myfork");												
    	printf("Error Creating Print Job.  Print Job Not Created");
    	exit(1);																																			
    }

    output:
    Code:
    C:\Users\TraV\Desktop\c programming\meow2.c||In function `int myfork()':|
    C:\Users\TraV\Desktop\c programming\meow2.c|46|error: `FIFO_NAME' was not declared in this scope|
    C:\Users\TraV\Desktop\c programming\meow2.c|46|error: `O_WRONLY' was not declared in this scope|
    ||=== Build finished: 6 errors, 0 warnings ===|
    i understand that i need to define a fifo name and stuff but i dont know how

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not accustomed to seeing C:\Users and <unistd.h> in the same place. Did you get other errors about missing header files? unistd.h is the "standard Unix" header, or some such name, and I wouldn't expect you to have it on a Win32 system (I don't think mingw includes it, but I can't check that right now).

    I'm guessing you'd have more luck if you were on a linux system.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by tabstop View Post
    I'm not accustomed to seeing C:\Users and <unistd.h> in the same place. Did you get other errors about missing header files? unistd.h is the "standard Unix" header, or some such name, and I wouldn't expect you to have it on a Win32 system (I don't think mingw includes it, but I can't check that right now).

    I'm guessing you'd have more luck if you were on a linux system.
    well i have compiled it in ubuntu using cc
    and this is the output
    Code:
    trav@travUBUNTU:~/Desktop$ cc lol.c -o lol
    lol.c: In function ‘myfork’:
    lol.c:46: error: ‘FIFO_NAME’ undeclared (first use in this function)
    lol.c:46: error: (Each undeclared identifier is reported only once
    lol.c:46: error: for each function it appears in.)
    lol.c:46: error: ‘O_WRONLY’ undeclared (first use in this function)
    lol.c:48: warning: incompatible implicit declaration of built-in function ‘strlen’
    lol.c:65: error: expected declaration or statement at end of input
    still same errors i think some of the code is missing and im trying to learn fifo and getting no where

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    strlen is in <string.h>. I believe O_WRONLY is in <fcntl.h>. I'm not sure where FIFO_NAME is; I've not seen any indication that it exists. The first argument of open is supposed to be a char* to a path; a cursory reading of the manual suggests that you'll want to use mkfifo to create such a thing.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I thought fork() will not work on windows period -- but I don't know for sure.
    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

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by tabstop View Post
    strlen is in <string.h>. I believe O_WRONLY is in <fcntl.h>. I'm not sure where FIFO_NAME is; I've not seen any indication that it exists. The first argument of open is supposed to be a char* to a path; a cursory reading of the manual suggests that you'll want to use mkfifo to create such a thing.

    thanks added in those header files made some errors go away
    im getting there just had a read and mess about with fifo and i have managed to get my m8s program to compile and run using this code:
    Code:
    #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)
    {
    myfork();
    }
    
    
    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()%10;
    printf("Child Process Being Created...please wait...\n");
    
    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(EXIT_SUCCESS);
    }
    else
    {
    	perror("myfork");												
    	printf("Error Creating Print Job.  Print Job Not Created");
    	exit(1);																																			
    }
    }
    and the output is :

    ./lol
    Child Process Being Created...please wait...

    Child Prioity = 3
    Child PID = 7562

    3��

    after that the program hangs and nothing else it outputted
    i think there is a problem with the fifo/pipe anyone got any ideas?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fortune2k View Post

    after that the program hangs and nothing else it outputted
    You never read from the fifo. Why would there be any output?
    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

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by MK27 View Post
    You never read from the fifo. Why would there be any output?
    ok sorry im not very experienced with C and im new to fifo and pipes ect ect if I wanted to read from my fifo how could i implement this?

    thankyou

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by fortune2k View Post
    ok sorry im not very experienced with C and im new to fifo and pipes ect ect if I wanted to read from my fifo how could i implement this?

    thankyou
    Very similar to the write, expect you use the function read instead.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The person who wrote this code for you wasn't doing you any favours! The way the file permissions are set, for example (0777), is just wrong (this is not "chmod"). Also, you never actually open() the fifo! I suggest you look around for a tutorial to work with.

    If you wait about 20 minutes, I might have some better code for you...I've never used a FIFO in C (just with bash scripting) so I'm kind of curious and have no problems using pipe() or fork().
    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

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This is what I came up with. Notice the use of "perror" throughout -- you should look this command up and learn to use it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main() {
    	char buffer[1024];
    	int fin, fout;
    	pid_t pid;
    	if (mkfifo("pipefile", S_IRUSR | S_IWUSR)!=0) perror("mkfifo");
    
    	if ((pid=fork())<0) perror("fork");
    	if (pid==0) {		/* the child writes */
    		if ((fin=open("pipefile", O_WRONLY))<3) perror("child open");
    		if (write(fin,"hello world\0",12)<0) perror ("child write");
    		return 0;
    	} else {		/* the parent reads */
    		if ((fout=open("pipefile", O_RDONLY))<3) perror("parent open");
    		if (read(fout,buffer,1024)<0) perror("parent read");
    		printf("Message: \"%s\"\n",buffer);
    	}
    	
    	return 0;
    }
    1) Make the fifo and set read/write permission for the owner.
    2) Fork and open the fifo for writing by the child process (then write something into it).
    3) Back in the parent process, open the fifo for reading, read, and print the mesage to the screen.
    4) Stir carefully and allow to cool.

    The reason I used <3 with read and write is that 0,1, and 2 are the standard file descriptors (in, out, and error).
    Last edited by MK27; 02-26-2009 at 03:29 PM.
    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

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by MK27 View Post
    This is what I came up with. Notice the use of "perror" throughout -- you should look this command up and learn to use it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main() {
    	char buffer[1024];
    	int fin, fout;
    	pid_t pid;
    	if (mkfifo("pipefile", S_IRUSR | S_IWUSR)!=0) perror("mkfifo");
    
    	if ((pid=fork())<0) perror("fork");
    	if (pid==0) {		/* the child writes */
    		if ((fin=open("pipefile", O_WRONLY))<3) perror("child open");
    		if (write(fin,"hello world\0",12)<0) perror ("child write");
    		return 0;
    	} else {		/* the parent reads */
    		if ((fout=open("pipefile", O_RDONLY))<3) perror("parent open");
    		if (read(fout,buffer,1024)<0) perror("parent read");
    		printf("Message: \"%s\"\n",buffer);
    	}
    	
    	return 0;
    }
    1) Make the fifo and set read/write permission for the owner.
    2) Fork and open the fifo for writing by the child process (then write something into it).
    3) Back in the parent process, open the fifo for reading, read, and print the mesage to the screen.
    4) Stir carefully and allow to cool.

    The reason I used <3 with read and write is that 0,1, and 2 are the standard file descriptors (in, out, and error).
    wow thanks i shall have a look and try to understand this i have to like have another .c file which reads the other end of fifo then kills processes and stuff. Im goin off for the night

    thankyou alot i will post back

    x

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fortune2k View Post
    i have to like have another .c file which reads the other end of fifo then kills processes and stuff.
    Make the "parent" and "child" separate programs (obviously you won't need fork() then).
    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

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by MK27 View Post
    Make the "parent" and "child" separate programs (obviously you won't need fork() then).
    well this is what i have to do for my program
    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.
    im still trying to get the first process all good before i start on the second one .

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fortune2k View Post
    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.
    By the time you're done this you also will be comfortable with pipe() (or fifo'ing) and fork(), which is good because fork() in particular is great for doing things there is no other way to do, except "real" threading.
    Since all the children are doing the same thing, you probably want one function containing the fork() that you call as many times as necessary. Have the function return the pid of the child and pipe that to the other process.

    I know you've been working on this for a while now, but here's my idea which may or may not address problems you may or may not be having:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int printfunc(char *what) {
    	pid_t pid=fork();
    	if (pid<0) { perror("fork"); return -1; }
    	if (pid==0) {
    		printf("%s\n",what);
    		return 0;   /* would be better and easier to just exit (0)  */
    	}
    	else return (int)pid;
    }
    
    
    int main() {
    	int pidqueue[3],i,tmp;
    	if ((tmp=printfunc("une"))==0) return 0;	
    	else pidqueue[0]=tmp;
    	if ((tmp=printfunc("deux"))==0) return 0;
    	else pidqueue[1]=tmp;
    	if ((tmp=printfunc("trois"))==0) return 0;
    	else pidqueue[2]=tmp;
    	for (i=0; i<3; i++) {
    		printf("%d\n",pidqueue[i]);
    		/* you would send to a named pipe (fifo) here */
    	}	
    	return 0;			
    }
    
    __Example Output__
    une
    deux
    1578
    trois
    1579
    1580
    The order doesn't matter here -- this is just to stress the importance of terminating the child one way or another (just use exit). But notice there are no extra parents or children doing unwanted or duplicate things in the output, which is how you need it.

    I did not at all think about the significance or meaning of this phrase:
    "to be inserted / spooled within a priority queue."
    Which hopefully you attended some classes Probably the last aspect to deal with in your complete program.

    Oh for this:
    "The process should be configurable using command line arguments."
    Look into getopt() -- but again, experiment with it in a small separate program until you figure it out, then add it into your project.

    Good luck and good coding!
    Last edited by MK27; 03-01-2009 at 10:10 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone interested in helping me out again??
    By xMEGANx in forum C++ Programming
    Replies: 19
    Last Post: 10-04-2007, 01:43 AM
  2. assembly for M8 microprocessor series
    By GanglyLamb in forum Tech Board
    Replies: 3
    Last Post: 03-15-2006, 05:34 PM
  3. Valve helping with Doom III?
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 05-14-2004, 09:41 PM
  4. An Idea on helping newbies read the FAQ
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-23-2002, 04:42 PM
  5. interested in helping????
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 05-05-2002, 09:02 PM