I have two programs to write and read a FIFO. One is read(O_RDONLY) a FIFO. Another is write data into this FIFO. This is code:
Read: The executable file is named read.
#include<errno.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[])
{
int fd, nread;
const char *pipe_file = "/tmp/test_fifo";
char buf[0x100] ;
fd = open(pipe_file, O_RDONLY);
while(1) {
printf("\n"); /* */
memset(buf, 0, 100);
nread = read(fd, buf, 0x100);
printf("data is %s", buf);
sleep(1);
}
return 0;
}
Write: The executable file is named write.
#include<errno.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[])
{
int fd;
int num = 1234;
char *s = "changzhi";
const char *pipe_file = "/tmp/test_fifo";
fd = open(pipe_file, O_WRONLY);
write(fd, s, strlen(s));
return 0;
}
The FIFO named /tmp/test_fifo is already exists. When I run read to read FIFO and run write to write the FIFO, everything goes ok. But, the read can not read data when there is no printf("\n"); in read code. I don not know why. Could someone help me ? Thanks a lot!
Your second printf() is buffered until the \n is written. The read is not blocked :-)
Rewrite your loop:
while(1) {
memset(buf, 0, 100);
nread = read(fd, buf, 0x100);
printf("data is %s\n", buf);
sleep(1);
}
With cleanly compiling code under stringent compilation options:
$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
> -Wold-style-definition -Werror write.c -o write
$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
> -Wold-style-definition -Werror read.c -o read
$
the code basically works for me. I've added error checking. The slightly odd argv[argc-argc] prints argv[0] but avoids a warning (error because of -Werror) about argc being unused.
read.c
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fd, nread;
const char *pipe_file = "/tmp/test_fifo";
char buf[0x100];
if ((fd = open(pipe_file, O_RDONLY)) < 0)
{
fprintf(stderr, "%s: Failed to open FIFO %s\n", argv[argc-argc], pipe_file);
return 1;
}
printf("%s: open successful\n", argv[0]);
while(1) {
printf("\n"); /* */
memset(buf, 0, 100);
nread = read(fd, buf, 0x100-1);
if (nread <= 0)
break;
printf("data is %s\n", buf);
sleep(1);
}
return 0;
}
write.c
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd;
char *s = "changzhi";
const char *pipe_file = "/tmp/test_fifo";
if ((fd = open(pipe_file, O_WRONLY)) < 0)
{
fprintf(stderr, "%s: failed to open FIFO %s\n", argv[argc-argc], pipe_file);
return 1;
}
printf("%s: open successful\n", argv[0]);
if (write(fd, s, strlen(s)) <= 0)
{
fprintf(stderr, "%s: failed to open FIFO %s\n", argv[argc-argc], pipe_file);
return 1;
}
printf("%s: write successful\n", argv[0]);
return 0;
}
Sample output
$ ./read & ./write
[1] 85542
./write: open successful
./write: write successful
./read: open successful
data is changzhi
$
[1]+ Done ./read
$ ./write & ./read
[1] 85544
./write: open successful
./read: open successful
./write: write successful
data is changzhi
[1]+ Done ./write
$
The programs report an error sensibly if the FIFO does not exist.
Related
The following program implements a shell (simplified). I have a problem: when I redirect stdout to a file, the$symbols appear at the end of the file. What edits do I need to do to stop appearing?
I also have a problem running the exit command: it does not exit the program. How can I solve it?
The shell reads the commands and tries to execute them.
code:
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
char *
getinput(char *buffer, size_t buflen) {
printf("$$ ");
return fgets(buffer, buflen, stdin);
}
int
main(int argc, char **argv) {
char buf[BUFSIZ];
pid_t pid;
int status;
(void)argc;
(void)argv;
while (getinput(buf, sizeof(buf))) {
buf[strlen(buf) - 1] = '\0';
if((pid=fork()) == -1) {
fprintf(stderr, "shell: can't fork: %s\n",
strerror(errno));
continue;
} else if (pid == 0) { /* child */
execlp(buf, buf, (char *)0);
fprintf(stderr, "shell: couldn't exec %s: %s\n", buf,
strerror(errno));
exit(EX_UNAVAILABLE);
}
/* parent waits */
if ((pid=waitpid(pid, &status, 0)) < 0) {
fprintf(stderr, "shell: waitpid error: %s\n",
strerror(errno));
}
}
exit(EX_OK);
}
I also have a problem running the exit command: it does not exit the program. How can I solve it?
the terminal looks like this:
mike#ubuntu:~process/homework$ echo "foobar" > input
mike#ubuntu:~process/homework$ cat output`
$$ $$ $$ mike#ubuntu:~process/homework$ ./shell < input > output 2 > err
mike#ubuntu:~process/homework$ cat output
$$ $$ $$ mike#ubuntu:~process/homework$ cat err
shell: couldn't exec foobar: No such file or directory
exit is a shell builtin, and should be handled before any forking occurs.
Use isatty(3) to determine if stdout refers to a terminal, and only print your prompt if it does.
Here is a basic example, but do note that a real exit command supports a single numerical argument, which becomes the exit status of the shell, otherwise the exit status is that of the last command executed.
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
char *
getinput(char *buffer, size_t buflen) {
if (isatty(STDOUT_FILENO))
printf("$$ ");
return fgets(buffer, buflen, stdin);
}
int
main(int argc, char **argv) {
char buf[BUFSIZ];
pid_t pid;
int status = EXIT_SUCCESS;
(void)argc;
(void)argv;
while (getinput(buf, sizeof(buf))) {
buf[strlen(buf) - 1] = '\0';
if (0 == strcmp(buf, "exit"))
exit(WEXITSTATUS(status));
if((pid=fork()) == -1) {
fprintf(stderr, "shell: can't fork: %s\n",
strerror(errno));
continue;
} else if (pid == 0) { /* child */
execlp(buf, buf, (char *)0);
fprintf(stderr, "shell: couldn't exec %s: %s\n", buf,
strerror(errno));
exit(EX_UNAVAILABLE);
}
/* parent waits */
if ((pid=waitpid(pid, &status, 0)) < 0) {
fprintf(stderr, "shell: waitpid error: %s\n",
strerror(errno));
}
}
exit(EX_OK);
}
I have built a IPC prgram with FIFO(named piped).
A very interesting problem is that my written message may be lost.
the following is the code snippet.
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
int main()
{
char buffer[2000] = {0};
strcpy(buffer, "abc");
char *write_path = "test-123";
mkfifo(write_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
int dummy = open(write_path, O_RDONLY | O_NONBLOCK);
int fd = open(write_path, O_WRONLY | O_NONBLOCK);
int bytes = write(fd, buffer, 2000);
printf("write_path:%d %s %d %d\n", bytes, write_path, dummy, fd);
close(fd);
close(dummy);
}
how to reproduce?
ubuntu 1804
gcc main.c -o main
./main
cat < test-123
it will be pending. I think, it should output abc.
You open the FIFO in nonblocking mode, which in means I/O functions can fail with (-1 and errno set to) EAGAIN or EWOULDBLOCK, instead of blocking (waiting) for the function to complete. In Linux, open descriptors to a FIFO have the same semantics as pipes.
If you bothered to check for errors (printing strerror(errno) whenever open() or write() returns -1 indicating an error), you'd already know the reason why this fails. As is, the code shown does not even reproduce the problem. The following code,
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char *argv[])
{
const char *msg = "The Example Message.\n";
const size_t msg_len = strlen(msg);
int rfd, wfd;
if (argc != 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
const char *mypath = (argc > 0 && argv && argv[0] && argv[0][0]) ? argv[0] : "(this)";
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [ -h | --help ]\n", mypath);
fprintf(stderr, " %s FIFO\n", mypath);
fprintf(stderr, "\n");
fprintf(stderr, "This program tests opening and writing a short message to the FIFO.\n");
fprintf(stderr, "\n");
return EXIT_SUCCESS;
}
rfd = open(argv[1], O_RDONLY | O_NONBLOCK);
if (rfd == -1) {
fprintf(stderr, "%s: Cannot open FIFO for reading: %s.\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
wfd = open(argv[1], O_WRONLY | O_NONBLOCK);
if (wfd == -1) {
fprintf(stderr, "%s: Cannot open FIFO for writing: %s.\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
ssize_t n = write(wfd, msg, msg_len);
if (n == -1) {
fprintf(stderr, "%s: Cannot write to FIFO: %s.\n", argv[1], strerror(errno));
return EXIT_FAILURE;
} else
if (n != (ssize_t)msg_len) {
fprintf(stderr, "%s: Wrote only %zd of %zu bytes to the FIFO.\n", argv[1], n, msg_len);
return EXIT_FAILURE;
} else {
printf("Success!\n");
}
close(wfd);
close(rfd);
return EXIT_SUCCESS;
}
compiles (gcc -Wall -Wextra -O2 source.c -o binary) cleanly, and implements the shown code (but with error checking, and using a command line parameter for the name of the FIFO to be opened). It does not verify the named file is a FIFO, though. If run on a non-existent FIFO, it complains "FIFO: Cannot open FIFO for reading: No such file or directory."
If you create the FIFO (mkfifo test-fifo) and run the test program, there are no errors; the only output is "Success!".
If you create the FIFO, and repeatedly write data to it but never read from it, at some point the kernel buffer for the FIFO becomes full, and running the test program will report "test-fifo: Cannot write to FIFO: Resource temporarily unavailable." (which means that write() returned -1 with errno==EWOULDBLOCK or errno==EAGAIN).
You can simulate this by running e.g. bash -c 'exec 4<>test-fifo ; dd if=/dev/zero of=test-fifo bs=1 oflag=nonblock status=progress', which opens the test-fifo FIFO read-write (which in Linux always succeeds) to descriptor 4, then runs dd to fill it with zeroes, using Bash (sub-)shell. On my system, a FIFO can hold 65536 bytes (64k). After filling the FIFO like this, running the test program (./binary test-fifo) will fail as described.
Hopefully, you'll see the light and the importance and usefulness of error checking. It is NOT something you should consider as "I'll add them in later when/if I have time"; they are also an important development tool.
I am trying to write a C program which uses standard I/O and System calls to perform copying of contents of one file to another file.
So far, I have done this :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd1, fd2;
char buffer[1024];
long int n1;
if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2=open(argv[2],O_CREAT|O_WRONLY|O_TRUNC, 0700)) == -1)){
perror("file problem");
exit(1);
}
while((n1=read(fd1, buffer, 1024) > 0)){
if(write(fd2, buffer, n1) != n1){
perror("writing problem ");
exit(3);
}
}
close(fd1);
close(fd2);
}
When I run the program like this :
cc copyContents.c
./a.out one.txt two.txt
Assuming that one.txt is well defined, what I want is to create a new file called two.txt and copy over all the contents of one.txt
When I look into the contents of two.txt after running the program, it has literally nothing in it. Just a blank file.
Where am I going wrong?
You wrote
while((n1=read(fd1, buffer, 1024) > 0)){
instead of
while ( (n1 = read(fd1, buffer, 1024)) > 0)
In your code the code int the while condition boils down to:
n1 = (read(fd1, buffer, 1024) > 0)
So the read is done correctly, it's return value is compared to 0, the result of the comparision (0 or 1) is assigned to n1.
This shows once more how important it is to format your code in a way that makes it readable.
You could have debugged this easily yourself with a debugger or by inserting one or two printfs in your code.
Input:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
void typefile (char *filename)
{
int fd, nread;
char buf[1024];
fd = open (filename, O_RDONLY);
if (fd == -1) {
perror (filename);
return;
}
while ((nread = read (fd, buf, sizeof (buf))) > 0)
write (1, buf, nread);
close (fd);
}
int main (int argc, char **argv)
{
int argno;
for (argno = 1; argno < argc; argno )
typefile (argv[argno]);
exit (0);
}
Output:
student#ubuntu:~$gcc –o prg10.out prg10.c
student#ubuntu:~$cat > ff
hello`enter code here`
hai
student#ubuntu:~$./prg10.out ff
hello
hai
This is the best solution and easily executable.
input:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int f1, f2;
char buff[50];
long int n;
if(((f1 = open(argv[1], O_RDONLY)) == -1 || ((f2=open(argv[2], O_CREAT |
O_WRONLY | O_TRUNC, 0700))== 1)))
{
perror("problem in file");
exit(1);
}
while((n=read(f1, buff, 50))>0)
if(write(f2, buff, n)!=n)
{
perror("problem in writing");
exit(3);
}
if(n==-1)
{
perror("problem in reading");
exit(2);
}
close(f2);
exit(0);
}
Output:
cc sys.c
./a.out a.txt b.txt
cat b.txt
So, a.txt should have some content and this content is copied to b.txt
by "cat b.txt" you can cross-check the content(which is in "a.txt").
Narenda checks if n==-1 inside the loop, but, the loop test is n>0, so, that'll never happen.
Also, the test for a bad read should precede the attempt to write.
I have two programs, server and client. Server should read a file and then send its content through a named pipe to client. But my server reads only two chars from file, and then exits. What is wrong with this code?
server.c:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_NAME "american_maid"
int main(void)
{
char line[300];
int num, fd;
FILE *fp;
fp = fopen("out.txt","r");
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for readers...\n");
fd = open(FIFO_NAME, O_WRONLY);
printf("got a reader--type some stuff\n");
while (fgets(line, sizeof(line), fp)) {
if ((num = write(fd, line, strlen(line))) == -1)
perror("write");
else
printf("speak: wrote %d bytes\n", num);
}
fclose(fp);
return 0;
}
client.c:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_NAME "american_maid"
int main(void)
{
char s[300];
int num, fd;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for writers...\n");
fd = open(FIFO_NAME, O_RDONLY);
printf("got a writer\n");
do {
if ((num = read(fd, s, 300)) == -1)
perror("read");
else {
s[num] = '\0';
printf("tick: read %d bytes: \"%s\"\n", num, s);
}
} while (num > 0);
return 0;
}
When I run the code shown below using the command sequence:
$ ln -s server.c out.txt
$ ./client &
$ ./server
$
I get a copy of the source code printed by the client program. Similarly when I run the commands using:
$ ./server &
$ ./client
$
The revised code is not modified all that significantly. It avoids do { } while(...) loops — they're so seldom really beneficial — and is very careful about not overflowing buffers. The code also has superfluous headers removed.
server.c
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define FIFO_NAME "american_maid"
int main(void)
{
const char infile[] = "out.txt";
FILE *fp = fopen(infile, "r");
if (fp == 0)
{
fprintf(stderr, "Failed to open %s for reading", infile);
return(1);
}
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for readers...\n");
int fd = open(FIFO_NAME, O_WRONLY);
if (fd > 0)
{
char line[300];
printf("got a reader--type some stuff\n");
while (fgets(line, sizeof(line), fp))
{
int len = strlen(line);
int num = write(fd, line, len);
if (num != len)
perror("write");
else
printf("speak: wrote %d bytes\n", num);
}
close(fd);
}
fclose(fp);
return 0;
}
client.c
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#define FIFO_NAME "american_maid"
int main(void)
{
const char outfile[] = "client.out";
FILE *fp = fopen(outfile, "w");
if (fp == 0)
{
fprintf(stderr, "Failed to open %s for writing\n", outfile);
return 1;
}
printf("waiting for writers...\n");
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
int fd = open(FIFO_NAME, O_RDONLY);
if (fd > 0)
{
int num;
char s[300];
printf("got a writer\n");
while ((num = read(fd, s, sizeof(s))) > 0)
{
printf("tick: read %d bytes: \"%.*s\"\n", num, num, s);
fprintf(fp, "%.*s", num, s);
}
close(fd);
}
fclose(fp);
return 0;
}
Note that this version writes its output to file client.out; even when given a file with some very long lines to process (2049 bytes including the newline at the end), the output in client.out exactly matches the input in out.txt.
Remove the line mknod(FIFO_NAME, S_IFIFO | 0666, 0); from the file client.c. Then the program will work as expected. Server will create a file and sent the content of the file to fifo.
I have a program that takes two files as an argument. The first file is to be copied into the second. The program forks into 2 children, the first child reads file and throws it thru the pipe to the other, then the other child writes it out to a file. The two files are supposed to be identical in the end.
When I run diff to compare the two files I get the following error:
virtual#ubuntu:~/Documents/OSprojects$ ./parent test.txt test2.txt
virtual#ubuntu:~/Documents/OSprojects$ cat test.txt
123456789112233445566778899
virtual#ubuntu:~/Documents/OSprojects$ cat test2.txt
123456789112233445566778899
virtual#ubuntu:~/Documents/OSprojects$ diff test.txt test2.txt
Binary files test.txt and test2.txt differ
virtual#ubuntu:~/Documents/OSprojects$
As you can see they are both the same, but the diff prints out that they are different. Obviously its just something I don't understand about the diff cmd. Any help would be appreciated.
I believe for some reason the file I create is a binary file whereas the first file is not, but I am unaware as to why it is a binary file. I believe it might have to do with this line of code:
write(1, buf, BUF_SIZE); //write to buffer
memset(buf, '\0', BUF_SIZE);
In one of the children this is writing out to the buffer then I am clearing the buffer. Am I clearing out that buffer wrong?
Here is the result of cat -e:
virtual#ubuntu:~/Documents/OSprojects$ cat -e test2.txt
123456789112233445566778899$
^#^#^#^#virtual#ubuntu:~/Documents/OSprojects$
Here is the result of cmp:
virtual#ubuntu:~/Documents/OSprojects$ cmp test.txt test2.txt
cmp: EOF on test.txt
virtual#ubuntu:~/Documents/OSprojects$
I believe that is my problem, how can I clear out that buffer so it doesn't throw those at the end?
ALL OF MY CODE::
Parent:
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUF_SIZE 16
void exitWithError(char* errorMsg, int exitWith); //generic error out function
void launch_writer(const char* pathname, char* const argv[], int pfd[]);
void launch_reader(const char* pathname, char* const argv[], int pfd[]);
int main(int argc, char* argv[]){
//making the pipe
int pfd[2];
if(pipe(pfd) == -1) //test pipe creation
exitWithError("PIPE FAILED", 1);
//forking
pid_t reader_child_pid;
pid_t writer_child_pid;
//args for each fork
char *args_1[] = {"reader", argv[1], (char *) 0};
char *args_2[] = {"writer", argv[2], (char *) 0};
if((writer_child_pid = fork()) == -1) {
exitWithError("WRITER FORK FAILED", 1);
}
else if (writer_child_pid == 0) { //first child comes here
launch_writer("./writer", args_2, pfd);
}
else if ((reader_child_pid = fork()) == -1) {
exitWithError("READER FORK FAILED", 1);
}
else if (reader_child_pid == 0) { //second child comes here
launch_reader("./reader", args_1, pfd);
}
//parent picks up here
//close off pipe from parents end
close(pfd[0]);
close(pfd[1]);
//wait for all processes to exit before ending
for(;;) {
if(wait(NULL) == -1){
if(errno == ECHILD)
exit(0);
else {
exitWithError("WAIT ERROR", 1);
}
}
}
}
void exitWithError(char* errorMsg, int exitWith) {
perror(errorMsg);
exit(exitWith);
}
void launch_writer(const char* pathname, char* const argv[], int pfd[]) {
dup2(pfd[0], 0);
close(pfd[1]);
close(pfd[0]);
execve(pathname, argv, NULL);
perror("execve failed");
}
void launch_reader(const char* pathname, char* const argv[], int pfd[]) {
dup2(pfd[1], 1);
close(pfd[1]);
close(pfd[0]);
execve(pathname, argv, NULL);
perror("execve failed");
}
Child 1:
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#define BUF_SIZE 16
int main(int argc, char* argv[]){
//Opens file to be read from
int inFile = open(argv[1], O_RDONLY);
//declaring variables
char buf[BUF_SIZE]; //temp hold whats read/written
int read_test; //check if EOF
for(;;) {
read_test = read(inFile, buf, BUF_SIZE); //read from file
if(read_test == 0) //eof
break;
write(1, buf, BUF_SIZE); //write to buffer
memset(buf, '\0', BUF_SIZE);
}
close(inFile);
exit(0);
}
Child 2:
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#define BUF_SIZE 16
int main(int argc, char* argv[]){
//Opens a file for reading/writing, if exists then truncates, otherwise makes new one
//with correct permissions
int wri_inFile = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IWUSR);
if(wri_inFile == -1)
perror("ERROR OPENING FILE");
//declaring variables
char buf[BUF_SIZE]; //to store what is read in/written out
int read_test; //test if EOF
for(;;) {
read_test = read(0, buf, BUF_SIZE); //read from buffer
if(read_test == 0) //eof
break;
write(wri_inFile, buf, BUF_SIZE); //write to file
}
close(wri_inFile);
exit(0);
}
You don't check (and use) the read data length. Therefore, your data gets padded with garbage.
There should be actual data bytes read (read_test):
read_test = read(0, buf, BUF_SIZE); //read from buffer
if(read_test == 0) //eof
break;
write(wri_inFile, buf, BUF_SIZE); //write to file
-----------------------^^^^^^^^
The same applies to the other child. You should also check the error conditions.