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; well i took the mkfifo out of 2.c Code: int main() { printf("Waiting.........\n"); int i=0; char byte, buffer[1000]; FILE *fifo; ...

  1. #31
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    well i took the mkfifo out of 2.c
    Code:
    int main() {
    	printf("Waiting.........\n");
    	int i=0;
    	char byte, buffer[1000];
    	FILE *fifo;
    
            //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("%s",buffer);
    
    
    	fclose(fifo);
    	return 0;
    }

    soo i only have the mkfifo running in one program 1.c is my main program that does all the fifo making putting stuff into the pipe making the children processes

  2. #32
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yes. Maybe you haven't realized this yet (depending on your file browser, which may not show them) but the fifo is a real file in a real place. If you take the unlink() out, it will still be there when you are done.

    If you use ls in the directory where your program runs, you should see an entry for "pipefile" (or whatever name you give it) while the program is running (unlink erases it at the end).
    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

  3. #33
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    right i have put in the command in differnt places

    i have messed around with the unlink() command and well still the same stuff

    i get the spam mkfifo and sometimes i get segmentation fault sooo i dont know whats goin on. the pipe seems to be broken
    Last edited by fortune2k; 03-04-2009 at 01:27 PM.

  4. #34
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fortune2k View Post
    i get the spam mkfifo and sometimes i get segmentation fault sooo i dont know whats goin on. the pipe seems to be broken
    The spam you refer to is because the file exists, right? You should be able to verify that with your file browser or ls, right? If "the writer" seg faults, the unlink() probably didn't happen.

    I discovered a few minor complications with using the fifo that I imagine would be flabbergasting if you didn't have the C skills I possess
    1) You must fflush() your transmissions.
    2) You must fopen the pipe "r" (read-only) or "r+" (read and write). "w" or "a" cause it to hang.

    The first one is important to you. Another problem is that it will be awkward to read and write to the fifo in both programs, so your prof probably does not intend that -- you want the first program to write, and the second one to read, as I think you've been thinking. So I wrote two little experiments that need to be run
    1) in separate consoles
    2) in the same directory, because the fifo will be there

    THE WRITER
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/stat.h>
    
    int getinput (char *ptr) {   /* simple multi-purpose keyboard input */
    	int ch, i=0;
    	printf("Message: ");
    	while ((ch=fgetc(stdin))!='\n') { ptr[i]=ch; i++; }
    	ptr[i]='\n';  /* need this */
    	ptr[i+1]='\0';  /* null terminate */
    	return i; /* we don't use the return value (length of string) in this program */
    }
    
    
    int main() {
    	char buffer[256];
    	FILE *fifo;
    	if (mkfifo("pipefile", S_IRUSR | S_IWUSR)!=0) perror("mkfifo");
    	
    	fifo=fopen("pipefile", "r+"); /* "w" and "a" stall!! */
    	if (!(fifo)) { perror("fifo"); return 0; }
    
    	while (1) {   /* infinte loop */
    		getinput(buffer);
    		fprintf(fifo,buffer);  /* send message */
    		fflush(fifo);  /* IMPORTANT! */
    		if (strncmp(buffer,"end",3)==0) break;  /* quit */
    	}
    
    	fclose(fifo);
    	unlink("pipefile");	/* erase it */
    	return 0;
    }
    Start this first. You can type a message to send to
    THE READER
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/stat.h>
    
    int main() {
    	int i;
    	char buffer[256], byte;
    	FILE *fifo=fopen("pipefile", "r");  /* read-only */
    	if (!(fifo)) { perror("fopen"); return 0; }  /* error check */
    	
    	while (1) {  /* infinite loop */
    		i=0;	/* reset */
    		while ((fread(&byte,1,1,fifo))) {   /* our old loop */
    			buffer[i]=byte;
    			i++;
    			if (byte=='\n') break;
    		}
    		printf("message is: %s", buffer); 
    	}
    	fclose(fifo);
    	return 0;
    }
    If you type "end", the writer will erase the pipe, which causes the reader to exit cleanly.

    Read your fflush() documentation, this turns out to be essential.
    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. #35
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    writer.c: In function ‘main’:
    writer.c:26: warning: format not a string literal and no format arguments

  6. #36
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fortune2k View Post
    writer.c: In function ‘main’:
    writer.c:26: warning: format not a string literal and no format arguments
    That is very correct (gcc -Wall missed this)! Line 26 should be:
    Code:
                    fprintf(fifo,"%s",buffer);
    Also, the reader may not shut down cleanly after all, so you should use a strncmp line like the writer's that will "return 0" to close.
    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. #37
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by MK27 View Post
    That is very correct (gcc -Wall missed this)! Line 26 should be:
    Code:
                    fprintf(fifo,"%s",buffer);
    Also, the reader may not shut down cleanly after all, so you should use a strncmp line like the writer's that will "return 0" to close.
    pretty cool program i have managed to find out my problem i my 1.c didnt open up the pipe so nothing was being sent. i have made the changes and well slowly on my way to getting somewhere however i have hit another problem i have now split my program into 2 functions main and myfork (myfork does all the fork stuff makes the children and will put them into the pipe ) when i comiple i get this error:
    Code:
    1.c:(.text+0x7e): undefined reference to `myfork'
    collect2: ld returned 1 exit status
    i have looked all over the internet and messed around and it comes back to haunt me .
    Last edited by fortune2k; 03-04-2009 at 04:48 PM.

  8. #38
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I just got the same error for trying to use a function that I was going to write in the future:
    Code:
    /tmp/ccKgrMsT.o: In function `pyramid':
    scene.c:(.text+0x4e7): undefined reference to `setvect'
    scene.c:(.text+0x507): undefined reference to `setvect'
    scene.c:(.text+0x523): undefined reference to `setvect'
    collect2: ld returned 1 exit status
    But presumably that is not your problem.

    Go ahead -- post some code.
    Last edited by MK27; 03-04-2009 at 05:30 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

  9. #39
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    yeaa i have been trying to fix it for the past hour or so

    Code:
    int myfork(int i);
    
    int main(void)
    {
    int numofpro, i;
    FILE *fifo;
    
    printf("How many child processes? (max5) >");
    scanf("%d", &numofpro);
    printf("\n%d Child Process Being Created...please wait...\n\n",numofpro);
      
    
    mkfifo("pipefile", S_IRUSR | S_IWUSR);
    fifo=fopen("pipefile", "r+"); /* read to filehandle */
    
    for(i=0;i<numofpro;i++)
      {
      myfork(i);
      }
      printf("Process 1 is sucsessfully ended\n\n ");
    }
    
    
    int mfork(int i)
    {
    int random;
    int pipepid[10], pipeprior[10];
    pid_t pid;
    FILE *fifo;
    
     random = rand()%90;
     pid = fork();	// fork														
    	if (pid == 0) /* child*/
    	{
    	pipepid[i] = getpid();
    	pipeprior[i] = random;
    	fprintf(fifo, "Prio : %d   PID : %d  \n",pipepid[i],pipeprior[i]); fflush(fifo);   /* notice the newline */					
    	printf("Message sent ..\n");						
            }
            else
            {
            /*PARENT*/
            }
    	fclose(fifo);
    	unlink("pipefile");	/* erase it */
    }
    i dont know weather to have the forking in diff functions is this a good way will it work out

    i want the user to input how many children for i i will then be passed into the fork function the fork function will then make sooo many children , put the pid and priority in order put them in the fifo then i will have a read function in 2.c to read it put the pid and prior back into arrays the other side and print them out soo i can then kill process n stuff . any ideas how i could achieve this ??

    EDIT:
    i have found the problem the myfork function had a typo although now when i run 2.c i get "Segmentation Fail" sooo now i have broken my fifo again.
    This was my orignal code before i made the myfork function:
    Code:
    int main(void)
    {
    int numofpro, i=0,random;
    int pipepid[10], pipeprior[10];
    FILE *fifo;
    pid_t pid;
    random = rand()%90;
    
    
    
    printf("How many child processes? (max5) > ");
    scanf("%d", &numofpro);
    printf("\n%d Child Process Being Created...please wait...\n\n",numofpro);
    
    mkfifo("pipefile", S_IRUSR | S_IWUSR);
    fifo=fopen("pipefile", "r+"); /* read to filehandle */
    
     for(i=0;i<numofpro;i++)
      {
    	pid = fork();	// fork														
    	if (pid == 0) /* child*/
    	{
    	pipepid[i] = getpid();
    	pipeprior[i] = random;
    	fprintf(fifo, "Prio : %d   PID : %d  \n",pipepid[i],pipeprior[i]); fflush(fifo);   /* notice the newline */					
    	printf("Message sent ..\n");						
            }
            else
            {
            /*PARENT*/
            }
    
    	fclose(fifo);
    	return 0;
    
      }
    	fclose(fifo);
    	unlink("pipefile");	/* erase it */
    	return 0;
    }
    Last edited by fortune2k; 03-04-2009 at 07:57 PM.

  10. #40
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fortune2k View Post
    yeaa i have been trying to fix it for the past hour or so
    Hack, man, hack!
    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. #41
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    its annoying when small things dont work you have any ideas why my prog doesnt work in its 2 functions whereas when its all in main it works 2.c just pops the segmentation fail out something with the fifo pipe i cannot see

    right bedtime i shall continue 2moz, thanks alot and good night

  12. #42
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    I do not understand where do you open the pipe for writing? shouldn't your code be different for server and client?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #43
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by vart View Post
    I do not understand where do you open the pipe for writing? shouldn't your code be different for server and client?

    the prog that writes/puts the pids and proty nums in order then puts them into the fifo
    Code:
    int myfork(int i);
    
    int main(void)
    {
    int numofpro, i;
    FILE *fifo;
    
    printf("How many child processes? (max5) > ");
    scanf("%d", &numofpro);
    printf("\n%d Child Process Being Created...please wait...\n\n",numofpro);
    sleep(2);
    
    mkfifo("pipefile", S_IRUSR | S_IWUSR);
    fifo=fopen("pipefile", "r+"); /* read to filehandle */
    
    for(i=0;i<numofpro;i++)
      {
      myfork(i);
      }
      printf("Process 1 is sucsessfully ended \n\n ");
    
    fclose(fifo);
    unlink("pipefile");	/* erase it */
    return 0;
    }
    
    
    int myfork(int i)
    {
      int random;
      int pipepid[10], pipeprior[10];
      pid_t pid;
      FILE *fifo;
    
     random = rand()%90; /* picks a random number out of 90 which will be the process number */
     pid = fork();	/* fork	*/													
    	if (pid == 0) /* child*/
    	{
    	pipepid[i] = getpid();
    	pipeprior[i] = random;
    	fprintf(fifo, "Prio : %d   PID : %d  \n",pipepid[i],pipeprior[i]); fflush(fifo);   /* notice the newline */					
    	printf("Message sent ..\n");						
            }
            else
            {
            /*PARENT*/
            }
    
    }

    this reads the contents , out puts it, kills process ect ect
    Code:
    int main() {
    	int i=0;
    	char byte, buffer[1000];
    	FILE *fifo;
    
    	printf("Waiting.........\n");
    
            //read from the pipe
    	fifo=fopen("pipefile", "r+"); /* read to filehandle */
    	while ((fread(&byte,1,1,fifo))) 
    	{   /* testing for truth, extra brackets */
    		buffer[i]=byte;
    		i++;
    		if (byte=='*') printf("\n");
    		if (byte=='\n') break;	/* message is finished */
    
    	}
    	buffer[i]=0;	/* need to null terminate string */
    	printf("%s",buffer);
    
    	fclose(fifo);
    	return 0;
    }
    yea after some fiddling the pipe is still messed

Page 3 of 3 FirstFirst 123
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