0

I'm trying to create a named pipe in C, but have not had any success.

Here is my code:

pid_t pid = getpid() ;
char * pipeNameo = malloc( sizeof(char) * 100 ) ;
len = 0 ;

//len += sprintf( pipeNameo + len, "%s", "/Users/Davidb/Desktop/") ; // tried various paths
len += sprintf( pipeNameo + len, "%ld", (long)pid) ;
len += sprintf( pipeNameo + len, "%s", "_") ;
len += sprintf( pipeNameo + len, "%d", i) ; // it is in a loop, i starts at 0 and increments
len += sprintf( pipeNameo + len, "%s", "o") ;

printf("pipeNameo : %s\n", pipeNameo ) ;
val = mkfifo(pipeNameo, 0666) ;
printf("Did named pipe succeed: %d\n", val) ;

So after running this, I check the directory, and there is no file being made. Here is some sample output:

OUTPUT, when loop runs twice
pipeNameo : /Users/Davidb/Desktop/1152_0o
Did named pipe succeed: 0
pipeNameo : /Users/Davidb/Desktop/1152_1o
Did named pipe succeed: 0

Please help :)

UPDATE Ok I see that the ls -l worked for when the path was set to my Desktop! But it doesn't work when I try and set it to the current directory (where the main.c is stored). I tried adding "/." and "/" before the pipe Name, neither worked.

David Baez
  • 1,208
  • 1
  • 14
  • 26

1 Answers1

0

mkfifo() will create the Named pipe for Inter process communication in the name what you are giving in pipeNameo. Named pipes are created in hard disk for IPC mechanism.

In Command line give ls -l to view that file.

For more reference mkfifo()

Sathish
  • 3,740
  • 1
  • 17
  • 28
  • But the fifo should be visible in the file system (otherwise other processes could not open it). – Martin R Jul 22 '14 at 18:21
  • @MartinR but Is it visible normally like other files? – Sathish Jul 22 '14 at 18:33
  • Yes it is, if you are calling `ls -l` the first flag will be shown as 'p': `prw-r--r-- 1 leo abkdev 0 2014-07-21 19:32 fifo` – Ingo Leonhardt Jul 22 '14 at 18:39
  • Ok I see that the ls -l worked for when the path was set to my Desktop! But it doesn't work when I try and set it to the current directory (where the main.c is stored). I tried adding "/." and "/" before the pipe Name, neither worked. – David Baez Jul 22 '14 at 20:19
  • @DavidBaez did you try it without giving path? i mean only the name! In this case i think it will create pipe in your PWD. – Sathish Jul 23 '14 at 05:03