Last active
December 14, 2015 11:38
-
-
Save sevki/5080370 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* this example shows how named pipes may be used */ | |
#define _OPEN_SYS | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <fcntl.h> | |
#include <sys/wait.h> | |
/* * | |
* Sample use of mkfifo() * | |
* */ | |
main() | |
{ | |
/* start of program */ | |
int flags, ret_value, c_status; | |
pid_t pid; | |
size_t n_elements; | |
char char_ptr[32]; | |
char str[] = "string for fifo "; | |
char fifoname[] = "temp.fifo"; | |
FILE *rd_stream,*wr_stream; | |
if ((mkfifo(fifoname,S_IRWXU)) != 0) { | |
printf("Unable to create a fifo; errno=%d\n",errno); | |
exit(1); | |
/* Print error message and return */ | |
} | |
if ((pid = fork()) < 0) { | |
perror("fork failed"); | |
exit(2); | |
} | |
if (pid == (pid_t)0) { | |
/* CHILD process */ | |
/* issue fopen for write end of the fifo */ | |
wr_stream = fopen(fifoname,"w"); | |
if (wr_stream == (FILE *) NULL) { | |
printf("In child process\n"); | |
printf("fopen returned a NULL, expected valid stream\n"); | |
exit(100); | |
} | |
/* perform a write */ | |
n_elements = fwrite(str,1,strlen(str),wr_stream); | |
if (n_elements != (size_t) strlen(str)) { | |
printf("Fwrite returned %d, expected %d\n", | |
(int)n_elements,strlen(str)); | |
exit(101); | |
} | |
exit(0); | |
/* return success to parent */ | |
} else { | |
/* PARENT process */ | |
/* issue fopen for read */ | |
rd_stream = fopen(fifoname,"r"); | |
if (rd_stream == (FILE *) NULL) { | |
printf("In parent process\n"); | |
printf("fopen returned a NULL, expected valid pointer\n"); | |
exit(2); | |
} | |
/* get current flag settings of file */ | |
if ((flags = fcntl(fileno(rd_stream),F_GETFL)) == -1) { | |
printf("fcntl returned -1 for %s\n",fifoname); | |
exit(3); | |
} | |
/* clear O_NONBLOCK and reset file flags */ | |
flags &= (O_NONBLOCK); | |
if ((fcntl(fileno(rd_stream),F_SETFL,flags)) == -1) { | |
printf("\nfcntl returned -1 for %s",fifoname); | |
exit(4); | |
} | |
/* try to read the string */ | |
ret_value = fread(char_ptr,sizeof(char),strlen(str),rd_stream); | |
if (ret_value != strlen(str)) { | |
printf("\nFread did not read %d elements as expected ", | |
strlen(str)); | |
printf("\nret_value is %d ",ret_value); | |
exit(6); | |
} | |
if (strncmp(char_ptr,str,strlen(str))) { | |
printf("\ncontents of char_ptr are %s ", | |
char_ptr); | |
printf("\ncontents of str are %s ", | |
str); | |
printf("\nThese should be equal"); | |
exit(7); | |
} | |
ret_value = fclose(rd_stream); | |
if (ret_value != 0) { | |
printf("\nFclose failed for %s",fifoname); | |
printf("\nerrno is %d",errno); | |
exit(8); | |
} | |
ret_value = remove(fifoname); | |
if (ret_value != 0) { | |
printf("\nremove failed for %s",fifoname); | |
printf("\nerrno is %d",errno); | |
exit(9); | |
} | |
pid = wait(c_status); | |
if ((WIFEXITED(c_status) !=0) &&; (WEXITSTATUS(c_status) !=0)) { | |
printf("\nchild exited with code %d",WEXITSTATUS(c_status)); | |
exit(10); | |
} | |
} | |
/* end of else clause */ | |
printf("About to issue exit(0), \ | |
processing completed successfully\n"); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment