helping a m8 fifo/pipes

This is a discussion on helping a m8 fifo/pipes within the C Programming forums, part of the General Programming Boards category; right you have given me alot to take in i have had the weekend off and tomorrow i will continue ...

  1. #16
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    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

  2. #17
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by MK27 View Post
    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;
    }
    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

    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)
    {
    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);																																			
      }
      
    
    }
    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 in

    this is my output :
    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
    why am i getting all these random numbers comming up is it something to do with the getpid();


    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 09:07 AM.

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fortune2k View Post
    i dont understand why have you got 3 different pipes??
    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:
    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;			
    }
    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.

    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 09: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

  4. #19
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  5. #20
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    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 ??

  6. #21
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fortune2k View Post
    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:
    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;
    }
    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).

    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

  7. #22
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    hmmm i have been looking for hours and guessing code to try come up with somthing right i shall have a play around

  8. #23
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    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
    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");
    
            //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;
    }
    2.c READER
    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!

  9. #24
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  10. #25
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    why is my program looping ?
    i run it and get a spam of
    Code:
    mkfifo: file exists
    mkfifo: file exists
    mkfifo: file exists
    mkfifo: file exists
    and well that doesnt stop i have to terminate the terminal to stop it

    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 02:48 PM.

  11. #26
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by MK27 View Post
    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").
    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);
    //}

  12. #27
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    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
    Code:
    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.
    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 this
    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 05:47 PM.

  13. #28
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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

  14. #29
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by sean View Post
    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

    Quote Originally Posted by fortune2k View Post
    why is my program looping ?
    i run it and get a spam of
    Code:
    mkfifo: file exists
    mkfifo: file exists
    mkfifo: file exists
    mkfifo: file exists
    and well that doesnt stop i have to terminate the terminal to stop it

    i have a loop set however if i input 1 for 1 child process i get infinite loop i have no idea why this is happening

    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>
    
    int fork();
    
    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++)
      {
      fork();
      }
      printf("Process 1 is sucsessfully running please start process 2\n\n ");
    }
    
    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;
    }}
    i like need the user to be able to state how many jobs they can have i have been trying





    :O
    Last edited by fortune2k; 03-04-2009 at 08:30 AM.

  15. #30
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fortune2k View Post
    why is my program looping ?
    100% sure because
    Quote Originally Posted by MK27

    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.
    @brewbuck: yeah, sorry I wrote that...probably because on my system, 0777 really does not produce the desired result, whereas using the masks does (which might be a better habit anyway)
    Last edited by MK27; 03-04-2009 at 09: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

Page 2 of 3 FirstFirst 123 LastLast
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, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21